def perform_create(self, serializer): request = self.request is_my_user = int(request.data['user']) == request.user.id # If is my user or is superuser can create if is_my_user or request.user.is_superuser: # Save the record comment if serializer.is_valid(): comment = serializer.save() else: return Response( serializer.errors, status=status.HTTP_400_BAD_REQUEST ) topic_id = request.data['topic'] topic = get_object_or_404(models.Topic, pk=topic_id) # Parameters for notification comments photo = utils.get_photo_profile(request.user.id) username = request.user.username forum = topic.forum.name # Send notifications comment params = utils.get_users_and_send_notification_comment( request, topic, comment ) list_us = params['list_us'] list_email = params['list_email'] # Get url for email url = reverse_lazy('topic', kwargs={ 'category': topic.forum.category, 'forum': forum, 'slug': topic.slug, 'idtopic': str(topic.idtopic) }) # Send e mail utils.send_mail_comment(str(url), list_email) # Data necessary for realtime data = realtime.data_base_realtime(topic, photo, forum, username) data['is_topic'] = False data['is_comment'] = True # Send new notification realtime realtime.new_notification(data, list_us) # Send new comment in realtime comment_description = request.data['description'] realtime.new_comment(data, comment_description) return Response( serializer.data, status=status.HTTP_201_CREATED ) else: raise PermissionDenied({ "message": "Not your user" })
def perform_create(self, serializer): request = self.request is_my_user = int(request.data['user']) == request.user.id # If is my user or is superuser can create if is_my_user or request.user.is_superuser: # Save the record comment if serializer.is_valid(): comment = serializer.save() else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) topic_id = request.data['topic'] topic = get_object_or_404(models.Topic, pk=topic_id) # Parameters for notification comments photo = utils.get_photo_profile(request.user.id) username = request.user.username forum = topic.forum.name # Send notifications comment params = utils.get_users_and_send_notification_comment( request, topic, comment) list_us = params['list_us'] list_email = params['list_email'] # Get url for email url = reverse_lazy('topic', kwargs={ 'category': topic.forum.category, 'forum': forum, 'slug': topic.slug, 'idtopic': str(topic.idtopic) }) # Send e mail utils.send_mail_comment(str(url), list_email) # Data necessary for realtime data = realtime.data_base_realtime(topic, photo, forum, username) data['is_topic'] = False data['is_comment'] = True # Send new notification realtime realtime.new_notification(data, list_us) # Send new comment in realtime comment_description = request.data['description'] realtime.new_comment(data, comment_description) return Response(serializer.data, status=status.HTTP_201_CREATED) else: raise PermissionDenied({"message": "Not your user"})
def post(self, request, category, forum, slug, idtopic, *args, **kwargs): # Form new comment form = forms.FormAddComment(request.POST) url = reverse_lazy('topic', kwargs={ 'category': category, 'forum': forum, 'slug': slug, 'idtopic': str(idtopic) }) if form.is_valid(): obj = form.save(commit=False) # Save new comment now = timezone.now() User = get_user_model() user = User.objects.get(id=request.user.id) topic = get_object_or_404(models.Topic, idtopic=idtopic) obj.date = now obj.user = user obj.topic_id = topic.idtopic obj.save() # Update last activity TopicSearch models.Topic.objects.filter(idtopic=idtopic).update( last_activity=now) # Data for notification real time idcomment = obj.idcomment comment = models.Comment.objects.get(idcomment=idcomment) username = request.user.username # Get photo profile photo = utils.get_photo_profile(request.user.id) # Send notifications comment params = utils.get_users_and_send_notification_comment( request, topic, comment) list_us = params['list_us'] list_email = params['list_email'] # Send email form.send_mail_comment(str(url), list_email) # Data necessary for realtime data = realtime.data_base_realtime(comment.topic, photo, forum, username) data['is_topic'] = False data['is_comment'] = True # Send new notification realtime realtime.new_notification(data, list_us) # Send new comment in realtime realtime.new_comment(data, comment.description) messages.success(request, _("Added new comment")) return HttpResponseRedirect(url) else: messages.error(request, _("Field required")) return HttpResponseRedirect(url)
def post(self, request, category, forum, slug, idtopic, *args, **kwargs): # Form new comment form = forms.FormAddComment(request.POST) url = reverse_lazy('topic', kwargs={ 'category': category, 'forum': forum, 'slug': slug, 'idtopic': str(idtopic) }) if form.is_valid(): obj = form.save(commit=False) # Save new comment now = timezone.now() User = get_user_model() user = User.objects.get(id=request.user.id) topic = get_object_or_404(models.Topic, idtopic=idtopic) obj.date = now obj.user = user obj.topic_id = topic.idtopic obj.save() # Update last activity TopicSearch models.Topic.objects.filter( idtopic=idtopic ).update(last_activity=now) # Data for notification real time idcomment = obj.idcomment comment = models.Comment.objects.get(idcomment=idcomment) username = request.user.username # Get photo profile photo = utils.get_photo_profile(request.user.id) # Send notifications comment params = utils.get_users_and_send_notification_comment( request, topic, comment ) list_us = params['list_us'] list_email = params['list_email'] # Send email form.send_mail_comment(str(url), list_email) # Data necessary for realtime data = realtime.data_base_realtime( comment.topic, photo, forum, username ) data['is_topic'] = False data['is_comment'] = True # Send new notification realtime realtime.new_notification(data, list_us) # Send new comment in realtime realtime.new_comment(data, comment.description) messages.success(request, _("Added new comment")) return HttpResponseRedirect(url) else: messages.error(request, _("Field required")) return HttpResponseRedirect(url)