def confirmed(cls, confirmation): # tries to retrieves the account for the provided confirmation # code and in case it fails produces an error account = cls.get(confirmation = confirmation) if not account: raise quorum.OperationalError("Account not found or invalid confirmation") if account.enabled: raise quorum.OperationalError("Account is already active") # sets the account model as enabled and then saves it in the # current data source account.enabled = True account.save()
def mail_activity(api=None, id=None, year=None, month=None, validate=False, links=True): api = api or logic.get_api() employee = api.get_employee(id) if id else api.self_employee() name = employee.get("full_name", None) working = employee.get("working", None) contact_information = employee.get("primary_contact_information", {}) email = contact_information.get("email", None) if not name: raise quorum.OperationalError("No name defined") if not email: raise quorum.OperationalError("No email defined") if not working == 1: raise quorum.OperationalError("No longer working") now = datetime.datetime.utcnow() now_s = now.strftime("%B %d %Y") operations,\ target_s,\ sales_total,\ sales_s,\ returns_s,\ _previous_month,\ _previous_year,\ _next_month,\ _next_year,\ _has_next = get_sales(api = api, id = id, year = year, month = month) if validate and not operations: return quorum.debug("Sending activity email to %s <%s>" % (name, email)) quorum.send_mail(subject=ACTIVITY_SUBJECT[config.LOCALE] % (target_s, now_s), sender=config.SENDER_EMAIL, receivers=["%s <%s>" % (name, email)], rich="email/activity.%s.html.tpl" % config.LOCALE, context=dict(settings=dict(logo=True, links=links), target=target_s, operations=operations, sales_total=sales_total, sales_count=len(sales_s), returns_count=len(returns_s), base_url=config.BASE_URL, omnix_base_url=config.OMNI_URL, commission_rate=config.COMMISSION_RATE))
def login(cls, username, password): # verifies that both the username and the password are # correctly set in the current instance if not username or not password: raise quorum.OperationalError( "Both username and password must be provided", code = 400 ) # retrieves the account associated with the provided username # in case none is found raises an operational error indicating # the problem with the account retrieval account = cls.get( username = username, rules = False, build = False, raise_e = False ) if not account: raise quorum.OperationalError( "No valid account found", code = 403 ) # creates the SHA1 hash value for the password and verifies that # the provided password is the expected password_sha1 = hashlib.sha1(quorum.legacy.bytes(password + PASSWORD_SALT)).hexdigest() _password = account.password if not password_sha1 == _password: raise quorum.OperationalError( "Invalid or mismatch password", code = 403 ) # sets the login count and last login values in the account as the # current time and then saves it in the data store login_count = account.val("login_count", 0) account.login_count = login_count + 1 account.last_login = time.time() account.save() # returns the account representing the user that has just been logged # in into the system to the caller method return account
def mail_birthday(api=None, id=None, links=True): api = api or logic.get_api() employee = api.get_employee(id) if id else api.self_employee() name = employee.get("full_name", None) working = employee.get("working", None) contact_information = employee.get("primary_contact_information", {}) email = contact_information.get("email", None) if not name: raise quorum.OperationalError("No name defined") if not email: raise quorum.OperationalError("No email defined") if not working == 1: raise quorum.OperationalError("No longer working") quorum.debug("Sending birthday email to %s <%s>" % (name, email)) quorum.send_mail(subject=BIRTHDAY_SUBJECT[config.LOCALE], sender=config.SENDER_EMAIL, receivers=["%s <%s>" % (name, email)], rich=config.BIRTHDAY_TEMPLATE, context=dict(settings=dict(logo=True, links=links), base_url=config.BASE_URL, omnix_base_url=config.OMNI_URL, commission_rate=config.COMMISSION_RATE))
def sso_login(cls, id, timestamp, token, nav_data): # retrieves the various configuration properties # to be used for this operation salt_h = quorum.conf("salt_h", None) # re-creates the token from the provided id and timestamp # and the "secret" salt value _token = id + ":" + salt_h + ":" + timestamp _token = quorum.legacy.bytes(_token) _token_s = hashlib.sha1(_token).hexdigest() # retrieves the current time to be used in the timestamp # validation process current_time = time.time() # validation the token and then checks if the provided timestamp # is not defined in the past if not _token_s == token: raise quorum.OperationalError("Invalid token", code = 403) if not current_time < timestamp: return quorum.OperationalError("Invalid timestamp (in the past)", code = 403) # tries to retrieve the account associated with the provided # id value in case none is found returns in error account = Account.get(username = id, build = False, raise_e = False) if not account: return quorum.OperationalError("No user found", code = 403) # sets the login count and last login values in the account as the # current time and then saves it in the data store login_count = account.val("login_count", 0) account.login_count = login_count + 1 account.last_login = time.time() account.save(account) # returns the account representing the user that has just been logged # in into the system to the caller method return account
def csv_file( file, callback, header = False, delimiter = ",", strict = False, encoding = "utf-8" ): _file_name, mime_type, data = file is_csv = mime_type in ("text/csv", "application/vnd.ms-excel") if not is_csv and strict: raise quorum.OperationalError("Invalid MIME type '%s'" % mime_type) data = data.decode(encoding) buffer = quorum.legacy.StringIO(data) return csv_import( buffer, callback, header = header, delimiter = delimiter )