<s> import bleach <EOL> from rest_framework import serializers as ser <EOL> from modularodm import Q <EOL> from framework . auth . core import Auth <EOL> from framework . exceptions import PermissionsError <EOL> from framework . guid . model import Guid <EOL> from website . files . models import StoredFileNode <EOL> from website . project . model import Comment <EOL> from rest_framework . exceptions import ValidationError , PermissionDenied <EOL> from api . base . exceptions import InvalidModelValueError , Conflict <EOL> from api . base . utils import absolute_reverse <EOL> from api . base . settings import osf_settings <EOL> from api . base . serializers import ( JSONAPISerializer , <EOL> TargetField , <EOL> RelationshipField , <EOL> IDField , TypeField , LinksField , <EOL> AuthorizedCharField ) <EOL> class CommentReport ( object ) : <EOL> def __init__ ( self , user_id , category , text ) : <EOL> self . _id = user_id <EOL> self . category = category <EOL> self . text = text <EOL> class CommentSerializer ( JSONAPISerializer ) : <EOL> filterable_fields = frozenset ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:target>' <EOL> ] ) <EOL> id = IDField ( source = '<STR_LIT>' , read_only = True ) <EOL> type = TypeField ( ) <EOL> content = AuthorizedCharField ( source = '<STR_LIT>' , required = True , max_length = osf_settings . COMMENT_MAXLENGTH ) <EOL> page = ser . CharField ( read_only = True ) <EOL> target = TargetField ( link_type = '<STR_LIT>' , meta = { '<STR_LIT:type>' : '<STR_LIT>' } ) <EOL> user = RelationshipField ( related_view = '<STR_LIT>' , related_view_kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> node = RelationshipField ( related_view = '<STR_LIT>' , related_view_kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> replies = RelationshipField ( self_view = '<STR_LIT>' , self_view_kwargs = { '<STR_LIT>' : '<STR_LIT>' } , filter = { '<STR_LIT:target>' : '<STR_LIT>' } ) <EOL> reports = RelationshipField ( related_view = '<STR_LIT>' , related_view_kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> date_created = ser . DateTimeField ( read_only = True ) <EOL> date_modified = ser . DateTimeField ( read_only = True ) <EOL> modified = ser . BooleanField ( read_only = True , default = False ) <EOL> deleted = ser . BooleanField ( read_only = True , source = '<STR_LIT>' , default = False ) <EOL> is_abuse = ser . SerializerMethodField ( help_text = '<STR_LIT>' ) <EOL> has_children = ser . SerializerMethodField ( help_text = '<STR_LIT>' ) <EOL> can_edit = ser . SerializerMethodField ( help_text = '<STR_LIT>' ) <EOL> links = LinksField ( { } ) <EOL> class Meta : <EOL> type_ = '<STR_LIT>' <EOL> def get_is_abuse ( self , obj ) : <EOL> user = self . context [ '<STR_LIT>' ] . user <EOL> if user . is_anonymous ( ) : <EOL> return False <EOL> return user . _id in obj . reports and not obj . reports [ user . _id ] . get ( '<STR_LIT>' , True ) <EOL> def get_can_edit ( self , obj ) : <EOL> user = self . context [ '<STR_LIT>' ] . user <EOL> if user . is_anonymous ( ) : <EOL> return False <EOL> return obj . user . _id == user . _id and obj . node . can_comment ( Auth ( user ) ) <EOL> def get_has_children ( self , obj ) : <EOL> return Comment . find ( Q ( '<STR_LIT:target>' , '<STR_LIT>' , Guid . load ( obj . _id ) ) ) . count ( ) > <NUM_LIT:0> <EOL> def get_absolute_url ( self , obj ) : <EOL> return absolute_reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : obj . _id } ) <EOL> def update ( self , comment , validated_data ) : <EOL> assert isinstance ( comment , Comment ) , '<STR_LIT>' <EOL> auth = Auth ( self . context [ '<STR_LIT>' ] . user ) <EOL> if validated_data : <EOL> if validated_data . get ( '<STR_LIT>' , None ) is False and comment . is_deleted : <EOL> try : <EOL> comment . undelete ( auth , save = True ) <EOL> except PermissionsError : <EOL> raise PermissionDenied ( '<STR_LIT>' ) <EOL> elif '<STR_LIT>' in validated_data : <EOL> content = validated_data . pop ( '<STR_LIT>' ) <EOL> try : <EOL> comment . edit ( content , auth = auth , save = True ) <EOL> except PermissionsError : <EOL> raise PermissionDenied ( '<STR_LIT>' ) <EOL> return comment <EOL> def get_target_type ( self , obj ) : <EOL> if not getattr ( obj . referent , '<STR_LIT>' , None ) : <EOL> raise InvalidModelValueError ( <EOL> source = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> detail = '<STR_LIT>' <EOL> ) <EOL> return obj . referent . target_type <EOL> def sanitize_data ( self ) : <EOL> ret = super ( CommentSerializer , self ) . sanitize_data ( ) <EOL> content = self . validated_data . get ( '<STR_LIT>' , None ) <EOL> if content : <EOL> ret [ '<STR_LIT>' ] = bleach . clean ( content ) <EOL> return ret <EOL> class CommentCreateSerializer ( CommentSerializer ) : <EOL> target_type = ser . SerializerMethodField ( method_name = '<STR_LIT>' ) <EOL> def get_validated_target_type ( self , obj ) : <EOL> target = obj . target <EOL> target_type = self . context [ '<STR_LIT>' ] . data . get ( '<STR_LIT>' ) <EOL> expected_target_type = self . get_target_type ( target ) <EOL> if target_type != expected_target_type : <EOL> raise Conflict ( '<STR_LIT>' . format ( expected_target_type , target_type ) ) <EOL> return target_type <EOL> def get_target ( self , node_id , target_id ) : <EOL> target = Guid . load ( target_id ) <EOL> if not target or not getattr ( target . referent , '<STR_LIT>' , None ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> elif not target . referent . belongs_to_node ( node_id ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> elif isinstance ( target . referent , StoredFileNode ) and target . referent . provider not in osf_settings . ADDONS_COMMENTABLE : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return target <EOL> def create ( self , validated_data ) : <EOL> user = validated_data [ '<STR_LIT:user>' ] <EOL> auth = Auth ( user ) <EOL> node = validated_data [ '<STR_LIT>' ] <EOL> target_id = self . context [ '<STR_LIT>' ] . data . get ( '<STR_LIT:id>' ) <EOL> try : <EOL> target = self . get_target ( node . _id , target_id ) <EOL> except ValueError : <EOL> raise InvalidModelValueError ( <EOL> source = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> detail = '<STR_LIT>' . format ( target_id ) <EOL> ) <EOL> validated_data [ '<STR_LIT:target>' ] = target <EOL> validated_data [ '<STR_LIT:content>' ] = validated_data . pop ( '<STR_LIT>' ) <EOL> try : <EOL> comment = Comment . create ( auth = auth , ** validated_data ) <EOL> except PermissionsError : <EOL> raise PermissionDenied ( '<STR_LIT>' ) <EOL> return comment <EOL> class CommentDetailSerializer ( CommentSerializer ) : <EOL> """<STR_LIT>""" <EOL> id = IDField ( source = '<STR_LIT>' , required = True ) <EOL> deleted = ser . BooleanField ( source = '<STR_LIT>' , required = True ) <EOL> class CommentReportSerializer ( JSONAPISerializer ) : <EOL> id = IDField ( source = '<STR_LIT>' , read_only = True ) <EOL> type = TypeField ( ) <EOL> category = ser . ChoiceField ( choices = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] , required = True ) <EOL> message = ser . CharField ( source = '<STR_LIT:text>' , required = False , allow_blank = True ) <EOL> links = LinksField ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class Meta : <EOL> type_ = '<STR_LIT>' <EOL> def get_absolute_url ( self , obj ) : <EOL> comment_id = self . context [ '<STR_LIT>' ] . parser_context [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return absolute_reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { <EOL> '<STR_LIT>' : comment_id , <EOL> '<STR_LIT>' : obj . _id <EOL> } <EOL> ) <EOL> def create ( self , validated_data ) : <EOL> user = self . context [ '<STR_LIT>' ] . user <EOL> comment = self . context [ '<STR_LIT>' ] . get_comment ( ) <EOL> if user . _id in comment . reports and not comment . reports [ user . _id ] . get ( '<STR_LIT>' , True ) : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> try : <EOL> comment . report_abuse ( user , save = True , ** validated_data ) <EOL> except ValueError : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> return CommentReport ( user . _id , ** validated_data ) <EOL> def update ( self , comment_report , validated_data ) : <EOL> user = self . context [ '<STR_LIT>' ] . user <EOL> comment = self . context [ '<STR_LIT>' ] . get_comment ( ) <EOL> if user . _id != comment_report . _id : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> try : <EOL> comment . report_abuse ( user , save = True , ** validated_data ) <EOL> except ValueError : <EOL> raise ValidationError ( '<STR_LIT>' ) <EOL> return CommentReport ( user . _id , ** validated_data ) <EOL> class CommentReportDetailSerializer ( CommentReportSerializer ) : <EOL> """<STR_LIT>""" <EOL> id = IDField ( source = '<STR_LIT>' , required = True ) </s>
