Forms in the create view
This commit is contained in:
@ -5,17 +5,42 @@ from django.conf import settings
|
|||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from wagtail.wagtailcore.models import Page
|
from wagtail.wagtailcore.models import Page
|
||||||
|
|
||||||
from personalisation.models import Segment
|
from wagtail.wagtailadmin.forms import WagtailAdminModelForm
|
||||||
|
|
||||||
|
from personalisation.models import (ReferralRule, Segment, TimeRule,
|
||||||
|
VisitCountRule)
|
||||||
|
|
||||||
|
|
||||||
class SegmentForm(forms.ModelForm):
|
class SegmentForm(forms.ModelForm):
|
||||||
"""
|
"""Custom Segment form for the create view."""
|
||||||
Custom Segment form for the create view
|
|
||||||
"""
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
"""Why does this need a docstring? I do not know."""
|
||||||
model = Segment
|
model = Segment
|
||||||
fields = (
|
fields = (
|
||||||
'name',
|
'name',
|
||||||
'status',
|
'status',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
class TimeRuleForm(WagtailAdminModelForm):
|
||||||
|
"""Create a form for the time rule model."""
|
||||||
|
title = "Time"
|
||||||
|
description = "Choose a time segment in which the user visits the site."
|
||||||
|
class Meta:
|
||||||
|
model = TimeRule
|
||||||
|
fields = ['start_time', 'end_time']
|
||||||
|
|
||||||
|
class ReferralRuleForm(WagtailAdminModelForm):
|
||||||
|
"""Create a form for the referral rule model."""
|
||||||
|
title = "Referrer"
|
||||||
|
description = "Define a referring page, domain or query the user has to come from."
|
||||||
|
class Meta:
|
||||||
|
model = ReferralRule
|
||||||
|
fields = ['regex_string']
|
||||||
|
|
||||||
|
class VisitCountRuleForm(WagtailAdminModelForm):
|
||||||
|
"""Create a form for the visit count rule model."""
|
||||||
|
title = "Visit count"
|
||||||
|
description = "Choose the number of visits the user has to have made."
|
||||||
|
class Meta:
|
||||||
|
model = VisitCountRule
|
||||||
|
fields = ['operator', 'count']
|
||||||
|
@ -7,18 +7,26 @@
|
|||||||
{% include "wagtailadmin/shared/header.html" with title=view.get_page_title subtitle=view.get_page_subtitle icon=view.header_icon tabbed=1 merged=1 %}
|
{% include "wagtailadmin/shared/header.html" with title=view.get_page_title subtitle=view.get_page_subtitle icon=view.header_icon tabbed=1 merged=1 %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
||||||
{{ modal_content }}
|
|
||||||
|
|
||||||
<form action="{% block form_action %}{{ view.create_url }}{% endblock %}" {% if is_multipart %} enctype="multipart/form-data"
|
<form action="{% block form_action %}{{ view.create_url }}{% endblock %}" {% if is_multipart %} enctype="multipart/form-data"
|
||||||
{% endif %} method="POST" novalidate>
|
{% endif %} method="POST" novalidate>
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
|
||||||
{% block form %}
|
{% block form %}
|
||||||
{{ edit_handler.render_form_content }}
|
{{ edit_handler.render_form_content }}
|
||||||
{% endblock %}
|
|
||||||
|
|
||||||
<ul class="objects">
|
{% for form in additional_forms %}
|
||||||
|
<li class="object">
|
||||||
|
<h2>
|
||||||
|
<label for="{{ form.title }}">{{ form.title }}</label>
|
||||||
|
</h2>
|
||||||
|
<fieldset class="">
|
||||||
|
<p>{{ form.description }}</p>
|
||||||
|
<legend>{{ form.title }}</legend>
|
||||||
|
{{ form }}
|
||||||
|
</fieldset>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
<li class="object">
|
<li class="object">
|
||||||
<h2>
|
<h2>
|
||||||
<label for="id_rules">Rules</label>
|
<label for="id_rules">Rules</label>
|
||||||
@ -57,9 +65,8 @@
|
|||||||
|
|
||||||
<h2>Your chosen rules</h2>
|
<h2>Your chosen rules</h2>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
{% endblock %}
|
||||||
|
|
||||||
{% block footer %}
|
{% block footer %}
|
||||||
<footer>
|
<footer>
|
||||||
|
@ -10,8 +10,14 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="nice-padding">
|
<div class="nice-padding">
|
||||||
<form>
|
<form class="embed-form" action="" method="POST" novalidate>
|
||||||
{{ form_fields }}
|
{% csrf_token %}
|
||||||
|
<ul class="fields">
|
||||||
|
{% for field in form %}
|
||||||
|
{% include "wagtailadmin/shared/field_as_li.html" with field=field %}
|
||||||
|
{% endfor %}
|
||||||
|
<li><button type="submit" class="button button-longrunning"><span class="icon icon-spinner"></span><em>{% trans 'Insert' %}</em></button></li>
|
||||||
|
</ul>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -10,8 +10,14 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="nice-padding">
|
<div class="nice-padding">
|
||||||
<form>
|
<form class="embed-form" action="" method="POST" novalidate>
|
||||||
{{ form_fields }}
|
{% csrf_token %}
|
||||||
|
<ul class="fields">
|
||||||
|
{% for field in form %}
|
||||||
|
{% include "wagtailadmin/shared/field_as_li.html" with field=field %}
|
||||||
|
{% endfor %}
|
||||||
|
<li><button type="submit" class="button button-longrunning"><span class="icon icon-spinner"></span><em>{% trans 'Insert' %}</em></button></li>
|
||||||
|
</ul>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -10,8 +10,14 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="nice-padding">
|
<div class="nice-padding">
|
||||||
<form>
|
<form class="embed-form" action="" method="POST" novalidate>
|
||||||
{{ form_fields }}
|
{% csrf_token %}
|
||||||
|
<ul class="fields">
|
||||||
|
{% for field in form %}
|
||||||
|
{% include "wagtailadmin/shared/field_as_li.html" with field=field %}
|
||||||
|
{% endfor %}
|
||||||
|
<li><button type="submit" class="button button-longrunning"><span class="icon icon-spinner"></span><em>{% trans 'Insert' %}</em></button></li>
|
||||||
|
</ul>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
@ -5,13 +5,12 @@ from django.http import HttpResponse, HttpResponseRedirect
|
|||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from wagtail.contrib.modeladmin.views import CreateView
|
from wagtail.contrib.modeladmin.views import CreateView
|
||||||
|
|
||||||
from personalisation.models import (
|
from personalisation.forms import (ReferralRuleForm, SegmentForm, TimeRuleForm,
|
||||||
ReferralRule, Segment, TimeRule, VisitCountRule)
|
VisitCountRuleForm)
|
||||||
from personalisation.models import Segment
|
from personalisation.models import (ReferralRule, Segment, TimeRule,
|
||||||
from personalisation.forms import SegmentForm
|
VisitCountRule)
|
||||||
|
|
||||||
|
|
||||||
def overview(request):
|
def overview(request):
|
||||||
@ -37,40 +36,22 @@ def disable(request, segment_id):
|
|||||||
# TODO: Make these requestable from an exsisting page (the create page.)
|
# TODO: Make these requestable from an exsisting page (the create page.)
|
||||||
# This code might become obselete.
|
# This code might become obselete.
|
||||||
|
|
||||||
class TimeRuleForm(ModelForm):
|
|
||||||
"""Create a form for the time rule model."""
|
|
||||||
class Meta:
|
|
||||||
model = TimeRule
|
|
||||||
fields = ['start_time', 'end_time']
|
|
||||||
|
|
||||||
def time_rule_embed(request):
|
def time_rule_embed(request):
|
||||||
"""Show the content of the time rule modal."""
|
"""Show the content of the time rule modal."""
|
||||||
return render(request, 'wagtailadmin/embeds/time_rule.html', {
|
return render(request, 'wagtailadmin/embeds/time_rule.html', {
|
||||||
'form_fields': TimeRuleForm(),
|
'form': TimeRuleForm,
|
||||||
})
|
})
|
||||||
|
|
||||||
class ReferralRuleForm(ModelForm):
|
|
||||||
"""Create a form for the referral rule model."""
|
|
||||||
class Meta:
|
|
||||||
model = ReferralRule
|
|
||||||
fields = ['regex_string']
|
|
||||||
|
|
||||||
def referral_rule_embed(request):
|
def referral_rule_embed(request):
|
||||||
"""Show the content of the referral rule modal."""
|
"""Show the content of the referral rule modal."""
|
||||||
return render(request, 'wagtailadmin/embeds/referral_rule.html', {
|
return render(request, 'wagtailadmin/embeds/referral_rule.html', {
|
||||||
'form_fields': ReferralRuleForm,
|
'form': ReferralRuleForm,
|
||||||
})
|
})
|
||||||
|
|
||||||
class VisitCountRuleForm(ModelForm):
|
|
||||||
"""Create a form for the visit count rule model."""
|
|
||||||
class Meta:
|
|
||||||
model = VisitCountRule
|
|
||||||
fields = ['operator', 'count']
|
|
||||||
|
|
||||||
def visit_c_rule_embed(request):
|
def visit_c_rule_embed(request):
|
||||||
"""Show the content of the visit count rule modal."""
|
"""Show the content of the visit count rule modal."""
|
||||||
return render(request, 'wagtailadmin/embeds/visit_count_rule.html', {
|
return render(request, 'wagtailadmin/embeds/visit_count_rule.html', {
|
||||||
'form_fields': VisitCountRule,
|
'form': VisitCountRuleForm,
|
||||||
})
|
})
|
||||||
|
|
||||||
class CreateSegmentView(CreateView):
|
class CreateSegmentView(CreateView):
|
||||||
|
@ -2,9 +2,12 @@ import time
|
|||||||
|
|
||||||
from django.conf.urls import include, url
|
from django.conf.urls import include, url
|
||||||
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
|
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
|
||||||
|
from wagtail.contrib.modeladmin.views import CreateView
|
||||||
from wagtail.wagtailcore import hooks
|
from wagtail.wagtailcore import hooks
|
||||||
|
|
||||||
from personalisation import admin_urls
|
from personalisation import admin_urls
|
||||||
|
from personalisation.forms import (ReferralRuleForm, TimeRuleForm,
|
||||||
|
VisitCountRuleForm)
|
||||||
from personalisation.models import Segment
|
from personalisation.models import Segment
|
||||||
|
|
||||||
|
|
||||||
@ -18,6 +21,14 @@ def register_admin_urls():
|
|||||||
namespace='personalisation')),
|
namespace='personalisation')),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
class SegmentCreateView(CreateView):
|
||||||
|
def get_context_data(self, **kwargs):
|
||||||
|
context = {
|
||||||
|
'additional_forms': (TimeRuleForm, ReferralRuleForm, VisitCountRuleForm),
|
||||||
|
}
|
||||||
|
context.update(kwargs)
|
||||||
|
return super(SegmentCreateView, self).get_context_data(**context)
|
||||||
|
|
||||||
|
|
||||||
class SegmentModelAdmin(ModelAdmin):
|
class SegmentModelAdmin(ModelAdmin):
|
||||||
"""The base model for the Segments administration interface."""
|
"""The base model for the Segments administration interface."""
|
||||||
@ -25,6 +36,7 @@ class SegmentModelAdmin(ModelAdmin):
|
|||||||
menu_icon = 'group'
|
menu_icon = 'group'
|
||||||
add_to_settings_menu = False
|
add_to_settings_menu = False
|
||||||
list_display = ('status', 'name', 'create_date', 'edit_date')
|
list_display = ('status', 'name', 'create_date', 'edit_date')
|
||||||
|
create_view_class = SegmentCreateView
|
||||||
index_view_extra_css = ['personalisation/segment/index.css']
|
index_view_extra_css = ['personalisation/segment/index.css']
|
||||||
form_view_extra_css = ['personalisation/segment/form.css']
|
form_view_extra_css = ['personalisation/segment/form.css']
|
||||||
inspect_view_enabled = True
|
inspect_view_enabled = True
|
||||||
|
Reference in New Issue
Block a user