def change_password_by_username(username=None, email=None, password=None): """ system change password according to user input :param username: :param email: :param password: :return: """ # TODO: refactor checking and sending email to different system result = ResponseEntity() try: if username is not None: user = User.objects.get(username=username) account = Account.objects.get(user=user) email = account.email user.set_password(password) subject = "Change Password in Conve!" message = "Your password has been change recently. Please report if this is not done by you." recipient_list = [email] utils.send_email(subject, message, recipient_list) user.save() result.success = True except Exception as e: print str(e) result.success = False result.message = str(e) finally: return result
def create_request(request_entity): """ from RequestEntity object (no id), create a request in database and update id to RequestEntity :param request_entity: RequestEntity :return: RequestEntity after update id """ result = ResponseEntity() try: user = User.objects.get(username=request_entity.username) account = Account.objects.get(user=user) request = Request() request.account = account request.save() request_entity.request_id = request.id request_result = convert_RequestEntity_to_Request(request_entity) if request_result.success == True: request_result.data.status = Enum.REQUEST_STATUS.Active.value request_result.data.save() result = request_result else: request_result.data.delete() result = request_result result.data = request # send message # subject = "New request "+unicode(request_entity.title) + " has been sent!!" # message = "You have created a request " + unicode(request_entity.title)+ ". Please contact Conve if it is not done by you." # recipient_list = ['*****@*****.**',request.account.email,'*****@*****.**'] # utils.send_email(subject,message,recipient_list) except Exception as e: print str(e) result.success = False finally: return result
def update_request(request_entity, username): """ update a request that already exists in DB from RequestEntity object (have id), update field in db :param request_entity: RequestEntity :return: RequestEntity after update id """ result = ResponseEntity() try: if check_request_authentication(id, username): raise Exception, 'This request not belong to this user' request_result = convert_RequestEntity_to_Request(request_entity) if request_result.success == True: request_result.data.save() result = request_result except Exception as e: print str(e) result.success = False finally: return result
def copy_AccountEntity_to_Account(account_entity): result = ResponseEntity() try: user = User.objects.get(username=account_entity.username) account = Account.objects.get(user=user) account.last_name = account_entity.last_name account.first_name = account_entity.first_name account.created_date = account_entity.created_date if account_entity.created_date is not None else account.created_date account.email = account_entity.email account.phone = account_entity.first_phone account.extra_phone = account_entity.second_phone account.address = account_entity.address account.photo_link = account_entity.photo_link account.thumb_link = account_entity.thumb_link account.facebook_link = account_entity.facebook_link account.view_num = account_entity.view_num if account_entity.view_num is not None else account.view_num account.description = account_entity.description result.success = True result.data = account except Exception as e: print str(e) result.success = False result.message = str(e) finally: return result
def get_requests(status=None, origin=None, destination=None): """ if status == None then get all else get request list by status :param status: Enum.REQUEST_STATUS :return: """ result = ResponseEntity() try: if status is None: request_list = Request.objects.exclude( status=Enum.REQUEST_STATUS.Deleted.value) else: request_list = Request.objects.filter( status=status.value).order_by("-created_date") if origin is not None: request_list = request_list.filter(origin_city=origin) if destination is not None: request_list = request_list.filter(destination_city=destination) request_entities = list_Request_to_RequestEntity(request_list) result.success = True result.data = request_entities except Exception as e: print str(e) result.message = str(e) result.success = False finally: return result
def verify_account(account_entity, username): result = ResponseEntity() try: if (account_entity.username == username): result.message = "Success" result.success = True else: result.message = "Session username " + str( username ) + " is different from account_entity username " + str( account_entity.username) result.success = False except Exception as e: print str(e) result.success = False result.message = str(e) result.data = None finally: return result
def update_account(account_entity): result = ResponseEntity() try: account_result = copy_AccountEntity_to_Account(account_entity) account_result.data.save() result.message = "Success" result.success = True result.data = account_result.data except Exception as e: print str(e) result.success = False result.message = str(e) result.data = None finally: return result
def get_all_by_user(username): result = ResponseEntity() try: user = User.objects.get(username=username) account = Account.objects.get(user=user) requests = Request.objects.filter(account=account).exclude( Enum.REQUEST_STATUS.Deleted.value) result.message = "Success" result.success = True result.data = requests except Exception as e: print str(e) result.success = False result.message = str(e) result.data = None finally: return result
def reset_password(username_or_email): """ system will generate a random password and send to account email :param username: :param email: :return: """ result = ResponseEntity() try: user = None try: user = User.objects.get(username=username_or_email) account = Account.objects.get(user=user) except Exception as e: pass if user is None: try: account = Account.objects.get(email=username_or_email) except Exception as e: pass if account is None: result.success = False result.message = "There is no username or email match" return email = account.email password = generate_password() user.set_password(password) subject = "Thay đổi mật khẩu ở Conve!" message = "Tên đăng nhập: " + str( user.username) + ". Mật khẩu: " + str( password ) + ". Vì lí do an toàn, bạn hãy đăng nhập và thay đổi mật khẩu." recipient_list = [email] utils.send_email(subject, message, recipient_list) user.save() result.success = True result.data = {"email": email, "username": user.username} except Exception as e: print str(e) result.success = False result.message = str(e) finally: return result
def get_account(username): result = ResponseEntity() try: user = User.objects.get(username=username) account = Account.objects.get(user=user) account_entity = convert_Account_to_AccountEntity(account) result.message = "Success" result.success = True result.data = account_entity except Exception as e: print str(e) result.success = False result.message = str(e) finally: return result
def add_comment(comment_entity): result = ResponseEntity() try: comment = Comment() comment.username = comment_entity.username comment.content = comment_entity.content comment.request_id = comment_entity.request_id comment.created_date = datetime.now(pytz.timezone(settings.TIME_ZONE)) comment.save() result.message = "Success" result.success = True result.data = comment except Exception as e: print str(e) result.success = False result.message = str(e) result.data = None finally: return result
def add_user(username, password, email=None): result = ResponseEntity() try: user = User.objects.create_user(username, email, password) user.save() result.success = True result.data = user except Exception as e: print str(e) result.message = str(e) result.success = False finally: return result
def register(account_entity, password): """ create one record in user table and one record in account table :param account_entity: AccountEntity :param password: :return: Account """ result = ResponseEntity() try: user = User.objects.filter(username=account_entity.username).first() if user is not None: raise ValueError(u"Tên đăng nhập này đã tồn tại trong hệ thống!") user = User.objects.filter(email=account_entity.email).first() if user is not None: raise ValueError(u"Email này đã tồn tại trong hệ thống!") user_result = add_user(account_entity.username, password, email=account_entity.email) if (user_result.success == False): result = user_result return account = Account(last_name=account_entity.last_name, first_name=account_entity.first_name, user=user_result.data, email=account_entity.email, phone=account_entity.first_phone, extra_phone=account_entity.second_phone, address=account_entity.address, photo_link=account_entity.photo_link, facebook_link=account_entity.facebook_link, description=account_entity.description) account.save() result.success = True result.data = convert_Account_to_AccountEntity(account) except ValueError as e: result.success = False result.message = e.message except Exception as e: print str(e) result.success = False # do not throw error message to top layer, to distinguish with valid message # result.message = str(e) finally: return result
def convert_Request_to_RequestEntity(request): """ copy attr values of Request to RequestEntity, not id :param request: :param request_entity: :return: """ result = ResponseEntity() try: request_entity = RequestEntity() request_entity.request_id = request.id request_entity.username = request.account.user.username request_entity.title = request.title request_entity.description = request.description request_entity.category = utils.convert_category_str_to_enum_list( request.category) request_entity.price = request.price request_entity.price_currency = Enum.CURRENCY( int(request.price_currency)) request_entity.origin_city = Enum.CITY(int(request.origin_city)) request_entity.origin_address = request.origin_address request_entity.destination_city = Enum.CITY( int(request.destination_city)) request_entity.destination_address = request.destination_address request_entity.image_url = utils.convert_string_to_list( request.image_url) # list request_entity.thumb_url = utils.convert_string_to_list( request.thumb_url) request_entity.expired_date = localtime(request.expired_date) request_entity.status = Enum.REQUEST_STATUS(int(request.status)) request_entity.created_date = localtime(request.created_date) request_entity.last_modified_date = request.last_modified_date result.success = True result.data = request_entity except Exception as e: print str(e) result.success = False result.message = str(e) finally: return result
def get_request(id): """ from RequestEntity object (no id), create a request in database and update id to RequestEntity :param request_entity: request_entity :return: RequestEntity after update id """ result = ResponseEntity() try: request = Request.objects.exclude( status=Enum.REQUEST_STATUS.Deleted.value).get(id=id) request_entity = convert_Request_to_RequestEntity(request) result.success = True # result.data= request_entity result.data = request_entity.data except Exception as e: print str(e) result.message = str(e) result.success = False finally: return result
def convert_RequestEntity_to_Request(request_entity): """ copy attr values of RequestEntity to Request, not id ex: want to update request :param request: :param request_entity: :return: """ result = ResponseEntity() try: user = User.objects.get(username=request_entity.username) account = Account.objects.get(user=user) request = Request.objects.get(id=request_entity.request_id) request = copy_field_RequestEntity_to_Request(request_entity, request) result.success = True result.data = request except Exception as e: print str(e) result.success = False result.message = str(e) finally: return result
def delete_request(id, username): """ This function will change the status of request into delete :param id: id of request need to be delete :return: """ result = ResponseEntity() try: if not check_request_authentication(id, username): raise Exception, 'This request not belong to this user' request = Request.objects.get(id=id) if request.status == Enum.REQUEST_STATUS.Deleted.value: result.message = "This request is already deleted" result.success = False result.data = convert_Request_to_RequestEntity(request) return request.status = Enum.REQUEST_STATUS.Deleted.value request.save() result.message = "Success" result.success = True result.data = convert_Request_to_RequestEntity(request) except Exception as e: print str(e) result.success = False result.message = str(e) result.data = None finally: return result