def get_or_create_conversation(self,user,id): userUtils = UserUtils() user_type = userUtils.get_user_instance_updated(user=user) if user_type[1] == 'spark_user': convo = self.create_new_conversation_by_spark_user(spark_user=user_type[0],counsellor_id=id) return convo elif user_type[1] == 'counsellor_user': print 'request is from counsellor :)' convo = self.create_new_conversation_by_consellor(counsellor=user_type[0],spark_user_id=id) return convo else: raise exceptions.PermissionDenied(detail="You don't have access to this resource via API")
def get_latest_message_per_user_converstation(self,user): userUtils = UserUtils() #print user.__class__.__name__ #method to show class name of object instance #print "We got here" user_type = userUtils.get_user_instance_updated(user=user) if user_type[1] is 'spark_user': latest_messages = LastConvoMessage.objects.filter(relation__user_instance=user_type[0]).order_by('-created_at')#order from earliest to latest return latest_messages elif user_type[1] is 'counsellor_user': latest_messages = LastConvoMessage.objects.filter(relation__counsellor_instance=user_type[0]).order_by('-created_at') return latest_messages else: raise exceptions.PermissionDenied(detail="You don't have the required permission to view this resource via API.")
def does_user_belong_to_convo(self,user,id): try: userUtils = UserUtils() user_type = userUtils.get_user_instance_updated(user=user) if user_type[1] == 'spark_user': convo = self.viewset.get(user_instance=user_type[0],pk=id) return convo elif user_type[1] == 'counsellor_user': convo = self.viewset.get(counsellor_instance=user_type[0],pk=id) return convo else: raise exceptions.PermissionDenied(detail="Yo do not have permission to view this resource via API.") except Conversation.DoesNotExist as e: raise exceptions.NotFound(detail="Conversation with provided id not found.")
def add_new_forum_report(self,user,data,id): print 'We got to add_new_forum_report' forumService = ForumService() forum = forumService.does_forum_exist(id=id) if forum is not None: userUtils = UserUtils() user_type = userUtils.get_user_instance_updated(user=user) if user_type[1] == 'spark_user': new_forum_report = ForumReport.objects.create( relation = user_type[0], forum_relation = forum, message = data['message'] ) #new_forum_report.save() return new_forum_report else: raise exceptions.PermissionDenied(detail="You do not have permission to your request via API.") else: raise exceptions.NotFound(detail="Forum id supplied was not found.")
def add_new_forum_request(self,user,data): userUtils = UserUtils() user_type = userUtils.get_user_instance_updated(user=user) if user_type[1] == 'spark_user': forumService = ForumService() serializer_class = self.get_serializer() serialized_data = serializer_class(data=data) if serialized_data.is_valid(): new_forum_request = ForumRequest.objects.create( relation = user_type[0], forum_name = data['forum_name'], message = data['message'] ) #new_forum_request.save() return new_forum_request else: raise exceptions.ParseError(detail=serialized_data.data) else: raise exceptions.PermissionDenied(detail="You cannot view this resource via API.")