Exemplo n.º 1
0
def settings_value(key, *args, **kwargs):
    """Return a settings value specified by the given key."""
    if 'user' in kwargs:
        if not kwargs['user'] or (kwargs['user'] and
                                  kwargs['user'].is_authenticated is False):
            return InvenTreeUserSetting.get_setting(key)
        return InvenTreeUserSetting.get_setting(key, user=kwargs['user'])

    return InvenTreeSetting.get_setting(key)
Exemplo n.º 2
0
def settings_value(key, *args, **kwargs):
    """
    Return a settings value specified by the given key
    """

    if 'user' in kwargs:
        return InvenTreeUserSetting.get_setting(key, user=kwargs['user'])

    return InvenTreeSetting.get_setting(key)
Exemplo n.º 3
0
def render_date(context, date_object):
    """
    Renders a date according to the preference of the provided user

    Note that the user preference is stored using the formatting adopted by moment.js,
    which differs from the python formatting!
    """

    if date_object is None:
        return None

    if type(date_object) == str:

        date_object = date_object.strip()

        # Check for empty string
        if len(date_object) == 0:
            return None

        # If a string is passed, first convert it to a datetime
        try:
            date_object = date.fromisoformat(date_object)
        except ValueError:
            logger.warning(f"Tried to convert invalid date string: {date_object}")
            return None

    # We may have already pre-cached the date format by calling this already!
    user_date_format = context.get('user_date_format', None)

    if user_date_format is None:

        user = context.get('user', None)

        if user and user.is_authenticated:
            # User is specified - look for their date display preference
            user_date_format = InvenTreeUserSetting.get_setting('DATE_DISPLAY_FORMAT', user=user)
        else:
            user_date_format = 'YYYY-MM-DD'

        # Convert the format string to Pythonic equivalent
        replacements = [
            ('YYYY', '%Y'),
            ('MMM', '%b'),
            ('MM', '%m'),
            ('DD', '%d'),
        ]

        for o, n in replacements:
            user_date_format = user_date_format.replace(o, n)

        # Update the context cache
        context['user_date_format'] = user_date_format

    if isinstance(date_object, (datetime, date)):
        return date_object.strftime(user_date_format)
    return date_object
Exemplo n.º 4
0
def setting_object(key, *args, **kwargs):
    """
    Return a setting object speciifed by the given key
    (Or return None if the setting does not exist)
    if a user-setting was requested return that
    """

    if 'user' in kwargs:
        return InvenTreeUserSetting.get_setting_object(key,
                                                       user=kwargs['user'])
    return InvenTreeSetting.get_setting_object(key)
Exemplo n.º 5
0
def setting_object(key, *args, **kwargs):
    """
    Return a setting object speciifed by the given key
    (Or return None if the setting does not exist)
    if a user-setting was requested return that
    """

    if 'plugin' in kwargs:
        # Note, 'plugin' is an instance of an InvenTreePlugin class

        plugin = kwargs['plugin']

        return PluginSetting.get_setting_object(key, plugin=plugin)

    if 'method' in kwargs:
        return NotificationUserSetting.get_setting_object(key, user=kwargs['user'], method=kwargs['method'])

    if 'user' in kwargs:
        return InvenTreeUserSetting.get_setting_object(key, user=kwargs['user'])

    return InvenTreeSetting.get_setting_object(key)
Exemplo n.º 6
0
    def test_print(self):
        """
        Printing tests for the BuildReport
        """

        report = self.model.objects.first()

        url = reverse(self.print_url, kwargs={'pk': report.pk})

        # Try to print without providing a valid BuildOrder
        response = self.get(url, expected_code=400)

        # Try to print with an invalid BuildOrder
        response = self.get(url, {'build': 9999}, expected_code=400)

        # Now print with a valid BuildOrder

        build = Build.objects.first()

        response = self.get(url, {'build': build.pk})

        self.assertEqual(type(response), StreamingHttpResponse)

        headers = response.headers

        self.assertEqual(headers['Content-Type'], 'application/pdf')
        self.assertEqual(headers['Content-Disposition'],
                         'attachment; filename="report.pdf"')

        # Now, set the download type to be "inline"
        inline = InvenTreeUserSetting.get_setting_object(
            'REPORT_INLINE', self.user)
        inline.value = True
        inline.save()

        response = self.get(url, {'build': 1})
        headers = response.headers
        self.assertEqual(headers['Content-Type'], 'application/pdf')
        self.assertEqual(headers['Content-Disposition'],
                         'inline; filename="report.pdf"')
Exemplo n.º 7
0
def user_settings(user, *args, **kwargs):
    """
    Return all USER settings as a key:value dict
    """

    return InvenTreeUserSetting.allValues(user=user)