def post(self, request, *args, **kwargs): # contType = "content_type='application/json'" # Parse data from JSON try: data = json.loads(request.body.decode("utf-8")) except: logger.warning(GeneralTools.getExceptionInfo(sys.exc_info())) return ApiTools.HttpJsonReponseBadRequest(GeneralTools.getExceptionInfo(sys.exc_info())) username = data.get('user','') if(username == ''): return ApiTools.HttpJsonReponseMissingParameter('A username must be supplied') password = data.get('password','') if(password == ''): # return ApiTools.HttpJsonReponse('422', 'A password must be supplied') return ApiTools.HttpJsonReponseMissingParameter('A password must be supplied') email = data.get('email','') if(email == ''): return ApiTools.HttpJsonReponseMissingParameter('An email must be supplied') # Attempt to create and save the new user object # This method automatically hashes the password try: user = User.objects.create_user(username=username,password=password,email=email) except IntegrityError as e: return ApiTools.HttpJsonReponseBadRequest('Username is already taken!') except: return ApiTools.HttpJsonReponseBadRequest(GeneralTools.getExceptionInfo(sys.exc_info())) # Create new user profile profile = Profile( user=user, age = data.get('age',''), location = data.get('location',''), yearsExperience = data.get('yearsExperience',''), avatarURL = data.get('avatarURL',''), reg_date=timezone.now(), update_date=timezone.now() ) errorMsg = CreateUserView.validateProfile(profile) if errorMsg != '': return ApiTools.HttpJsonReponseBadRequest(errorMsg) try: profile.save() except: return ApiTools.HttpJsonReponseBadRequest('Profile failed to create with error: ' + GeneralTools.getExceptionInfo(sys.exc_info())) return ApiTools.HttpJsonReponse(serializers.serialize('json', Profile.objects.filter(id=profile.id)))
def post(self, request, *args, **kwargs): # contType = "content_type='application/json'" # Make sure we're dealing with AJAX request if not request.is_ajax(): return HttpJsonReponseBadRequest('Expected an XMLHttpRequest') # Parse data from JSON try: data = json.loads(request.body.decode("utf-8")) except: logger.warning(GeneralTools.getExceptionInfo(sys.exc_info())) return ApiTools.HttpJsonReponseBadRequest(GeneralTools.getExceptionInfo(sys.exc_info())) logger.warning(data) # return ApiTools.HttpJsonReponse('500', request.body); username = data.get('user','') if(username == ''): return ApiTools.HttpJsonReponseMissingParameter('A username must be supplied') #how are we passing password? I know not flattext but I wonder how we handle logging in here. password = data.get('password','') if(password == ''): return ApiTools.HttpJsonReponseMissingParameter('A password must be supplied') user = authenticate(username=username, password=password) # If username/password combo is invalid it returns 'None' if user is not None: if user.is_active: #do we want to redirect to another page here or just return a session and have the UI handle it? login(request, user) s = SessionStore() s['last_login'] = DateTools.getNowAsString() s['userid'] = user.username s.save() return ApiTools.HttpJsonReponse('Login successful!') else: # Will we ever have inactive users? return ApiTools.HttpJsonReponseUnauthorized('Inactive user') else: return ApiTools.HttpJsonReponseUnauthorized('Invalid username or password')
def post(self, request, *args, **kwargs): # contType = "content_type='application/json'" # Parse data from JSON try: data = json.loads(request.body.decode("utf-8")) except: logger.warning(GeneralTools.getExceptionInfo(sys.exc_info())) return ApiTools.HttpJsonReponseBadRequest(GeneralTools.getExceptionInfo(sys.exc_info())) useridi = data.get('userid','') styleidi = data.get('styleid','') substyleidi = data.get('substyleid','') descriptioni = data.get('description','') boilTimei = data.get('boilTime','') unitsi = data.get('units','') methodi = data.get('method','') batchSizei = data.get('batchSize','') boilSizei = data.get('boilSize','') #id = data.get('id','') will be supplied abvi = data.get('abv','') ibui = data.get('ibu','') namei = data.get('name','') user_idi = request.user.id; if(user_idi == None): user_idi = 1; # for testing, should throw an exception here if(namei == ''): return ApiTools.HttpJsonReponseMissingParameter('A name must be supplied') recipeToSave = Recipe(userid=useridi, styleid=styleidi, substyleid=substyleidi, description=descriptioni, boilTime=boilTimei, units=unitsi, method=methodi, batchSize=batchSizei, boilSize=boilSizei, abv=abvi, ibu=ibui, name=namei, user_id=user_idi, pub_date=timezone.now()); errorMsg = RecipeView.validateRecipe(recipeToSave) if errorMsg != '': return ApiTools.HttpJsonReponseBadRequest(errorMsg) try: recipeToSave.save(); except: return ApiTools.HttpJsonReponseBadRequest('Recipe failed to create with error: ' + GeneralTools.getExceptionInfo(sys.exc_info())) return ApiTools.HttpJsonReponse(serializers.serialize('json', Recipe.objects.filter(id=recipeToSave.id)));
def get(self, request, *args, **kwargs): if(len(kwargs) == 1): #return the item passed in try: user = serializers.serialize('json', Profile.objects.filter(id=kwargs['pk'])) except: return ApiTools.HttpJsonReponseBadRequest(GeneralTools.getExceptionInfo(sys.exc_info())) else: #return all objects user = serializers.serialize('json', Profile.objects.all()) return ApiTools.HttpJsonReponseWithJsonData(user)
def patch(self, request, *args, **kwargs): try: data = json.loads(request.body.decode("utf-8")) except: logger.warning(GeneralTools.getExceptionInfo(sys.exc_info())) return ApiTools.HttpJsonReponseBadRequest(GeneralTools.getExceptionInfo(sys.exc_info())) recipe = Recipe.objects.filter(id=data.get('id')) recipe.userid = data.get('userid','') recipe.styleid = data.get('styleid','') recipe.substyleid = data.get('substyleid','') recipe.description = data.get('description','') recipe.boilTime = data.get('boilTime','') recipe.units = data.get('units','') recipe.method = data.get('method','') recipe.batchSize = data.get('batchSize','') recipe.boilSize = data.get('boilSize','') recipe.id = data.get('id','') recipe.abv = data.get('abv','') recipe.ibu = data.get('ibu','') recipe.name = data.get('name','') recipe.save();