def censor(self, request, pk=None): bullet_point = self.get_object() bullet_point.is_removed = True bullet_point.save(update_fields=['is_removed']) content_id = f'{type(bullet_point).__name__}_{bullet_point.id}' user = request.user content_creator = bullet_point.created_by events_api.track_flag_content( content_creator, content_id, user.id ) decisions_api.apply_bad_content_decision( content_creator, content_id, 'MANUAL_REVIEW', user ) decisions_api.apply_bad_user_decision( content_creator, 'MANUAL_REVIEW', user ) return Response( self.get_serializer(instance=bullet_point).data, status=200 )
def flag(self, request, pk=None): paper = self.get_object() reason = request.data.get("reason") referrer = request.user flag = Flag.objects.create(paper=paper, created_by=referrer, reason=reason) content_id = f"{type(paper).__name__}_{paper.id}" events_api.track_flag_content(paper.uploaded_by, content_id, referrer.id) return Response(FlagSerializer(flag).data, status=201)
def flag(self, request, pk=None): item = self.get_object() user = request.user reason = request.data.get("reason") try: flag = create_flag(user, item, reason) serialized = FlagSerializer(flag) content_id = f"{type(item).__name__}_{item.id}" events_api.track_flag_content(item.created_by, content_id, user.id) return Response(serialized.data, status=201) except Exception as e: return Response( f"Failed to create flag: {e}", status=status.HTTP_400_BAD_REQUEST )
def handle(self, *args, **options): votes = Vote.objects.filter(created_by__probable_spammer=True, is_removed=False) count = votes.count() for i, vote in enumerate(votes): print('{} / {}'.format(i, count)) vote.is_removed = True content_id = f'{type(vote).__name__}_{vote.id}' decisions_api.apply_bad_content_decision(vote.created_by, content_id) events_api.track_flag_content( vote.created_by, content_id, 1, ) vote.save()
def censor(self, request, pk=None): item = self.get_object() item.remove_nested() item.update_discussion_count() content_id = f"{type(item).__name__}_{item.id}" user = request.user content_creator = item.created_by events_api.track_flag_content(content_creator, content_id, user.id) decisions_api.apply_bad_content_decision( content_creator, content_id, "MANUAL_REVIEW", user ) content_type = get_content_type_for_model(item) Contribution.objects.filter( content_type=content_type, object_id=item.id ).delete() try: if item.review: item.review.is_removed = True item.review.save() doc = item.unified_document doc_type = get_doc_type_key(doc) hubs = list(doc.hubs.all().values_list('id', flat=True)) reset_unified_document_cache( hub_ids=hubs, document_type=[doc_type, 'all'], filters=[DISCUSSED, TRENDING] ) except Exception as e: pass try: if item.paper: item.paper.reset_cache() except Exception as e: pass return Response( self.get_serializer(instance=item).data, status=200 )
def handle(self, *args, **options): low_threads = Thread.objects.annotate(text_len=Length('plain_text')).filter(text_len__lte=25, is_removed=False) thread_count = low_threads.count() for i, thread in enumerate(low_threads): print('{} / {}'.format(i, thread_count)) thread.is_removed = True content_id = f'{type(thread).__name__}_{thread.id}' if not thread.created_by: continue try: decisions_api.apply_bad_content_decision(thread.created_by, content_id) events_api.track_flag_content( thread.created_by, content_id, 1, ) except Exception as e: print(e) pass thread.save()
def censor_pdf(self, request, pk=None): paper = self.get_object() paper_id = paper.id paper.file = None paper.url = None paper.pdf_url = None paper.figures.all().delete() paper.save() content_id = f"{type(paper).__name__}_{paper_id}" user = request.user content_creator = paper.uploaded_by events_api.track_flag_content(content_creator, content_id, user.id) decisions_api.apply_bad_content_decision(content_creator, content_id, "MANUAL_REVIEW", user) decisions_api.apply_bad_user_decision(content_creator, "MANUAL_REVIEW", user) hub_ids = list(paper.hubs.values_list("id", flat=True)) hub_ids = add_default_hub(hub_ids) paper.reset_cache(use_celery=False) return Response(self.get_serializer(instance=paper).data, status=200)
def censor(self, request, pk=None): paper = self.get_object() paper_id = paper.id unified_doc = paper.unified_document cache_key = get_cache_key("paper", paper_id) cache.delete(cache_key) hub_ids = list(paper.hubs.values_list("id", flat=True)) content_id = f"{type(paper).__name__}_{paper_id}" user = request.user content_creator = paper.uploaded_by if content_creator: events_api.track_flag_content(content_creator, content_id, user.id) decisions_api.apply_bad_content_decision(content_creator, content_id, "MANUAL_REVIEW", user) decisions_api.apply_bad_user_decision(content_creator, "MANUAL_REVIEW", user) Contribution.objects.filter(unified_document=unified_doc).delete() paper.is_removed = True paper.save() censored_paper_cleanup.apply_async((paper_id, ), priority=3) unified_document = paper.unified_document unified_document.is_removed = True unified_document.save() reset_unified_document_cache( hub_ids, filters=[TRENDING, TOP, DISCUSSED, NEWEST], document_type=["all", "paper"], with_default_hub=True, ) return Response("Paper was deleted.", status=200)
def create_action(sender, instance, created, **kwargs): if created: if sender == Summary: user = instance.proposed_by elif sender == Paper or sender == PaperSubmission: user = instance.uploaded_by else: if sender == Thread: thread = instance if thread.is_removed: content_id = f"{type(thread).__name__}_{thread.id}" decisions_api.apply_bad_content_decision( thread.created_by, content_id) events_api.track_flag_content( thread.created_by, content_id, 1, ) user = instance.created_by """ If we're creating an action for the first time, check if we've been referred """ referral_content_types = [ get_content_type_for_model(Thread), get_content_type_for_model(Reply), get_content_type_for_model(Comment), get_content_type_for_model(Paper), get_content_type_for_model(ResearchhubPost), get_content_type_for_model(Hypothesis), ] if (user is not None and user.invited_by and not Action.objects.filter( user=user, content_type__in=referral_content_types).exists() and sender in [ Thread, Reply, Comment, Paper, ResearchhubPost, Hypothesis ]): timestamp = time() if calculate_show_referral(user.invited_by): referred = Distributor( distributions.Referral, user, user.invited_by, timestamp, None, ) referred.distribute() referrer = Distributor( distributions.Referral, user.invited_by, user.invited_by, timestamp, None, ) referrer.distribute() vote_types = [PaperVote, ReactionVote, BulletPointVote, SummaryVote] display = (False if (sender in vote_types or sender == PaperSubmission or sender != ReactionVote and (hasattr(instance, "is_removed") and instance.is_removed)) else True) action = Action.objects.create(item=instance, user=user, display=display) hubs = [] if sender == Paper: hubs = instance.hubs.all() elif sender != BulletPointVote and sender != SummaryVote: hubs = get_related_hubs(instance) if hubs: action.hubs.add(*hubs) create_notification(sender, instance, created, action, **kwargs) return action