def save_model(self, request, obj, form, change): instance = form.save(commit=False) is_moderate = False forum = obj.forum forum_id = obj.forum_id if not change: is_moderate = utils.check_topic_moderate(request.user, forum) instance.is_moderate = is_moderate instance.save() # Check if is create and is_moderate topic if not change and is_moderate: domain = utils.get_domain(request) # Send email to moderators nt_email.send_notification_topic_to_moderators(forum, obj, domain) # Get moderators forum and send notification list_us = nt.get_moderators_and_send_notification_topic( request, forum, obj) # Data necessary for realtime data = realtime.data_base_realtime(obj, forum, True) # Send new notification realtime realtime.new_notification(data, list_us) # Send new topic in forum realtime.new_topic_forum(forum_id, data)
def save_model(self, request, obj, form, change): obj.save() topic = obj.topic # If is create if not change: # Send notifications comment params = nt.get_users_and_send_notification_comment( request, topic, obj) list_us = params['list_us'] list_email = params['list_email'] # Get url topic for email url = utils.get_url_topic(request, topic) # Send email nt_email.send_mail_comment(str(url), list_email) # Data necessary for realtime data = realtime.data_base_realtime(obj, topic.forum, False) # Send new notification realtime realtime.new_notification(data, list_us) # Send new comment in realtime comment_description = obj.description realtime.new_comment(data, comment_description)
def perform_create(self, serializer): request = self.request forum_id = int(request.data['forum']['id']) user_id = int(request.data['user']['id']) # If is my user or is superuser can create if user_id == request.user.id or request.user.is_superuser: forum = get_object_or_404(models.Forum, pk=forum_id) user = get_object_or_404(get_user_model(), pk=request.user.id) category = forum.category.name # If has permissions if utils.user_can_create_topic(category, forum, request.user): # Save the record topic if serializer.is_valid(): # Check if moderation topic is_moderate = utils.check_topic_moderate( request.user, forum) # If the forum is moderate send email serializer = utils.check_moderate_topic_email( request, forum, serializer) # Save record topic = serializer.save(forum=forum, user=user, is_moderate=is_moderate) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) # If is moderate send notifications if is_moderate: domain = utils.get_domain(request) # Send email to moderators nt_email.send_notification_topic_to_moderators( forum, topic, domain) # Get moderators forum and send notification list_us = nt.get_moderators_and_send_notification_topic( request, forum, topic) # Data necessary for realtime data = realtime.data_base_realtime(topic, forum, True) # Send new notification realtime realtime.new_notification(data, list_us) # Send new topic in forum realtime.new_topic_forum(forum_id, data) return Response(serializer.data, status=status.HTTP_201_CREATED) else: raise PermissionDenied( {"message": "You don't have permission to access"}) else: raise PermissionDenied({"message": "The user is invalid"})
def perform_create(self, serializer): request = self.request # Get user id user_id = request.data['user']['id'] # Get topic topic_id = request.data['topic']['id'] topic = get_object_or_404(models.Topic, pk=topic_id) is_my_user = int(user_id) == 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( topic=topic, user=request.user ) else: return Response( serializer.errors, status=status.HTTP_400_BAD_REQUEST ) # Send notifications comment params = nt.get_users_and_send_notification_comment( request, topic, comment ) list_us = params['list_us'] list_email = params['list_email'] # Get url for email url = url = utils.get_url_topic(request, topic) # Send email nt_email.send_mail_comment(str(url), list_email) # Data necessary for realtime data = realtime.data_base_realtime( comment, topic.forum, False ) # 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": "Error: User incorrect." })