def post(self, request, *args, **kwargs): if not request.user.is_authenticated: return JsonResponse({'Status': False, 'Error': 'Log in required'}, status=403) if {'city', 'street', 'phone'}.issubset(request.data): request.data._mutable = True request.data.update({'user': request.user.id}) serializer = ContactSerializer(data=request.data) if serializer.is_valid(): serializer.save() return JsonResponse({'Status': True}) else: JsonResponse({'Status': False, 'Errors': serializer.errors}) return JsonResponse({'Status': False, 'Errors': 'Не указаны все необходимые аргументы'})
def put(self, request, *args, **kwargs): if not request.user.is_authenticated: return JsonResponse({'Status': False, 'Error': 'Log in required'}, status=403) if 'id' in request.data: if request.data['id'].isdigit(): contact = Contact.objects.filter(id=request.data['id'], user_id=request.user.id).first() if contact: serializer = ContactSerializer(contact, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return JsonResponse({'Status': True}) else: return JsonResponse({'Status': contact, 'Error': 'Не указаны все необходимые аргументы'}) return JsonResponse({'Status': False, 'Errors': 'Не указаны все необходимые аргументы'})
def delete(self): toDelete = Contact.query.filter(Contact.is_selected == True).all() for contact in toDelete: db.session.delete(contact) db.session.commit() contacts = Contact.query.all() return ContactSerializer(contacts, many=True).data, 201
def get(self, request, *args, **kwargs): if not request.user.is_authenticated: return JsonResponse({'Status': False, 'Error': 'Log in required'}, status=403) contact = Contact.objects.filter( user_id=request.user.id) serializer = ContactSerializer(contact, many=True) return Response(serializer.data)
def post(self): form = ContactCreateForm() if not form.validate_on_submit(): return form.errors, 422 contact = Contact(form.text.data, form.first_name.data, form.last_name.data, form.is_selected.data) db.session.add(contact) db.session.commit() return ContactSerializer(contact).data, 201
def put(self, id): form = ContactUpdateForm() if not form.validate_on_submit(): return form.errors, 422 contact = Contact.query.filter_by(id=id).first() contact.first_name = form.first_name.data contact.last_name = form.last_name.data contact.text = form.text.data db.session.commit() return ContactSerializer(contact).data, 201
def delete(self, id): contact = Contact.query.filter_by(id=id).first() db.session.delete(contact) db.session.commit() contacts = Contact.query.all() return ContactSerializer(contacts, many=True).data
def get(self, id): contacts = Contact.query.filter_by(id=id).first() return ContactSerializer(contacts).data
def get(self): contacts = Contact.query.all() return ContactSerializer(contacts, many=True).data