def inner(*args, **kwargs): from webservice_tools.response_util import ResponseObject response = ResponseObject() try: request = [a for a in args if hasattr(a, 'user')][0] except IndexError: return response.send(errors="Login required method called without request object", status=500) if request.user.is_authenticated(): return fn(*args, **kwargs) return response.send(errors='401 -- Unauthorized', status=401)
def changePass(request): response = ResponseObject() oldPass = request.POST.get('oldPassword') newPass1 = request.POST.get('newPassword1') newPass2 = request.POST.get('newPassword2') if not newPass1 == newPass2 and request.user.check_password(oldPass): response.addErrors("Your old password was not entered correctly or your new passwords don't match. Please try again") return response.send() request.user.set_password(newPass1) request.user.save() return response.send()
def generic_exception_handler(request, exception): from webservice_tools.response_util import ResponseObject from django.db import transaction response = ResponseObject() _, _, tb = sys.exc_info() # we just want the last frame, (the one the exception was thrown from) lastframe = get_traceback_frames(tb)[-1] location = "%s in %s, line: %s" % (lastframe["filename"], lastframe["function"], lastframe["lineno"]) response.addErrors([exception.message, location]) logger = logging.getLogger("webservice") logger.debug([exception.message, location]) if transaction.is_dirty(): transaction.rollback() return HttpResponse(simplejson.dumps(response.send()._container), status=500)
def read(self, request, network, response=None): """ This is the entrypoint for social network's callbacks """ if not response: response = ResponseObject() try: network = SocialNetwork.objects.get(name=network) except SocialNetwork.DoesNotExist: return response.send(errors='Invalid network', status=404) #Use the name of the network to call the helper function if request.session.get('last_url'): getattr(self, network.name)(request, network, response) return HttpResponseRedirect(request.session.get('last_url')) return getattr(self, network.name)(request, network, response)
def inner(*args, **kwargs): from webservice_tools.response_util import ResponseObject response = ResponseObject() try: request = [a for a in args if hasattr(a, 'user')][0] except IndexError: return response.send(errors="Data delete decorated function called without request object", status=400) if request.method == 'DELETE': request.DELETE = {} if request.raw_post_data: request.DELETE = QueryDict(request.raw_post_data) t_args = [a for a in args] t_args[t_args.index(request)] = request return fn(*t_args, **kwargs) if not request.DELETE and request.GET: request.DELETE = request.GET return fn(*args, **kwargs)
def create(self, request, network, response=None): """ Attempts to gain permission to a user's data with a social network, if successful, will return a redirect to the network's servers, there the user will be prompted to login if necessary, and allow or deny us access. network = {facebook|twitter|linkedin|latitude|gowalla|foursquare} API handler: POST /social/register/{network} Params: None """ if request.META.get('HTTP_REFERER') and not 'social/test' in request.META.get('HTTP_REFERER'): request.session['last_url'] = request.META['HTTP_REFERER'] if not response: response = ResponseObject() try: network = SocialNetwork.objects.get(name=network) except SocialNetwork.DoesNotExist: return response.send(errors='Invalid network', status=404) #return the results of the helper function that has the name of the network referenced return getattr(self, network.name)(request, network, response)
def read(self, request, response=None): """ Geocode an address or reverse geocode a lat/lng pair API Handler: GET /services/geo GET Params: @address [string] the address you'd like to geocode @lat [latitude] the latitude you'd like to reverse geocode, required if address is not supplied @lng [longitude] the longitude you'd like to reverse geocode, required if address is not supplied """ if not response: response = ResponseObject() address = request.GET.get('address') lat = request.GET.get('lat') lng = request.GET.get('lng') if address: get_coords = strToBool(request.GET.get('get_coords', 'True')) if hasattr(settings, 'GOOGLE_API_KEY'): geo_code = GeoCode(address) else: #just use the api key in the utils module geo_code = GeoCode(address) if get_coords: try: response.set(result=geo_code.getCoords()) except: return response.send(errors='Invalid Address') else: result = geo_code.getResponse() if int(result['Status']['code']) == 200: response.set(result=geo_code.getResponse()) else: return response.send(errors="Invalid Address") elif (lat and lng): address = ReverseGeoCode(latlng='%s,%s' % (lat, lng)).getAddress() response.set(address=address) else: return response.send(errors="Please provide a lat/lng pair or address") return response.send()