Пример #1
0
 def _build_url(self):
     if not self.sid:
         raise appier.OperationalError(message="No account sid provided")
     if not self.auth_token:
         raise appier.OperationalError(message="No auth token provided")
     self.secure_url = self.base_template % (self.sid, self.auth_token)
     self.account_url = self.secure_url + "Accounts/%s/" % self.sid
Пример #2
0
    def login_key(cls, key):
        # verifies that secret key is provided, is considered valid for domain
        # and that it is correctly and properly defined (required for validation)
        if not key:
            raise appier.OperationalError(
                message="Secret key must be provided", code=400)

        # tries to retrieve the account with the provided username, so that
        # the other validation steps may be done as required by login operation
        account = cls.get(key=key, rules=False, build=False, raise_e=False)
        if not account:
            raise appier.OperationalError(message="No valid account found",
                                          code=403)

        # verifies that the retrieved account is currently enabled, because
        # disabled accounts are not considered to be valid ones
        if not account.enabled:
            raise appier.OperationalError(message="Account is not enabled",
                                          code=403)

        # "touches" the current account meaning that the last login value will be
        # updated to reflect the current time and then returns the current logged
        # in account to the caller method so that it may used (valid account)
        account.touch_login_s()
        return account
Пример #3
0
 def _build_url(self):
     if self.is_oauth():
         self.base_url = "https://%s/" % API_DOMAIN
         return
     if not self.username:
         raise appier.OperationalError(message="No username provided")
     if not self.password:
         raise appier.OperationalError(message="No password provided")
     self.base_url = "https://%s:%s@%s/" % (self.username, self.password,
                                            API_DOMAIN)
Пример #4
0
 def reset_s(self, password, password_confirm):
     if not password:
         raise appier.OperationalError(message="No password provided",
                                       code=400)
     if not password == password_confirm:
         raise appier.OperationalError(
             message="Invalid password confirmation", code=400)
     self.password = password
     self.password_confirm = password_confirm
     self.reset_token = None
     self.save()
Пример #5
0
 def _build_url(self):
     if not self.api_key:
         raise appier.OperationalError(message = "No API key provided")
     if not self.password:
         raise appier.OperationalError(message = "No password provided")
     if not self.store_url:
         raise appier.OperationalError(message = "No store URL provided")
     self.base_url = "https://%s:%s@%s/" % (
         self.api_key, self.password, self.store_url
     )
     self.website_url = "http://%s/" % (self.website_url or self.store_url)
Пример #6
0
 def _csv_read(
     cls,
     file,
     mime_type = None,
     strict = False,
     named = False,
     delimiter = ",",
     quotechar = "\"",
     quoting = csv.QUOTE_MINIMAL,
     encoding = "utf-8"
 ):
     is_unicode = appier.legacy.PYTHON_3
     if appier.legacy.is_bytes(file): mime_type, data = mime_type, file
     else: _file_name, mime_type, data = file
     is_csv = mime_type in ("text/csv", "application/vnd.ms-excel")
     if not is_csv and strict:
         raise appier.OperationalError(
             message = "Invalid MIME type '%s'" % mime_type
         )
     if is_unicode:
         data = data.decode(encoding)
         buffer = appier.legacy.StringIO(data)
     else:
         buffer = appier.legacy.BytesIO(data)
     csv_reader = csv.reader(
         buffer,
         delimiter = delimiter,
         quotechar = quotechar,
         quoting = quoting
     )
     return csv_reader
Пример #7
0
 def _json_read(cls, file, strict=False, encoding="utf-8"):
     _file_name, mime_type, data = file
     is_json = mime_type in ("text/json", "application/json")
     if not is_json and strict:
         raise appier.OperationalError(message="Invalid MIME type '%s'" %
                                       mime_type)
     data = data.decode(encoding)
     json_data = json.loads(data)
     return json_data
Пример #8
0
 def loads(self, value):
     cls = self.__class__
     if isinstance(value, cls):
         self._datetime = value._datetime
     elif isinstance(value, datetime.datetime):
         self._datetime = value
     elif isinstance(value, (int, float)):
         self._datetime = datetime.datetime.utcfromtimestamp(value)
     else:
         raise appier.OperationalError()
Пример #9
0
    def login(cls, username, password, insensitive=True):
        # in case the (case) insensitive option is enabled and the username
        # is defined the value is converted to lower case so that a proper
        # comparison may be used (not case sensitive)
        if insensitive and username: username = username.lower()

        # verifies that both the provided username and password are valid
        # and that are correctly and properly defined (required for validation)
        if not username or not password:
            raise appier.OperationalError(
                message="Both username and password must be provided",
                code=400)

        # tries to retrieve the account with the provided username, so that
        # the other validation steps may be done as required by login operation
        account = cls.get(username=username,
                          rules=False,
                          build=False,
                          raise_e=False)
        if not account:
            raise appier.OperationalError(message="No valid account found",
                                          code=403)

        # verifies that the retrieved account is currently enabled, because
        # disabled accounts are not considered to be valid ones
        if not account.enabled:
            raise appier.OperationalError(message="Account is not enabled",
                                          code=403)

        # retrieves the value of the password for the stored account and then
        # verifies that the value matched the one that has been provided
        _password = account.password
        if not cls.verify(_password, password):
            raise appier.OperationalError(
                message="Invalid or mismatch password", code=403)

        # "touches" the current account meaning that the last login value will be
        # updated to reflect the current time and then returns the current logged
        # in account to the caller method so that it may used (valid account)
        account.touch_login_s()
        return account
