def send_comment_email(self, pk, retry=False): """ :param self: :param pk: :return: """ with redis.lock("tasks:send_comment_email", timeout=LOCK_TIMEOUT): from townsquare.models import Comment from marketing.mails import comment_email instance = Comment.objects.get(pk=pk) comment_email(instance) print("SENT EMAIL")
def api(request, activity_id): # pull back the obj try: activity = Activity.objects.get(pk=activity_id) except: raise Http404 # setup response response = {} # check for permissions has_perms = request.user.is_authenticated if request.POST.get('method') == 'delete': has_perms = activity.profile == request.user.profile if not has_perms: raise Http404 # deletion request if request.POST.get('method') == 'delete': activity.delete() # like request elif request.POST.get('method') == 'like': if request.POST['direction'] == 'liked': Like.objects.create(profile=request.user.profile, activity=activity) if request.POST['direction'] == 'unliked': activity.likes.filter(profile=request.user.profile).delete() # flag request elif request.POST.get('method') == 'flag': if request.POST['direction'] == 'flagged': Flag.objects.create(profile=request.user.profile, activity=activity) flag_threshold_to_hide = 3 #hides comment after 3 flags is_hidden_by_users = activity.flags.count( ) > flag_threshold_to_hide is_hidden_by_staff = activity.flags.filter( profile__user__is_staff=True).count() > 0 is_hidden = is_hidden_by_users or is_hidden_by_staff if is_hidden: activity.hidden = True activity.save() if request.POST['direction'] == 'unflagged': activity.flags.filter(profile=request.user.profile).delete() # comment request elif request.POST.get('method') == 'comment': comment = request.POST.get('comment') comment = Comment.objects.create(profile=request.user.profile, activity=activity, comment=comment) to_emails = set( activity.comments.exclude( profile=request.user.profile).values_list('profile__email', flat=True)) comment_email(comment, to_emails) elif request.GET.get('method') == 'comment': comments = activity.comments.order_by('created_on') comments = [ comment.to_standard_dict(properties=['profile_handle']) for comment in comments ] response['comments'] = comments return JsonResponse(response)