def register_pill_by_name(request): '''This method is for the autosuggestion used in manual lookup. This method is for registering pills using the name, not the id''' if request.method == 'POST': if request.user.is_authenticated: try: req_data = json.loads(request.body.decode()) except (KeyError, ValueError): return HttpResponseBadRequest() pill_name = req_data['pill_name'] pill_company = req_data['pill_company'] new_pill = Pill.objects.get(product_name=pill_name, company_name=pill_company, custom=False) existing_pill_names = request.user.pills.all().values_list( 'product_name', flat=True) if pill_name in existing_pill_names: return HttpResponse(status=status.HTTP_400_BAD_REQUEST) request.user.pills.add(new_pill) Notification.create(request.user, new_pill) new_pill_dict = get_pill_dict(new_pill) return JsonResponse(new_pill_dict, status=status.HTTP_201_CREATED) else: return HttpResponse(status=401) else: return HttpResponseNotAllowed(['POST'])
def post(self, request, pill_id): """ add new pill item for user <int:pk> """ if request.user.is_authenticated: # check if pill_id already exists in user's pills existing_pills = request.user.pills.all().values_list('id', flat=True) if pill_id in existing_pills: return HttpResponse(status=status.HTTP_400_BAD_REQUEST) # get pill object from Pill model by id new_pill = Pill.objects.get(pk=pill_id) # add retrieved pill object to current user's pills field request.user.pills.add(new_pill) # add notification for the new pill Notification.create(request.user, new_pill) image_query = Image.objects.filter(user=request.user, pill=new_pill) if image_query.exists(): image_instance = image_query[0] new_pill_dict = get_pill_dict(new_pill, image_instance) else: new_pill_dict = get_pill_dict(new_pill) # new_pill_dict = get_pill_dict(new_pill) return JsonResponse(new_pill_dict, status=status.HTTP_201_CREATED) else: return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
def custompill_post(request): """ add new pill item for user <int:pk> """ if request.user.is_authenticated: # check if pill_id already exists in user's pills try: pill_data = json.loads(request.body.decode()) print(pill_data) take_method = pill_data['take_method'] product_name = pill_data['product_name'] expiration_date = pill_data['expiration_date'] functions = pill_data['functions'] print('error1') store_method = pill_data['store_method'] company_name = pill_data['company_name'] standards = pill_data['standards'] precautions = pill_data['precautions'] take_method_preprocessed = pill_data['take_method_preprocessed'] image_id = pill_data['image_id'] print('error') except (KeyError, ValueError): return HttpResponseBadRequest() existing_pills = request.user.pills.all().values_list('product_name', flat=True) if product_name in existing_pills: return HttpResponse(status=status.HTTP_400_BAD_REQUEST) print(pill_data) # get pill object from Pill model by id new_pill = CustomPill.objects.create( user=request.user, take_method=take_method, product_name=product_name, expiration_date=expiration_date, functions=functions, store_method=store_method, company_name=company_name, standards=standards, precautions=precautions, take_method_preprocessed=take_method_preprocessed, custom=True, ) # Assign pill to image image_instance = Image.objects.get(id=image_id) image_instance.pill = new_pill image_instance.user = request.user image_instance.save() # add retrieved pill object to current user's pills field request.user.pills.add(new_pill) # add notification for the new pill Notification.create(request.user, new_pill) return JsonResponse(get_pill_dict(new_pill), status=status.HTTP_201_CREATED) else: return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
def post(self, request): notification = Notification.objects.filter(expo_token=str(request.data['expo_token'])) if notification: return Response(status=status.HTTP_200_OK) else: Notification.create( user=request.user, token=str(request.data['expo_token']) ) return Response(status=status.HTTP_200_OK)
def setUp(self): management.call_command('loaddata', 'dataset/fixtures/pill_data.json') self.client = Client() new_user = User.objects.create_user(email="*****@*****.**", password="******", name="test1") new_notisetting = NotiSetting(user=new_user) new_notisetting.save() self.client.login(email="*****@*****.**", password="******") # get pill object from Pill model by id new_pill = Pill.objects.get(pk=1) # add retrieved pill object to current user's pills field new_user.pills.add(new_pill) new_notification = Notification.create(new_user, new_pill) new_image = Image.objects.create(filename="file", user=new_user, pill=new_pill) new_image.save() print(f'setup all images view{Image.objects.all()}') self.maxDiff = None