def create(self,request, uuid=None, *args, **kwargs): """ POST Request handler. Requires valid form defined by model of extending class. """ logging.info("create(): %s" % (request.user)) if uuid: return self.update(request,uuid=uuid) try: # Always cache the object instance = self._create(request, args, kwargs) logging.info('created object uuid=%s' % instance.uuid) # Send to any linked backends _instance = backends.create(instance,auth=auth.parse_auth(request)) # If a remote is listed as the target we # assume uuuid is set by the remote if not settings.TARGET == 'SELF': if _instance: if _instance.uuid != instance.uuid: logging.info('updating object uuid=%s' % _instance.uuid) instance.uuid = _instance.uuid else: logging.info('remote uuid is equal') else: logging.info('NoneType instance returned') # Persist object here instance.save() logging.info('POST success object.uuid=%s' % instance.uuid) return succeed(instance) except Exception, e: logging.error('ERROR') return self.trace(request, e)
def create(self, request): try: content_type = request.META.get('CONTENT_TYPE', None) logging.debug(content_type) is_json = 'json' in content_type logging.debug("is_json: %s" % is_json) if is_json: raw_data = request.read() data = cjson.decode(raw_data) else: data = self.flatten_dict(request.POST) username = data.get('username', 'empty') password = data.get('password', 'empty') if not settings.TARGET == 'SELF': instance = User(username=username) auth = {'username': username, 'password': password} result = backends.create('Session', auth, instance) if not result: return fail([], errors=[ "Observer does not exist", ], code=404) # Create a user or fetch existin and update password user, created = User.objects.get_or_create( username=result.user.username) user.set_password(password) user.save() # should have returned an Observer instance here observers = Observer.objects.filter( user__username=user.username) # If none were returned we need to create the Observer if observers.count() == 0: observer = Observer(user=user, uuid=result.uuid) observer.save() else: # Observer already exists so we don't have to do # anything since password cache is updated observer = observers[0] return succeed(observer.uuid) else: user = authenticate(username=username, password=password) if user is not None: observer = Observer.objects.get(user=user) return succeed(observer.uuid) else: msg = "Invalid credentials" logging.warn(msg) return fail(msg) except Exception as e: msg = "Internal Server Error" logging.error(unicode(e)) logtb() return error(msg)
def create(self,request): try: content_type = request.META.get('CONTENT_TYPE', None) logging.debug(content_type) is_json = 'json' in content_type logging.debug("is_json: %s" % is_json) if is_json: raw_data = request.read() data = cjson.decode(raw_data) else: data = self.flatten_dict(request.POST) username = data.get('username', 'empty') password = data.get('password','empty') if not settings.TARGET == 'SELF': instance = User(username=username) auth = {'username':username, 'password':password } result = backends.create('Session', auth, instance) if not result: return fail([],errors=["Observer does not exist",],code=404) # Create a user or fetch existin and update password user,created = User.objects.get_or_create(username=result.user.username) user.set_password(password) user.save() # should have returned an Observer instance here observers = Observer.objects.filter(user__username=user.username) # If none were returned we need to create the Observer if observers.count() == 0: observer = Observer( user=user, uuid = result.uuid) observer.save() else: # Observer already exists so we don't have to do # anything since password cache is updated observer = observers[0] return succeed(observer.uuid) else: user = authenticate(username=username, password=password) if user is not None: observer = Observer.objects.get(user=user) return succeed(observer.uuid) else: msg = "Invalid credentials" logging.warn(msg) return fail(msg) except Exception as e: msg = "Internal Server Error" logging.error(unicode(e)) logtb() return error(msg)