def post(self, request, *args, **kwargs): zipcode = request.data.get('zipcode') if not zipcode: zipcode = request.data.get('zip_code') if not zipcode: return Http400(_('zipcode not sent')) form = ZipCodeForm({'zipcode': zipcode}) if not form.is_valid(): return Http400(_('invalid data'), errors=form.errors) response = requests.get( '{}{}'.format( settings.ZIPCODE_API_URL, form.cleaned_data['zipcode'] ) ) if response.status_code == 404: return Http404(_(u'zipcode not found')) response = response.json() obj = Address.objects.create( zipcode=response['cep'], address=response.get('logradouro'), # nullable neighborhood=response.get('bairro'), # nullable state=response['estado'], city=response['cidade'], address2=response.get('complemento'), # nullable ) return Http201(self.serialize(obj))
def iOSLoginBAWF(request): if request.method == "POST": print 'abcd' print request.POST print request.GET print request username = request.POST.get('username') password = request.POST.get('password') if not username and not password: print "response inside if" return Http400("failed") print 'ca', username print 'ca', password user_auth = authenticate(username=username, password=password) try: print 'auth', user_auth user_auth.backend = 'django.contrib.auth.backends.ModelBackend' auth_login(request, user_auth) print 'auth' if not user_auth.is_staff: return Http400("failed") token = Token.objects.get_or_create(user=user_auth)[0] # return HttpResponse(json.dumps({ # 'key':token.key, # })) return Http200(serialize({ 'key': token.key, })) except Exception as e: print e return Http400("failed")
def errors_or_paygov(self, account, data, host): """Return a 400 or the paygov data""" amount_form = DonationAmountForm(data, account=account) if not amount_form.is_valid(): errors = self.serialize_errors(amount_form) return Http400("validation error", error_form="amount", errors=errors) donorinfo_form = DonationPaymentForm(data) if not donorinfo_form.is_valid(): errors = self.serialize_errors(donorinfo_form) return Http400("validation error", error_form="donorinfo", errors=errors) # convert to cents payment_amount = int(amount_form.cleaned_data['payment_amount'] * 100) payment_data = dict(donorinfo_form.cleaned_data) payment_data['payment_amount'] = payment_amount payment_data['project_code'] = account.code paygov = convert_to_paygov(payment_data, account, "https://" + host) paygov.save() return { "agency_id": settings.PAY_GOV_AGENCY_ID, "agency_tracking_id": paygov.agency_tracking_id, "app_name": settings.PAY_GOV_APP_NAME, "oci_servlet_url": settings.PAY_GOV_OCI_URL, }
def post(self, request): form = AuthorForm(request.data) if form.is_valid(): author = form.save() return Http201(serialize(author)) else: return Http400(reason='invalid author data', details=form.errors)
def post(self, request): user = None username = request.data.get('username') or request.data.get('email') password = request.data.get('password') if not (username and password): return Http400( "Both a username and a password field are required for authentication." ) user = auth.authenticate(username=username, password=password) if not user: return Http400( "The username and password combination that you provided is incorrect." ) return JSONResponse({'uid': user.id, 'token': user.fresh_token})
def put(self, request, author_id=None): author_id = int(author_id) try: author = Author.objects.get(id=author_id) except Author.DoesNotExist: return Http404(reason='no such author') form = AuthorForm(request.data, instance=author) if form.is_valid(): author = form.save() return serialize(author) else: return Http400(reason='invalid author data', details=form.errors)
def get_query_set(self, request, *args, **kwargs): qs = super(AddressListView, self).get_query_set(request, *args, **kwargs) # TODO implement a start limit = request.params.get('limit') if limit: try: limit = int(limit) except ValueError: return Http400(_('invalid limit')) else: qs = qs[:limit] return qs
def post(self, request): if not request.user.is_authenticated(): return Http403("You must log in to modify your user resource.") user = request.user form = UpdateUserForm(request.data, instance=user) if form.is_valid(): user = form.save() return UserPresenter(user).serialized return Http400("Derp derp.", details=form.errors)
def get(self, request, access_key): identifier = request.user.team.identifier url = "%s/api/keys/%s/%s" % ( KEYMASTER_HOST, identifier, access_key, ) r = requests.get(url) if r.status_code in [400, 404]: return Http400({"error": "Could not fetch key information"}) return r.json()
def delete(self, request, access_key): identifier = request.user.team.identifier url = "%s/api/keys/%s/%s" % ( KEYMASTER_HOST, identifier, access_key, ) r = requests.delete(url) if r.status_code == 400: return Http400({"error": "Key could not be removed."}) return "Key Successfully Removed"
def post(self, request): identifier = request.user.team.identifier url = "%s/api/keys/%s" % ( KEYMASTER_HOST, identifier, ) r = requests.post(url, data=request.body) if r.status_code == 400: return Http400({"error": "Key could not be added"}) return r.json()
def post(self, request): if request.user.is_authenticated(): return Http403( "You cannot create a new user while you're logged-in.") form = UpdateUserForm(request.data) if form.is_valid(): user = form.save() response = UserPresenter(user).serialized response['token'] = user.fresh_token return Http201(response) return Http400(reason="The user could not be created.", details=form.errors)