def rewards_for_posting(request, quest_id, facebook=False, tumblr=False): rewards = defaultdict(int) def reward(name): rewards[name] += knobs.REWARDS[name] if not request.user.is_authenticated() or QuestComment.posting_in_first_quest(request.user): reward('first_quest') return {'rewards': rewards} quest = get_object_or_404(Quest, id=quest_id) if QuestComment.posting_would_complete_quest_of_the_day(request.user, quest): reward('quest_of_the_day') elif QuestComment.posting_would_complete_archived_quest(request.user, quest): reward('archived_quest') if QuestComment.posting_in_first_quest(request.user): reward('first_quest') if facebook: reward('personal_share') if tumblr: reward('personal_share') streak = QuestComment.posting_would_reward_streak(request.user, quest) if streak: reward('streak_{}'.format(streak)) return {'rewards': rewards}
def moderation_queue(): sections = [ QuestComment.unjudged_flagged().order_by('-id'), QuestComment.by_unknown_users().order_by('-id').exclude(flags__undone=False), QuestComment.by_distrusted_users().order_by('-id').exclude(flags__undone=False), ] return chain(*sections)
def moderation(request, id_range=None): flagged = QuestComment.unjudged_flagged().order_by('-id') unknown = QuestComment.by_unknown_users().order_by('-id').exclude(flags__undone=False) #distrusted = QuestComment.by_distrusted_users().order_by('-id').exclude(flags__undone=False) #trusted = QuestComment.by_trusted_users().order_by('-id').exclude(flags__undone=False) ctx = _moderation_context([flagged, unknown], id_range=id_range) ctx.update({ 'flagged_count': flagged.count(), 'trusted_user_count': UserInfo.objects.filter(trusted=True).count(), }) return r2r_jinja('whitelisting/moderation.html', ctx, request)
def flag_queue(request): ctx = { 'comments': QuestComment.unjudged_flagged().order_by('id')[:knobs.WHITELIST_COMMENTS_PER_PAGE], 'body_class': 'flag_queue', 'unjudged_count': QuestComment.objects.filter(judged=False).count(), } return r2r_jinja('whitelisting/flagged.html', ctx, request)
def new_paginated(request, after_id=None): ctx = { 'comments': QuestComment.unjudged().filter( id__gt=after_id, ).order_by('-id')[:knobs.WHITELIST_COMMENTS_PER_PAGE], } return r2r_jinja('whitelisting/whitelist_items.html', ctx, request)
def post_quest_comment(request, quest_id, content_id, fact_metadata={}, facebook_share=False, facebook_access_token=None): # Rate-limit? if not request.user.is_staff: prefix = 'user:{}:post_limit:'.format(request.user.id) if not RateLimit(prefix+'h', 60, 60*60).allowed() or not RateLimit(prefix+'d', 100, 8*60*60).allowed(): raise ServiceError("Attempting to post drawings too quickly.") _, parent_comment, content, _, _, _ = validate_and_clean_comment( request.user, parent_comment=quest_id, reply_content=content_id, ) if facebook_share: if not facebook_access_token: raise ServiceError("Can't share to your timeline if you haven't signed into Facebook yet.") associate_facebook_account(request.user, facebook_access_token) comment = QuestComment.create_and_post(request, request.user, content, parent_comment, fact_metadata=fact_metadata) if facebook_share: complete_quest(request.user, comment, facebook_access_token, request=request) return { 'comment': comment.details(), 'balance': economy.balance(request.user), }
def starred_comments_gallery(user, offset='top', direction='next'): stars = CommentSticker.objects.filter(user=user).order_by('-timestamp') pagination = Paginator(stars, knobs.COMMENTS_PER_PAGE, offset=offset, direction=direction) comments = CachedCall.multicall([QuestComment.details_by_id(id_) for id_ in pagination.items.values_list('comment_id', flat=True)]) return comments, pagination
def top_comments(user): comments = QuestComment.by_author(user).filter(star_count__gt=0, visibility=Visibility.PUBLIC) comments = comments.order_by('-star_count') comments = comments[:8] if len(comments) < 4: return None return comments
def whitelisting_paginated(request, after_id=None): freeze_id = redis.get('dq:comment_freeze_id') comments = [] if after_id >= freeze_id: comments = QuestComment.unjudged().filter( id__gt=after_id, ).order_by('id')[:knobs.WHITELIST_COMMENTS_PER_PAGE], return r2r_jinja('whitelisting/whitelist_items.html', {'comments': comments}, request)
def test_auto_moderation_from_flags(self): cmt = create_quest_comment() for i in range(1, settings.AUTO_MODERATE_FLAGGED_COMMENTS_THRESHOLD + 1): resp = self.api_post('/api/comment/flag', {'comment_id': cmt.id}) cmt = QuestComment.all_objects.get(pk=cmt.pk) getattr(self, 'assert' + str(i == settings.AUTO_MODERATE_FLAGGED_COMMENTS_THRESHOLD))( cmt.visibility == Visibility.DISABLED) self.assertTrue(cmt.id in [qc.id for qc in QuestComment.unjudged_flagged()])
def new_divvy(request, id_range=None): from_, to = get_divvy_range(id_range) per_page = knobs.WHITELIST_COMMENTS_PER_PAGE * (to - from_) comments = divvy(QuestComment.unjudged().order_by('-id')[:per_page], from_, to) ctx = { 'comments': comments, 'body_class': 'new', 'unjudged_count': QuestComment.objects.filter(judged=False).count(), } return r2r_jinja('whitelisting/new.html', ctx, request)
def user_comments(request, username, since_id=None, before_id=None): user = get_object_or_404(User, username=username) comments = QuestComment.by_author(user) paginator = Paginator(comments, knobs.COMMENTS_PER_PAGE, since_id=since_id, before_id=before_id) comments = paginator.items comments = CachedCall.multicall([cmt.details for cmt in comments]) comments = filter_frozen_comments(comments) return {'comments': comments, 'pagination': paginator}
def user_comments(request, username, page='top'): user = get_object_or_404(User, username=username) comments = QuestComment.by_author(user) if request.user.id != user.id: comments = comments.exclude(visibility=Visibility.CURATED) comments = comments[:knobs.COMMENTS_PER_PAGE] comments = CachedCall.queryset_details(comments) return { 'comments': comments, }
def post_quest_comment(request, quest_id, content_id, fact_metadata={}, facebook_share=False, facebook_access_token=None, twitter_share=False, twitter_access_token=None, twitter_access_token_secret=None, email_share=False, email_recipients=[], resolve_share_ids=[], uuid=None): if not request.user.is_staff and not settings.STAGING: prefix = 'user:{}:post_limit:'.format(request.user.id) if not RateLimit(prefix+'h', 60, 60*60).allowed() or not RateLimit(prefix+'d', 100, 8*60*60).allowed(): raise ServiceError("Attempting to post drawings too quickly.") _, parent_comment, content, _, _ = validate_and_clean_comment( request.user, parent_comment=quest_id, reply_content=content_id, ) if facebook_share: facebook_share = sns_publishing.facebook_share_pre_post(request, facebook_access_token) if twitter_share: twitter_share = sns_publishing.twitter_share_pre_post(request, twitter_access_token, twitter_access_token_secret) comment = QuestComment.create_and_post(request, request.user, content, parent_comment, uuid=uuid, fact_metadata=fact_metadata, debug_content_id=content_id) if facebook_share: sns_publishing.facebook_share_post_post(request, facebook_access_token, comment) if twitter_share: sns_publishing.twitter_share_post_post(request, twitter_access_token, twitter_access_token_secret, comment) for share_id in resolve_share_ids: share = ShareTrackingUrl.objects.get(id=share_id) if share.redirect_url: pass #TODO log some error here without failing this request share.redirect_url = comment.get_share_page_url() share.save() if email_share: @bgwork.defer def defer_email_share(): sns_publishing.share_comment_by_email(comment, request.user, email_recipients) return { 'comment': comment.details(), 'balance': economy.balance(request.user), }
def whitelisting(request): freeze_id = redis.get('dq:comment_freeze_id') comments = [] if freeze_id is not None: comments = QuestComment.unjudged().filter( id__gte=freeze_id, ).order_by( 'id')[:knobs.WHITELIST_COMMENTS_PER_PAGE] ctx = { 'comments': comments, 'enabled': freeze_id is not None, } return r2r_jinja('whitelisting/whitelisting.html', ctx, request)
def whitelisting(request): freeze_id = redis.get('dq:comment_freeze_id') comments = [] if freeze_id is not None: comments = QuestComment.unjudged().filter( id__gte=freeze_id, ).order_by('id')[:knobs.WHITELIST_COMMENTS_PER_PAGE] ctx = { 'comments': comments, 'enabled': freeze_id is not None, } return r2r_jinja('whitelisting/whitelisting.html', ctx, request)
def test_auto_moderation_from_flags(self): cmt = create_quest_comment() for i in range(1, knobs.AUTO_DISABLE_FLAGGED_COMMENTS_THRESHOLD[None] + 1): resp = self.api_post('/api/comment/flag', {'comment_id': cmt.id}) cmt = QuestComment.all_objects.get(pk=cmt.pk) getattr( self, 'assert' + str(i == knobs.AUTO_DISABLE_FLAGGED_COMMENTS_THRESHOLD[None]))( cmt.visibility == Visibility.DISABLED) self.assertTrue( cmt.id in [qc.id for qc in QuestComment.unjudged_flagged()])
def post_quest_comment(request, quest_id, content_id, fact_metadata={}, facebook_share=False, facebook_access_token=None): # Rate-limit? if not request.user.is_staff: prefix = 'user:{}:post_limit:'.format(request.user.id) if not RateLimit(prefix + 'h', 60, 60 * 60).allowed() or not RateLimit( prefix + 'd', 100, 8 * 60 * 60).allowed(): raise ServiceError("Attempting to post drawings too quickly.") _, parent_comment, content, _, _, _ = validate_and_clean_comment( request.user, parent_comment=quest_id, reply_content=content_id, ) if facebook_share: if not facebook_access_token: raise ServiceError( "Can't share to your timeline if you haven't signed into Facebook yet." ) associate_facebook_account(request.user, facebook_access_token) comment = QuestComment.create_and_post(request, request.user, content, parent_comment, fact_metadata=fact_metadata) if facebook_share: complete_quest(request.user, comment, facebook_access_token, request=request) return { 'comment': comment.details(), 'balance': economy.balance(request.user), }
def _profile(request, user, template='profiles/profile.html'): comments = QuestComment.by_author(user) top_comments = models.top_comments(user) if top_comments is None: comments = CachedCall.queryset_details(comments) else: comments, top_comments = CachedCall.many_queryset_details(comments, top_comments) follow_counts = following_models.counts(user) return r2r_jinja(template, { 'target_user': user, 'comments': comments, 'top_comments': top_comments, 'follower_count': follow_counts['followers'], 'following_count': follow_counts['following'], }, request)
def _profile(request, user, template='profiles/profile.html'): comments = QuestComment.by_author(user) top_comments = models.top_comments(user) if top_comments is None: comments = CachedCall.queryset_details(comments) else: comments, top_comments = CachedCall.many_queryset_details( comments, top_comments) follow_counts = following_models.counts(user) return r2r_jinja( template, { 'target_user': user, 'comments': comments, 'top_comments': top_comments, 'follower_count': follow_counts['followers'], 'following_count': follow_counts['following'], }, request)
def rewards_for_posting(request, quest_id, facebook=False, tumblr=False, twitter=False): rewards = defaultdict(int) def reward(name): rewards[name] += knobs.REWARDS[name] if not request.user.is_authenticated() or QuestComment.posting_in_first_quest(request.user): reward('first_quest') ret = {'rewards': rewards} if request.user.is_authenticated(): streak, days_to_next_streak, next_streak_goal = QuestComment.posting_would_reward_streak(request.user) ret.update({ 'days_to_next_streak': days_to_next_streak, 'next_streak_goal': next_streak_goal, }) return ret quest = get_object_or_404(Quest, id=quest_id) if QuestComment.posting_would_complete_quest_of_the_day(request.user, quest): reward('quest_of_the_day') elif QuestComment.posting_would_complete_archived_quest(request.user, quest): reward('archived_quest') if QuestComment.posting_in_first_quest(request.user): reward('first_quest') if facebook: reward('personal_share') if twitter: reward('personal_twitter_share') streak, days_to_next_streak, next_streak_goal = QuestComment.posting_would_reward_streak(request.user) if streak: reward('streak_{}'.format(streak)) return { 'rewards': rewards, 'days_to_next_streak': days_to_next_streak, 'next_streak_goal': next_streak_goal, }
def quest_comments(request, quest_id, force_comment_id=None): quest = get_object_or_404(Quest, id=quest_id) # Exclude curated comments here since we ignore curation status for forced comments, below. comments = QuestComment.objects.filter(parent_comment=quest).exclude(visibility=Visibility.CURATED) comments = comments.order_by('-id') comments = comments[:knobs.COMMENTS_PER_PAGE] comments = CachedCall.queryset_details(comments) forced_comment = None if force_comment_id: for comment in comments: if str(comment.id) == str(force_comment_id): forced_comment = comment break if force_comment_id and forced_comment is None: forced_comment = QuestComment.details_by_id(force_comment_id)() if forced_comment is not None and str(forced_comment.id) not in [str(comment.id) for comment in comments]: comments.append(forced_comment) return {'comments': comments}
def distrusted(request, id_range=None): distrusted = QuestComment.by_distrusted_users().order_by('-id').exclude(flags__undone=False) ctx = _moderation_context([distrusted], id_range=id_range) return r2r_jinja('whitelisting/moderation.html', ctx, request)
def from_id(cls, comment_id): from drawquest.apps.quest_comments.models import QuestComment return QuestComment.details_by_id(comment_id)()
def new(request): ctx = { 'comments': QuestComment.unjudged().order_by('-id')[:knobs.WHITELIST_COMMENTS_PER_PAGE], } return r2r_jinja('whitelisting/new.html', ctx, request)
def flag_queue(request): ctx = { 'comments': QuestComment.unjudged_flagged().order_by('id')[:knobs.WHITELIST_COMMENTS_PER_PAGE], } return r2r_jinja('whitelisting/flagged.html', ctx, request)
def completed_by_user_count(self, user): """ The number of quests a user has completed. """ return QuestComment.by_author(user).values('parent_comment_id').distinct().count()
def from_id(cls, comment_id): from drawquest.apps.quest_comments.models import QuestComment return QuestComment.details_by_id(comment_id, promoter=cls)()
def distrusted(request, id_range=None): distrusted = QuestComment.by_distrusted_users().order_by('-id').exclude( flags__undone=False) ctx = _moderation_context([distrusted], id_range=id_range) return r2r_jinja('whitelisting/moderation.html', ctx, request)
def explore_comment_details(viewer=None): comments = CachedCall.multicall([QuestComment.details_by_id(id_, promoter=QuestCommentExploreDetails) for id_ in preloaded_explore_comment_ids()]) add_viewer_has_starred_field(comments, viewer=viewer) return comments