def user_login(): """Log a user in and return their information""" uname = flask.request.args['username'].lower() passw = flask.request.args['password'] # Try and find the user user = None try: user = models.Provider.objects(username=uname).get() except DoesNotExist: return helpers.api_error( code=401, data='username', message='Username does not exist' ) # Check the password if user.password != passw: return helpers.api_error( code=401, data='password', message='Password is incorrect' ) # Log them in! flask_login.login_user(user) return helpers.api_success(data=user)
def debug_login(): action = flask.request.args.get('action', None) if action == 'make': email = flask.request.args.get('email', '') try: user = models.User.objects(email=email).get() except: helpers.api_error(code=404, message='Invalid email') t = models.Ticket(user=user) t.save() dialog = open('./dialogs/utility-auth.txt', 'r').read() print(dialog.format(colorama, user, t)) return helpers.api_success() elif action == 'confirm': token = flask.request.args.get('token', None) ticket = models.Ticket.objects(id=token).get() user = ticket.user flask_login.login_user(user, fresh=True) return helpers.api_success() elif action == 'release': flask_login.logout_user() return helpers.api_success() else: helpers.api_error(code=400, message='Invalid utility-auth action')
def model_get(model_name, pitch_a_fit=True): '''Given the name of a mongo model, retrieve it's actual class''' # make sure they aren't dumb AF, else carry on if model_name not in model_lookup: if pitch_a_fit: helpers.api_error(400, model_name + ' is not a valid model name') else: return None return model_lookup[model_name]
def api_document_put(document_name, document_id): '''Make changes to an existing document and save it to the database''' user = helpers.user_required(1) model = models.model_get(document_name) # get the dictionary of arguments args = flask.request.get_json() # get the document document = None try: document = model.objects.get(id=document_id) except mongoengine.errors.ValidationError: return helpers.api_error(message='No document found', code=404) # iterate through the args and screen them if args: for arg_name in args: print('PROCESSING ARG_NAME', arg_name) print('VALUE', args[arg_name]) # update the document and return a success document.update(**args) document.reload() if document._after_put(user): document.save() return helpers.api_success(message='Document updated.')
def api_document_delete(document_name, document_id): """Delete an existing document from the database""" user = helpers.user_required(1) model = models.model_get(document_name) # get the document document = None try: document = model.objects.get(id=document_id) except mongoengine.errors.ValidationError: return helpers.api_error(message='No document found', code=404) # delete it! document.delete() return helpers.api_success(message='Document deleted.')