def forgotPassword(request, format=None): if request.method == 'PUT': data = JSONParser().parse(request) TO = getPropertyByName('email', data.items()) if emailExists(TO): code = md5Encoding(tokenGenerator()) SUBJECT = "AliveTracker forgot password instructions. (DO NOT REPLY)" FROM = "*****@*****.**" MESSAGE = """ Hey, we heard you lost your AliveTracker password. Use the following link to reset your password: http://www.alivetracker.com/#resetPasswordPage?email=""" + TO + """&token=""" + code + """ Ignore this email if you haven't experienced any password trouble. Thanks, AliveTracker Team""" try: tmpUser = User.objects.get(email=TO) User_Forgot_Password.objects.get_or_create(user=tmpUser) User_Forgot_Password.objects.filter(user=tmpUser).update(token=code) except: return responseJsonUtil(False, 'ERROR000', None) try: sendEmail(FROM, TO, SUBJECT, MESSAGE) return responseJsonUtil(True, None, None) except: return responseJsonUtil(False, 'ERROR002', None) else: return responseJsonUtil(False, 'ERROR102', None)
def getAllProjects(argRequest, format=None): try: tmpAllProjects = Project.objects.all() tmpProjectsSerialize = ProjectSerializer(tmpAllProjects) return responseJsonUtil(True, None, tmpProjectsSerialize) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def passwordSendEmail(request, format=None): if request.method == 'PUT': data = JSONParser().parse(request) email = getPropertyByName('email', data.items()) token = getPropertyByName('token', data.items()) if correctForgotPasswordToken(email, token): TO = email tmpPassword = tokenGenerator() code = md5Encoding(tmpPassword) SUBJECT = "AliveTracker reset password message. (DO NOT REPLY)" FROM = "*****@*****.**" MESSAGE = """ You requested to have your password reset, below is your new password. Username:""" + TO + """ New Password: """ + tmpPassword + """ To login your new password, please go to http://www.alivetracker.com Thanks, AliveTracker Team""" try: User.objects.filter(email=TO).update(password=code, entity_status=2) except: return responseJsonUtil(False, 'ERROR000', None) try: sendEmail(FROM, TO, SUBJECT, MESSAGE) return responseJsonUtil(True, None, None) except: return responseJsonUtil(False, 'ERROR002', None) else: return responseJsonUtil(False, 'ERROR100', None)
def getAllUsers(argRequest, format=None): try: tmpAllUsers = User.objects.all() tmpAllUsersSerializer = UserSerializer(tmpAllUsers) return responseJsonUtil(True, None, tmpAllUsersSerializer) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def getProjectsByGroup(argRequest, argGroupId, format=None): try: tmpResult = Project.objects.all().filter(group=argGroupId, entity_status=0) serializer = ProjectSerializer(tmpResult) return responseJsonUtil(True,None,serializer) except BaseException: return responseJsonUtil(False,'ERROR000', None)
def saveProject(argRequest, argGroupId, format=None): try: if not userAuthentication(argRequest): return responseJsonUtil(False, 'ERROR103', None) tmpData = JSONParser().parse(argRequest) if argRequest.method == 'POST': tmpNewProject = Project.objects.create(name=getPropertyByName('name', tmpData.items()), description=getPropertyByName('description', tmpData.items()), created=date.today(), entity_status=0, group=Group.objects.get(pk=argGroupId)) updateUserListInProject(tmpData, tmpNewProject.id) tmpSerializer = ProjectSerializer(tmpNewProject) return responseJsonUtil(True, None, tmpSerializer) if argRequest.method == 'PUT': Project.objects.filter(id=getPropertyByName('id', tmpData.items())).update( name=getPropertyByName('name', tmpData.items()), description=getPropertyByName('description', tmpData.items()), group=Group.objects.get(pk=argGroupId)) updateUserListInProject(tmpData, getPropertyByName('id', tmpData.items())) return responseJsonUtil(True, None, None) except Project.DoesNotExist: return responseJsonUtil(False, 'ERROR500', None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def deleteUserProject(argRequest, argProjectId, argUserId,format=None): try: Project_User.objects.filter(user_id=argUserId, project_id=argProjectId).delete() return responseJsonUtil(True,None,None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def getRoles(argRequest): if not userAuthentication(argRequest): return responseJsonUtil(False, 'ERROR100', None) try: tmpRoles = Role.objects.all() tmpRolesSerializer = RoleSerializer(tmpRoles) return responseJsonUtil(True, None, tmpRolesSerializer) except Role.DoesNotExist: return responseJsonUtil(False, 'ERROR600', None)
def user_permissions(request, pk, format=None): try: tmpGroup = Group.objects.get(pk=pk, entity_status=0) tmpUser = getUserByRequest(request) serializer = getGroupPermissionsByUser(tmpUser, tmpGroup) return responseJsonUtil(True, None, serializer) except User.DoesNotExist: return responseJsonUtil(False, 'ERROR100', None) except Group.DoesNotExist: return responseJsonUtil(False, "ERROR200", None)
def register_user(request): try: data = JSONParser().parse(request) tmpNewUser = User.objects.create(email=getPropertyByName('email', data.items()), password=getPropertyByName('password', data.items())); newSessionHandler(request, tmpNewUser); tmpUserSerializer = UserSerializer(tmpNewUser) return responseJsonUtil(True, None, tmpUserSerializer) except BaseException: return responseJsonUtil(False, 'ERROR101', None)
def getUsers(argRequest, argEmail, format=None): try: if not userAuthentication(argRequest): return responseJsonUtil(False, 'ERROR103', None) tmpLimit = 10 tmpResult = User.objects.filter(email__icontains=argEmail)[:tmpLimit] tmpSerializer = UserSerializer(tmpResult) return responseJsonUtil(True, None, tmpSerializer) except Group.DoesNotExist: return responseJsonUtil(False, "ERROR200", None)
def deleteUser(argRequest, argUserID, argGroupID): if not userAuthentication(argRequest): return responseJsonUtil(False, 'ERROR100', None) try: tmpGroupUser = Group_User.objects.get(user_id=str(argUserID), group_id=str(argGroupID)) tmpGroupUser.delete() return responseJsonUtil(True, None, None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def setPassword(request, format=None): if request.method == 'PUT': if not userAuthentication(request): return responseJsonUtil(False, 'ERROR103', None) data = JSONParser().parse(request) tmpPassword = getPropertyByName('password', data.items()) User.objects.filter(session_key=request.session._session_key).update(password=tmpPassword) tmpUser = getUserByRequest(request) tmpSerializer = UserSerializer(tmpUser) return responseJsonUtil(True, None, tmpSerializer)
def getUserAuth(argRequest, format=None): try: if userAuthentication(argRequest): tmpUser = User.objects.raw( 'Select * from main_user where session_key = \'' + argRequest.session.session_key + '\'') tmpSerializer = UserSerializer(tmpUser) return responseJsonUtil(True, None, tmpSerializer) return responseJsonUtil(False, 'ERROR103', None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def update_default_group(request, pk, format=None): try: user = User.objects.get(pk=pk) except User.DoesNotExist: return responseJsonUtil(False, 404, None) data = JSONParser().parse(request) tmpDefaultGroup = getPropertyByName('default_group', data.items()) user.default_group = tmpDefaultGroup user.save() serializer = UserSerializer(user, data=data) return responseJsonUtil(True, None, serializer)
def deleteProject(argRequest, argProjectID, format=None): try: if not userAuthentication(argRequest): return responseJsonUtil(False, 'ERROR103', None) if argRequest.method == 'DELETE': Project.objects.filter(id=argProjectID).update(entity_status=1) tmpProject = Project.objects.filter(id=argProjectID) tmpSerializer = ProjectSerializer(tmpProject) return responseJsonUtil(True, None, tmpSerializer) except Project.DoesNotExist: return responseJsonUtil(False, 'ERROR500', None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def saveUserProject(argRequest,argProjectID,argUserId, format=None): try: tmpUser = User.objects.get(pk=argUserId) tmpUserRole = Role.objects.get(pk=2) tmpProject = Project.objects.get(pk=argProjectID) Project_User.objects.create(user=tmpUser, project=tmpProject, role=tmpUserRole) return responseJsonUtil(True,None,None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def update_user(request, pk, format=None): try: user = User.objects.get(pk=pk) except User.DoesNotExist: return responseJsonUtil(False, 404, None) data = JSONParser().parse(request) tmpName = getPropertyByName('name', data.items()) tmpPassword = getPropertyByName('password', data.items()) user.name = tmpName user.password = tmpPassword user.entity_status = 0 user.save() serializer = UserSerializer(user, data=data) return responseJsonUtil(True, None, serializer)
def getAllProjectsByUser(argRequest, format=None): if not userAuthentication(argRequest): return responseJsonUtil(False,'ERROR103', None) try: tmpUserId = getUserByRequest(argRequest)._get_pk_val tmpResult = Project.objects.all().filter(project_user__user=tmpUserId, entity_status=0) serializer = ProjectSerializer(tmpResult) return responseJsonUtil(True,None,serializer) except User.DoesNotExist: return responseJsonUtil(False, 'ERROR400', None) except Project.DoesNotExist: return responseJsonUtil(False, 'ERROR500', None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def resetPassword(request, format=None): if request.method == 'PUT': data = JSONParser().parse(request) tmpEmail = getPropertyByName('email', data.items()) tmpToken = getPropertyByName('token', data.items()) tmpPassword = getPropertyByName('password', data.items()) if correctForgotPasswordToken(tmpEmail, tmpToken): User.objects.filter(email=tmpEmail).update(password=tmpPassword) tmpUser = User.objects.get(email=tmpEmail) newSessionHandler(request, tmpUser); tmpSerializer = UserSerializer(tmpUser) return responseJsonUtil(True, None, tmpSerializer) else: return responseJsonUtil(False, 'ERROR104', None)
def getUserByGroupAndProject(request, group, project): if not userAuthentication(request): return responseJsonUtil(False, 'ERROR103', None) if not groupExists(group): return responseJsonUtil(False, 'ERROR200', None) if not projectExists(project): return responseJsonUtil(False, 'ERROR500', None) if request.method == 'GET': tmpResultUser = User.objects.raw('select * from main_user tmpUser ' 'inner join (select user_id as userId, role_id as role_id from main_project_user where project_id in ' '(select id from main_project where group_id = ' + str( group) + ' and id=' + str(project) + ' )) tmpProjectUser on tmpUser.id = tmpProjectUser.userId') tmpSerializer = UserDTOSerializer(tmpResultUser) return responseJsonUtil(True, None, tmpSerializer)
def getProjectsByUserAndGroup(argRequest, argGroupID, format=None): if not userAuthentication(argRequest): return responseJsonUtil(False, 'ERROR103', None) try: tmpMail = getUserByRequest(argRequest).email tmpResult = Project.objects.raw('select mproject.id, mproject.name, mproject.created, mproject.group_id \ from main_project_user project_user inner join main_user muser on project_user.user_id = muser.id \ inner join main_project mproject on project_user.project_id = mproject.id \ where muser.entity_status = 0 and mproject.entity_status = 0 and muser.email= \'' + str(tmpMail) + '\' and mproject.group_id = ' + str(argGroupID)) serializer = ProjectSerializer(tmpResult) return responseJsonUtil(True, None, serializer) except User.DoesNotExist: return responseJsonUtil(False, 'ERROR400', None) except Project.DoesNotExist: return responseJsonUtil(False, 'ERROR500', None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def user_services(request, pk, format=None): if not userAuthentication(request): return responseJsonUtil(False, 'ERROR103', None) if request.method == 'GET': return user_authentication(request) if request.method == 'POST': return register_user(request) if request.method == 'PUT': return update_user(request, pk)
def user_authentication(argRequest, format=None): try: tmpData = JSONParser().parse(argRequest) tmpEmail = str(getPropertyByName('email', tmpData.items())) tmpPassword = str(getPropertyByName('password', tmpData.items())) tmpUser = User.objects.exclude(entity_status = 1).get(password=tmpPassword, email=tmpEmail) if argRequest.method == 'POST': if 'id' not in argRequest.session or argRequest.session._session_key == locales.INVALID_SESSION_KEY: newSessionHandler(argRequest, tmpUser) else: User.objects.filter(pk=tmpUser.id).update(session_key=argRequest.session._session_key) tmpSerializer = UserSerializerDTO(tmpUser) return responseJsonUtil(True, None, tmpSerializer) except User.DoesNotExist: return responseJsonUtil(False, 'ERROR400', None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def getUserProjectByGroup(argRequest, argProjectID, format=None): if not userAuthentication(argRequest): return responseJsonUtil(False, 'ERROR103', None) try: tmpProject = Project.objects.get(id=argProjectID) tmpProjectSerializer = ProjectSerializer(tmpProject) cursor = connection.cursor() cursor.execute('select muser.id as id, muser.email as name, mrole.name as role\ from main_project_user project_user inner join main_user muser on muser.id = project_user.user_id \ inner join main_role mrole on project_user.role_id = mrole.id \ where muser.entity_status = 0 and project_user.project_id = ' + str(argProjectID)) tmpResult = cursor.fetchall() connection.close() tmpUserSerializer = convertUserRole(tmpResult) tmpProjectUserListSerializer = createProjectListDTOObject(tmpProjectSerializer, tmpUserSerializer, argProjectID) return responseJsonUtil(True, None, tmpProjectUserListSerializer) except Project.DoesNotExist: return responseJsonUtil(False, 'ERROR500', None) except BaseException: return responseJsonUtil(False, 'ERROR000', None)
def createOrUpdateUserGroup(argRequest, argGroupID, argEmailUser, format=None): data = JSONParser().parse(argRequest) if argRequest.method == 'POST': try: TO = argEmailUser FROM = "*****@*****.**" tmpPassword = tokenGenerator() onUserInvitationEmail(FROM,TO,tmpPassword) encryptedPassword = md5Encoding(tmpPassword) tmpNewUser = User.objects.create(email=argEmailUser, password=encryptedPassword) newSessionHandler(argRequest, tmpNewUser) tmpGroupInstance = Group.objects.get(pk=argGroupID) Group_User.objects.create(user=tmpNewUser, group=tmpGroupInstance, role=getDeveloperRole()) tmpUserSerializer = UserSerializer(tmpNewUser) return responseJsonUtil(True, None, tmpUserSerializer) except BaseException: return responseJsonUtil(False, 'ERROR101', None) if argRequest.method == 'PUT': try: tmpUserToAddGroup = User.objects.get(pk=getPropertyByName('id', data.items())) tmpGroupInstance = Group.objects.get(pk=argGroupID) Group_User.objects.create(user=tmpUserToAddGroup, group=tmpGroupInstance, role=getDeveloperRole()) tmpUserSerializer = UserSerializer(tmpUserToAddGroup) return responseJsonUtil(True, None, tmpUserSerializer) except BaseException: return responseJsonUtil(False, 'ERROR101', None)
def logout(argRequest): if argRequest.method == 'POST': if 'id' in argRequest.session: argRequest.session['id'] = locales.INVALID_SESSION_KEY argRequest.session._session_key = locales.INVALID_SESSION_KEY return responseJsonUtil(True, None, None)