Initial rule widget
This commit is contained in:
parent
ba2b5d0547
commit
9df0a86790
6 changed files with 34 additions and 81 deletions
|
|
@ -0,0 +1,10 @@
|
||||||
|
<input class="input category-form__input" type="checkbox"
|
||||||
|
name="rules" value="{{ rule.pk }}" checked{% endif %} />
|
||||||
|
|
||||||
|
{% if rule.favicon %}
|
||||||
|
<img class="favicon" src="{{ rule.favicon }}" />
|
||||||
|
{% else %}
|
||||||
|
<i class="gg-image"></i>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<span>{{ rule.name }}</span>
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
<ul class="list checkbox-list">
|
||||||
|
{% for rule in rules %}
|
||||||
|
<li class="list__item checkbox-list__item">
|
||||||
|
{% include "collection/widgets/rule.html" %}
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
6
src/newsreader/news/collection/widgets.py
Normal file
6
src/newsreader/news/collection/widgets.py
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
from django.forms.widgets import CheckboxSelectMultiple
|
||||||
|
|
||||||
|
|
||||||
|
class RulesWidget(CheckboxSelectMultiple):
|
||||||
|
template_name = "collection/widgets/rules.html"
|
||||||
|
option_template_name = "collection/widgets/rule.html"
|
||||||
|
|
@ -1,20 +1,13 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
|
|
||||||
from newsreader.accounts.models import User
|
|
||||||
from newsreader.news.collection.models import CollectionRule
|
from newsreader.news.collection.models import CollectionRule
|
||||||
|
from newsreader.news.collection.widgets import RulesWidget
|
||||||
from newsreader.news.core.models import Category
|
from newsreader.news.core.models import Category
|
||||||
|
|
||||||
|
|
||||||
class CategoryForm(forms.ModelForm):
|
class CategoryForm(forms.ModelForm):
|
||||||
rules = forms.ModelMultipleChoiceField(
|
rules = forms.ModelMultipleChoiceField(
|
||||||
required=False,
|
required=False, queryset=CollectionRule.objects.all(), widget=RulesWidget
|
||||||
queryset=CollectionRule.objects.all(),
|
|
||||||
widget=forms.widgets.CheckboxSelectMultiple,
|
|
||||||
)
|
|
||||||
|
|
||||||
user = forms.ModelChoiceField(
|
|
||||||
queryset=User.objects.none(),
|
|
||||||
widget=forms.widgets.HiddenInput(attrs={"readonly": True}),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
@ -23,12 +16,10 @@ class CategoryForm(forms.ModelForm):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.fields["rules"].queryset = CollectionRule.objects.filter(user=self.user)
|
self.fields["rules"].queryset = CollectionRule.objects.filter(user=self.user)
|
||||||
self.fields["user"].queryset = User.objects.filter(pk=self.user.pk)
|
|
||||||
|
|
||||||
self.initial["user"] = self.user
|
|
||||||
|
|
||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
instance = super().save(commit=False)
|
instance = super().save(commit=False)
|
||||||
|
instance.user = self.user
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
instance.save()
|
instance.save()
|
||||||
|
|
@ -41,4 +32,4 @@ class CategoryForm(forms.ModelForm):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Category
|
model = Category
|
||||||
fields = ("name", "rules", "user")
|
fields = ("name", "rules")
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
{% extends "core/category.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block form-header %}
|
{% load static %}
|
||||||
<h1 class="h1 form__title">Create a category</h1>
|
|
||||||
{% endblock %}
|
|
||||||
|
|
||||||
{% block confirm-button %}
|
{% block content %}
|
||||||
<button class="button button--confirm">Create category</button>
|
<main id="category--page" class="main">
|
||||||
|
{% url "news:core:categories" as cancel_url %}
|
||||||
|
{% include "form/form.html" with form=form title="Create category" cancel_url=cancel_url only %}
|
||||||
|
</main>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
|
|
@ -1,62 +0,0 @@
|
||||||
{% extends "base.html" %}
|
|
||||||
|
|
||||||
{% load static %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
<main id="category--page" class="main">
|
|
||||||
<form class="form category-form" method="post">
|
|
||||||
{% csrf_token %}
|
|
||||||
|
|
||||||
<div class="form__header">
|
|
||||||
{% block form-header %}{% endblock %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{{ form.non_field_errors }}
|
|
||||||
{{ form.user.errors }}
|
|
||||||
{{ form.user }}
|
|
||||||
|
|
||||||
<section class="section form__section category-form__section">
|
|
||||||
<fieldset class="form__fieldset category-form__fieldset">
|
|
||||||
<label class="label category-form__label" for="name">Name</label>
|
|
||||||
{{ form.name.errors }}
|
|
||||||
{{ form.name }}
|
|
||||||
</fieldset>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section class="section form__section category-form__section">
|
|
||||||
<fieldset class="form__fieldset category-form__fieldset">
|
|
||||||
<label class="label category-form__label" for="rules">Collection rules</label>
|
|
||||||
<small class="small help-text">
|
|
||||||
Note that existing assigned rules will be reassigned to this category
|
|
||||||
</small>
|
|
||||||
{{ form.rules.errors }}
|
|
||||||
|
|
||||||
<ul class="list checkbox-list">
|
|
||||||
{% for rule in rules %}
|
|
||||||
<li class="list__item checkbox-list__item">
|
|
||||||
<input class="input category-form__input" type="checkbox" name="rules"
|
|
||||||
{% if category and rule.pk in category.rule_ids %}checked{% endif %}
|
|
||||||
value="{{ rule.pk }}" />
|
|
||||||
|
|
||||||
{% if rule.favicon %}
|
|
||||||
<img class="favicon" src="{{ rule.favicon }}" />
|
|
||||||
{% else %}
|
|
||||||
<i class="gg-image"></i>
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
<span>{{ rule.name }}</span>
|
|
||||||
</li>
|
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
|
||||||
</fieldset>
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<section class="section form__section category-form__section">
|
|
||||||
<fieldset class="form__fieldset category-form__fieldset">
|
|
||||||
<a class="link button button--cancel" href="{% url 'news:core:categories' %}">Cancel</a>
|
|
||||||
{% block confirm-button %}{% endblock %}
|
|
||||||
</fieldset>
|
|
||||||
</section>
|
|
||||||
</form>
|
|
||||||
</main>
|
|
||||||
{% endblock %}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue