コード例 #1
0
ファイル: models.py プロジェクト: mundo03/toggl-cli
    def stop_and_save(self=None, stop=None):
        """
        Stops running the entry. It has to be running entry.

        :param stop: DateTime which should be set as stop time. If None, then current time is used.
        :return: Self
        """
        if self is None:
            # noinspection PyMethodFirstArgAssignment
            self = TimeEntry.objects.current()
            if self is None:
                raise exceptions.TogglValidationException(
                    'There is no running entry to be stoped!')

        if not self.is_running:
            raise exceptions.TogglValidationException(
                'You can\'t stop not running entry!')

        config = self._config or utils.Config.factory()

        if stop is None:
            stop = pendulum.now(config.timezone)

        self.stop = stop
        self.is_running = False
        self.save(config=config)

        return self
コード例 #2
0
ファイル: models.py プロジェクト: tbrodbeck/toggl-cli
    def signup(cls, email, password, timezone=None, created_with='TogglCLI',
               config=None):  # type: (str, str, str, str, utils.Config) -> User
        """
        Creates brand new user. After creation confirmation email is sent to him.

        :param email: Valid email of the new user.
        :param password: Password of the new user.
        :param timezone: Timezone to be associated with the user. If empty, than timezone from config is used.
        :param created_with: Name of application that created the user.
        :param config:
        :return:
        """
        if config is None:
            config = utils.Config.factory()

        if timezone is None:
            timezone = config.timezone

        if not validate_email(email):
            raise exceptions.TogglValidationException('Supplied email \'{}\' is not valid email!'.format(email))

        user_json = json.dumps({'user': {
            'email': email,
            'password': password,
            'timezone': timezone,
            'created_with': created_with
        }})
        data = utils.toggl("/signups", "post", user_json, config=config)
        return cls.deserialize(config=config, **data['data'])
コード例 #3
0
    def validate(
            self, value,
            instance):  # type: (str, typing.Optional['base.Entity']) -> None
        super().validate(value, instance)

        if value not in self.choices:
            raise exceptions.TogglValidationException(
                'Value \'{}\' is not valid choice!'.format(value))
コード例 #4
0
ファイル: models.py プロジェクト: tbrodbeck/toggl-cli
    def invite(self, *emails):  # type: (str) -> None
        """
        Invites users defined by email addresses. The users does not have to have account in Toggl, in that case after
        accepting the invitation, they will go through process of creating the account in the Toggl web.

        :param emails: List of emails to invite.
        :return: None
        """
        for email in emails:
            if not validate_email(email):
                raise exceptions.TogglValidationException('Supplied email \'{}\' is not valid email!'.format(email))

        emails_json = json.dumps({'emails': emails})
        data = utils.toggl("/workspaces/{}/invite".format(self.id), "post", emails_json, config=self._config)

        if 'notifications' in data and data['notifications']:
            raise exceptions.TogglException(data['notifications'])
コード例 #5
0
    def validate(self, value, instance):  # type: (T, base.Entity) -> None
        """
        Validates if the passed value is valid from the perspective of the data type that the field represents.

        Basic implementation validate only 'required' and 'premium' attributes.

        :param instance: Instance of the TogglEntity which we are validating the value against
        :param value: Any value
        :raises exceptions.TogglValidationException: When the passed value is not valid.
        :raises exceptions.TogglPremiumException: If the associated Workspace is not premium workspace.
        """
        if self.required and self.default is NOTSET and not value:
            raise exceptions.TogglValidationException(
                'The field \'{}\' is required!'.format(self.name))

        if self.premium:
            from .models import WorkspacedEntity, Workspace
            workspace = instance.workspace if isinstance(
                instance, WorkspacedEntity) else instance  # type: Workspace

            if getattr(instance, self.name, False) and not workspace.premium:
                raise exceptions.TogglPremiumException(
                    'You are trying to save object with premium field \'{}.{}\' in non-premium Workspace: {}'
                    .format(instance.__class__.__name__, self.name, workspace))
コード例 #6
0
    def validate(self, value, instance):
        super().validate(value, instance)

        if not validate_email(value):
            raise exceptions.TogglValidationException(
                'Email \'{}\' is not valid email address!'.format(value))