def post(self, request, *args, **kwargs): """ Save user color theme selection """ form = self.get_form() # Get current user theme user_theme = self.get_user_theme() # Create theme entry if user did not select one yet if not user_theme: user_theme = ColorTheme() user_theme.user = request.user if form.is_valid(): theme_selected = form.cleaned_data['name'] # Set color theme to form selection user_theme.name = theme_selected user_theme.save() return self.form_valid(form) else: # Set color theme to default user_theme.name = ColorTheme.default_color_theme[0] user_theme.save() return self.form_invalid(form)
def get_available_themes(*args, **kwargs): """Return the available theme choices.""" themes = [] for key, name in ColorTheme.get_color_themes_choices(): themes.append({'key': key, 'name': name}) return themes
def get_user_color_theme(username): """ Get current user color theme """ try: user_theme = ColorTheme.objects.filter(user=username).get() user_theme_name = user_theme.name if not user_theme_name or not ColorTheme.is_valid_choice(user_theme): user_theme_name = 'default' except ColorTheme.DoesNotExist: user_theme_name = 'default' return user_theme_name
def get_color_theme_css(username): try: user_theme = ColorTheme.objects.filter(user=username).get() user_theme_name = user_theme.name if not user_theme_name or not ColorTheme.is_valid_choice(user_theme): user_theme_name = 'default' except ColorTheme.DoesNotExist: user_theme_name = 'default' # Build path to CSS sheet inventree_css_sheet = os.path.join('css', 'color-themes', user_theme_name + '.css') # Build static URL inventree_css_static_url = os.path.join(settings.STATIC_URL, inventree_css_sheet) return inventree_css_static_url
def get(self, request, *args, **kwargs): """ Check if current color theme exists, else display alert box """ context = {} form = self.get_form() context['form'] = form user_theme = self.get_user_theme() if user_theme: # Check color theme is a valid choice if not ColorTheme.is_valid_choice(user_theme): user_color_theme_name = user_theme.name if not user_color_theme_name: user_color_theme_name = 'default' context['invalid_color_theme'] = user_color_theme_name return self.render_to_response(context)
def __init__(self, *args, **kwargs): super(ColorThemeSelectForm, self).__init__(*args, **kwargs) # Populate color themes choices self.fields['name'].choices = ColorTheme.get_color_themes_choices() self.helper = FormHelper() # Form rendering self.helper.form_show_labels = False self.helper.layout = Layout( Div( Div(Field('name'), css_class='col-sm-6', style='width: 200px;'), Div(StrictButton(_('Apply Theme'), css_class='btn btn-primary', type='submit'), css_class='col-sm-6', style='width: auto;'), css_class='row', ), )
def post(self, request, *args, **kwargs): """Save user color theme selection.""" theme = request.POST.get('theme', None) # Get current user theme user_theme = self.get_user_theme() # Create theme entry if user did not select one yet if not user_theme: user_theme = ColorTheme() user_theme.user = request.user user_theme.name = theme user_theme.save() return redirect(reverse_lazy('settings'))