def put(self, quantityID): quantityData = parseJsonResponse(request.data) del quantityData['_id'] quantity = Quantity.objects.get(id = quantityID) quantity.modify(**quantityData) quantity.save() return makeJsonResponse(None, 'Quantity saved')
def changePassword(self): requestData = parseJsonResponse(request.data) username = requestData['username'] oldPassword = requestData['oldPassword'] newPassword = requestData['newPassword'] try: user = User.objects.get(username = username) except DoesNotExist: raise NotFoundError("User not found") permission = UserAdminPermission(user) if not permission.can(): raise APIException('You have no permission to change the user password') passwordValid = bcrypt.check_password_hash(user.password, oldPassword) if (not passwordValid): raise APIException('Invalid old password') if (len(newPassword) < 6): raise APIException('Your new password has to be at least 6 characters long') user.modify(password = unicode(bcrypt.generate_password_hash(newPassword))) return makeJsonResponse(None, 'Password changed')
def put(self, moduleID): with AdminPermission.require(): moduleData = parseJsonResponse(request.data) del moduleData['_id'] module = LibraryModule.objects.get(id = moduleID) module.modify(**moduleData) module.save()
def put(self, modelID): """Updates a model definition""" model = Model.objects.get(id=modelID) permission = MP.ModelEditPermission(model) if permission.can(): modelData = parseJsonResponse(request.data) model.modify( name=modelData["name"], description=modelData["description"], board=Board(**modelData["board"]), background=modelData.get("background"), publicAccess=modelData.get("publicAccess"), ) model.save() return makeJsonResponse(None, "Model saved") else: raise UnauthorizedError("You have no permissions to save changes to this model")
def create(self): # If no users exist, init the user DB if (User.objects.count() == 0): self.initUsersDB(); userData = parseJsonResponse(request.data) if (len(userData[u'password']) < 6): raise APIException('Your password has to be at least 6 characters long') if (User.objects(username = userData['username']).count() > 0): raise APIException('User with this username already exists') if (User.objects(email = userData['email']).count() > 0): raise APIException('User with this email already exists') roleUser = Role.objects.get(name='user') user = User( username = userData['username'], email = userData['email'], firstName = userData['firstName'], lastName = userData['lastName'], country = userData['country'], organization = userData.get('organization', ''), password = unicode(bcrypt.generate_password_hash(userData[u'password'])), roles = [roleUser] ) # If no users exist, init the user DB if (User.objects.count() == 0): user.roles.append(Role.objects.get(name='admin')) try: user.save() # Send email to the user msg = Message("Welcome to STEM", recipients = [user.email]) msg.body = """\ Please click on the link to activate your profile http://stem.sysmoltd.com/stem/api/Users/confirm?username={}&activationCode={}""".format(user.username, str(user.id)) mail.send(msg) # Send email to admin msg = Message("New user registration", recipients = ["*****@*****.**"]) msg.body = "username: {}\n email: {}\n".format(user.username, user.email) mail.send(msg) except NotUniqueError: raise APIException('Registration failed. Please contact the administrator [email protected]') return makeJsonResponse({ 'msg': 'Successfully created user {}'.format(user.username) })
def login(self): userData = parseJsonResponse(request.data) if current_user.is_authenticated(): return makeJsonResponse({'msg': 'You are already logged in'}) else: try: user = User.objects.get(email = userData['id']) except DoesNotExist: raise APIException('User does not exist') if (not user.active): raise APIException('User has not been activated or has been deactivated. Please contact the administrator!') if (not user.confirmed): raise APIException('Your registration has not been confirmed. Please visit the link found in yout email!') passwordValid = bcrypt.check_password_hash(user.password, userData['password']) if (passwordValid): login_user(user) identity_changed.send(current_app._get_current_object(), identity = Identity(user.get_id())) response = makeJsonResponse({'msg': 'You have sucessfully logged in'}) response.set_cookie('user.username', user.username) response.set_cookie('user.roles', '-'.join([role.name for role in user.roles])) return response else: raise APIException('Incorrect password')
def compute(self): modelData = parseJsonResponse(request.data) ex = ModelCalculator(modelData) ex.compute() return makeJsonResponse(modelData, "Model computed")