def get(self, request): # get patient instance patient = get_model_object(models.Patient, {'user': request.user}) if patient: # get all the groups that the patient has subscribed groups = patient.groups.values('disease_name') groups = [x.get('disease_name') for x in groups] doctor = get_model_object(models.Doctor, {'user': request.user}) if doctor: groups = doctor.groups.values('disease_name') groups = [x.get('disease_name') for x in groups] # get all the notifications where initated_by != request.user notifications = models.Notifications.objects.exclude( initiated_by=request.user) # check for notification groups notifications = notifications.filter( post__group__disease_name__in=groups).order_by('-date') if doctor: notifications = notifications.filter( notification_type='experience') serializer = self.serializer_class(instance=notifications, many=True) return Response(serializer.data)
def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): # now we need to get the disease/group object group = get_model_object( models.Groups, {'slug': serializer.validated_data.get('slug')}) if group: # get patient instance patient = get_model_object(models.Patient, {'user': request.user}) if patient: # add the patient to that particular group patient.groups.add(group) return Response( data={ 'success': { 'id': group.id, 'disease_name': group.disease_name, 'slug': group.slug } }) return Response(data={'error': 'Patient not found'}, status=status.HTTP_400_BAD_REQUEST) return Response(data={'error': 'Not found'}, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors)
def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): user = authenticate( email=serializer.validated_data.get('email'), password=serializer.validated_data.get('password')) status = serializer.validated_data.get('status') if user: # grab token token, _ = Token.objects.get_or_create(user=user) if status == 'patient': instance = get_model_object(models.Patient, {'user': user}) if instance: return Response({'token': token.key}) elif status == 'doctor': instance = get_model_object(models.Doctor, {'user': user}) if instance: return Response({'token': token.key}) elif status == 'admin': if user.is_superuser: return Response({'token': token.key}) elif status == 'business': instance = get_model_object(models.Business, {'user': user}) if instance: return Response({'token': token.key}) return Response({'error': 'Account not found'}, status=400) return Response(serializer.errors)
def get(self, request, listing_id=None): business = get_model_object(models.Business, {'user': request.user}) if listing_id: listing = get_model_object(models.Listings, {'id': listing_id}) if listing: serializer = self.serializer_class(instance=listing) return Response(serializer.data) else: return Response({'error': 'listing not found'}) elif business: queryset = business.listings_set.all() serializer = self.serializer_class(instance=queryset, many=True) return Response(serializer.data) return Response({'error': 'business account not found'})
def get(self, request): doctor = get_model_object(models.Doctor, {'user': request.user}) if doctor: groups = doctor.groups.all() groups = [x.disease_name for x in groups] # get all posts of type question and present in same group as dr. questions = models.Posts.objects.filter( post_type='question', group__disease_name__in=groups) # now check whether the dr. has previously commented temp = [] for each in questions: if each.comments_set.filter( commented_by=request.user).exists(): temp.append(each) print(temp) notifications = models.Notifications.objects.exclude( post__in=temp).filter(notification_type='question') serializer = self.serializer_class(instance=notifications, many=True) return Response(serializer.data) return Response({'error': 'Not doctor account'}, status=400)
def get(self, request, slug): # get the requested dieases disease = get_model_object(models.Groups, {'slug': slug}) if disease: serializer = self.serializer_class(instance=disease) return Response(serializer.data) return Response({'error': 'Not fount'}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request): # preparing queryset patient = get_model_object(models.Patient, {'user': request.user}) if patient: serialized = api_ser.ShowAllGroupsSerializer(patient.groups.all(), many=True) return Response({ 'username': f"{request.user.first_name} {request.user.last_name}", 'groups': serialized.data, 'settings': { 'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email } }) doctor = get_model_object(models.Doctor, {'user': request.user}) if doctor: groups = doctor.groups.all() serializer = api_ser.ShowAllGroupsSerializer(groups, many=True) return Response({ 'username': f"{request.user.first_name} {request.user.last_name}", 'groups': serializer.data, 'settings': { 'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email } }) business = get_model_object(models.Business, {'user': request.user}) if business: return Response({ 'username': f"{request.user.first_name} {request.user.last_name}", 'settings': { 'first_name': request.user.first_name, 'last_name': request.user.last_name, 'email': request.user.email } })
def get(self, request, option="overall", group=None): doctor = get_model_object(models.Doctor, {'user': request.user}) if doctor: if option == 'overall': temp = dict() current_date = timezone.now() for each in range(10, -1, -1): date = current_date - timedelta(days=each) # counting the number of questions answered by doctor comments = models.Comments.objects.filter( date__date=date, commented_by=request.user, post__post_type='question') temp[self.date_format(date)] = comments.count() return Response(temp) elif option == 'distribution': # get groups of doctor groups = [x.disease_name for x in doctor.groups.all()] # count the number of patients in each group temp = {} for each in groups: temp[each] = models.Patient.objects.filter( groups__disease_name=each).count() return Response(temp) elif option == 'groupwise' and group: temp = dict() current_date = timezone.now() for each in range(10, -1, -1): date = current_date - timedelta(days=each) # counting the number of questions answered by doctor comments = models.Comments.objects.filter( date__date=date, commented_by=request.user, post__post_type='question', post__group__disease_name__iexact=group, ) temp[self.date_format(date)] = comments.count() return Response(temp) else: return Response({'error': 'Invalid choice'}, status=400) return Response({'error': 'Doctor account invalid'}, status=400)
def post(self, request): serializer = self.serializer_class(data=request.data) if serializer.is_valid(): # create customuser instance custom_user = models.CustomUser.objects.create_user( first_name='Dr.' + serializer.validated_data.get('first_name'), last_name=serializer.validated_data.get('last_name'), mobile=serializer.validated_data.get('phone'), email=serializer.validated_data.get('email'), username=serializer.validated_data.get('email'), password=serializer.validated_data.get('password'), is_active=False) # get specialization instance specialization = get_model_object( models.Specialization, { 'specialization_name__iexact': serializer.validated_data.get('specialization') }) if specialization: # creating doctor instance doctor = models.Doctor.objects.create( user=custom_user, specialization=specialization, qualification_certificate=serializer.validated_data.get( 'qualification_certificate'), address=serializer.validated_data.get('address')) # create token token = Token.objects.create(user=custom_user) return Response( data={'success': 'doctor created successfully'}, status=status.HTTP_201_CREATED) # delete instance if something goes wrong custom_user.delete() return Response({'error': 'Specialization not found'}, status=status.HTTP_400_BAD_REQUEST) return Response(serializer.errors)
def get(self, request, post_id): # get Post instance post = get_model_object(models.Posts, {'id': post_id}) serializer = self.serializer_class(instance=post) return Response(serializer.data)