Пример #10
0
    def recover(cls, identifier, send_email=False):
        # verifies if a valid identifier has been provided because that value
        # is required for the proper account recover process to be executed
        if not identifier:
            raise appier.OperationalError(
                message="An identifier must be provided", code=400)

        # creates the keyword based arguments that are going to be used to provide
        # an extra layer of or based filtering to the retrieval process of the account
        # that is going to be performed next
        kwargs = {"$or": [dict(username=identifier), dict(email=identifier)]}
        account = cls.get(build=False, raise_e=False, **kwargs)

        # in case no account has been retrieved an error is raised indicating such
        # problem, as that is required for the account recover process
        if not account:
            raise appier.OperationalError(message="No valid account found",
                                          code=403)

        # runs the recover instance method that should generate a new reset token
        # for the account and send the proper notifications
        return account.recover_s(send_email=send_email)
Пример #11
0
def _handle_error(error, fallback=None, code=400, encoding="utf-8"):
    # reads the complete set of data from the error (assumes
    # that the error is string based and implements a read interface)
    data = error.read()

    # tries to retrieve the proper message by decoding the
    # received bytes using the provided encoding value, in
    # case it fails sets a default message (as fallback)
    is_bytes = appier.legacy.is_bytes(data)
    try:
        data_s = data.decode(encoding) if is_bytes else str(data)
    except UnicodeDecodeError:
        data_s = "Undefined message"

    try:
        # tries to load the data as a JSON based message (for
        # structure parsing) may fail due to bad JSON structure
        # if the message data is not JSON valid
        data_j = json.loads(data_s)

        # tries to retrieve the message from the provided
        # information, as expected by the structure
        message = data_j.get("message", None)
    except Exception:
        # in case there was an unexpected error interpreting
        # the data then sets the data itself as the message
        message = data_s

    # in case the message is defined special final characters
    # must be removed in order to normalize the message
    if message: message = message.rstrip(".")

    # in case there is no message then uses the fallback instead
    # as the fallback should represent the same error but in a
    # much broader sense (less accurate)
    fallback = fallback or data_s
    message = message or fallback

    # raises the error as an exception using the proper internal
    # usage error format (as expected by upper layers)
    raise appier.OperationalError(message=message, code=code)
Пример #12
0
 def upgrade_service(self,
                     id,
                     batch_size=1,
                     interval=2000,
                     full_upgrade=True,
                     try_finish=True,
                     safe=True,
                     launch_config=None):
     url = self.base_url + "services/%s?action=upgrade" % id
     if try_finish: self._service_try_finish(id)
     if launch_config == None:
         launch_config = self._service_launch_config(id)
     if safe and not self._service_active(id):
         raise appier.OperationalError(
             message="Service is currently not ready for upgrade")
     contents = self.post(
         url,
         data_j=dict(inServiceStrategy=dict(batchSize=batch_size,
                                            intervalMillis=interval,
                                            fullUpgrade=full_upgrade,
                                            launchConfig=launch_config,
                                            secondaryLaunchConfigs=[])))
     data = contents["data"]
     return data
Пример #13
0
 def raiser(): raise appier.OperationalError(message = "hello")
 struct = appier.lazy_dict(
Пример #14
0
 def raiser():
     raise appier.OperationalError(message="hello")
Пример #15
0
 def ensure_set(self, **kwargs):
     for key, value in kwargs.items():
         if value: continue
         raise appier.OperationalError(message="Invalid %s received '%s'" %
                                       (key, value))
Пример #16
0
 def _build_url(self):
     if not self.store_url:
         raise appier.OperationalError(message = "No store URL provided")
     self.base_url = "https://%s/" % self.store_url
Пример #17
0
 def ensure_method(cls, payment_method):
     if payment_method == "credit_card": return
     raise appier.OperationalError(message="Unexpected payment method")
Пример #18
0
 def _build_url(self):
     if not self.repo:
         raise appier.OperationalError(message="No repo name provided")
     if not self.base_url:
         raise appier.OperationalError(message="No base URL provided")
     self.base_url = self.base_url % self.repo
Пример #19
0
 def _ensure_same(message = None):
     if element._is_same(): return
     message = message or "Element (%s) is not the same" % element._text()
     raise appier.OperationalError(message = message)