Commit ea3039d6 by Kálmán Viktor

Merge branch 'issue-282' into 'master'

Make request.user the initial owner in network.* forms

Closes #282
parents 1138a614 9a062596
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
from django.forms import ModelForm from django.forms import ModelForm
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext_lazy as _
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Div, Submit, BaseInput from crispy_forms.layout import Layout, Fieldset, Div, Submit, BaseInput
...@@ -56,8 +57,9 @@ class BlacklistItemForm(ModelForm): ...@@ -56,8 +57,9 @@ class BlacklistItemForm(ModelForm):
) )
), ),
FormActions( FormActions(
Submit('submit', 'Save changes'), Submit('submit', _('Save changes')),
LinkButton('back', 'Back', reverse_lazy('network.blacklist_list')) LinkButton('back', _("Back"),
reverse_lazy('network.blacklist_list'))
) )
) )
...@@ -77,8 +79,8 @@ class DomainForm(ModelForm): ...@@ -77,8 +79,8 @@ class DomainForm(ModelForm):
), ),
), ),
FormActions( FormActions(
Submit('submit', 'Save'), Submit('submit', _('Save')),
LinkButton('back', 'Back', reverse_lazy('network.domain_list')) LinkButton('back', _("Back"), reverse_lazy('network.domain_list'))
) )
) )
...@@ -91,15 +93,15 @@ class GroupForm(ModelForm): ...@@ -91,15 +93,15 @@ class GroupForm(ModelForm):
helper.layout = Layout( helper.layout = Layout(
Div( Div(
Fieldset( Fieldset(
'Identity', '',
'name', 'name',
'description', 'description',
'owner', 'owner',
), ),
), ),
FormActions( FormActions(
Submit('submit', 'Save'), Submit('submit', _('Save')),
LinkButton('back', 'Back', reverse_lazy('network.group_list')) LinkButton('back', _("Back"), reverse_lazy('network.group_list'))
) )
) )
...@@ -112,13 +114,13 @@ class HostForm(ModelForm): ...@@ -112,13 +114,13 @@ class HostForm(ModelForm):
helper.layout = Layout( helper.layout = Layout(
Div( Div(
Fieldset( Fieldset(
'Identity', '',
'hostname', 'hostname',
'reverse', 'reverse',
'mac', 'mac',
), ),
Fieldset( Fieldset(
'Network', _('Network'),
'vlan', 'vlan',
'ipv4', 'ipv4',
'ipv6', 'ipv6',
...@@ -126,7 +128,7 @@ class HostForm(ModelForm): ...@@ -126,7 +128,7 @@ class HostForm(ModelForm):
'external_ipv4', 'external_ipv4',
), ),
Fieldset( Fieldset(
'Information', _('Information'),
'description', 'description',
'location', 'location',
'comment', 'comment',
...@@ -134,8 +136,8 @@ class HostForm(ModelForm): ...@@ -134,8 +136,8 @@ class HostForm(ModelForm):
), ),
), ),
FormActions( FormActions(
Submit('submit', 'Save'), Submit('submit', _('Save')),
LinkButton('back', 'Back', reverse_lazy('network.host_list'))) LinkButton('back', _('Back'), reverse_lazy('network.host_list')))
) )
class Meta: class Meta:
...@@ -159,8 +161,8 @@ class RecordForm(ModelForm): ...@@ -159,8 +161,8 @@ class RecordForm(ModelForm):
) )
), ),
FormActions( FormActions(
Submit('submit', 'Save'), Submit('submit', _("Save")),
LinkButton('back', 'Back', reverse_lazy('network.record_list')) LinkButton('back', _("Back"), reverse_lazy('network.record_list'))
) )
) )
...@@ -173,7 +175,7 @@ class RuleForm(ModelForm): ...@@ -173,7 +175,7 @@ class RuleForm(ModelForm):
helper.layout = Layout( helper.layout = Layout(
Div( Div(
Fieldset( Fieldset(
'Identity', '',
'direction', 'direction',
'description', 'description',
'foreign_network', 'foreign_network',
...@@ -189,7 +191,7 @@ class RuleForm(ModelForm): ...@@ -189,7 +191,7 @@ class RuleForm(ModelForm):
'nat_external_ipv4', 'nat_external_ipv4',
), ),
Fieldset( Fieldset(
'External', _('External'),
'vlan', 'vlan',
'vlangroup', 'vlangroup',
'host', 'host',
...@@ -198,8 +200,8 @@ class RuleForm(ModelForm): ...@@ -198,8 +200,8 @@ class RuleForm(ModelForm):
) )
), ),
FormActions( FormActions(
Submit('submit', 'Save'), Submit('submit', _("Save")),
LinkButton('back', 'Back', reverse_lazy('network.rule_list')) LinkButton('back', _("Back"), reverse_lazy('network.rule_list'))
) )
) )
...@@ -219,8 +221,8 @@ class SwitchPortForm(ModelForm): ...@@ -219,8 +221,8 @@ class SwitchPortForm(ModelForm):
) )
), ),
FormActions( FormActions(
Submit('submit', 'Save'), Submit('submit', _("Save")),
LinkButton('back', 'Back', LinkButton('back', _("Back"),
reverse_lazy('network.switch_port_list')) reverse_lazy('network.switch_port_list'))
) )
) )
...@@ -234,41 +236,42 @@ class VlanForm(ModelForm): ...@@ -234,41 +236,42 @@ class VlanForm(ModelForm):
helper.layout = Layout( helper.layout = Layout(
Div( Div(
Fieldset( Fieldset(
'Identity', '',
'name', 'name',
'vid', 'vid',
'network_type', 'network_type',
'managed', 'managed',
), ),
Fieldset( Fieldset(
'IPv4', _('IPv4'),
'network4', 'network4',
'snat_to', 'snat_to',
'snat_ip', 'snat_ip',
'dhcp_pool', 'dhcp_pool',
), ),
Fieldset( Fieldset(
'IPv6', _('IPv6'),
'network6', 'network6',
'ipv6_template', 'ipv6_template',
'host_ipv6_prefixlen', 'host_ipv6_prefixlen',
), ),
Fieldset( Fieldset(
'Domain name service', _('Domain name service'),
'domain', 'domain',
'reverse_domain', 'reverse_domain',
), ),
Fieldset( Fieldset(
'Info', _('Info'),
'description', 'description',
'comment', 'comment',
'owner',
# 'created_at', # 'created_at',
# 'modified_at', # 'modified_at',
), ),
), ),
FormActions( FormActions(
Submit('submit', 'Save'), Submit('submit', _("Save")),
LinkButton('back', 'Back', reverse_lazy('network.vlan_list')) LinkButton('back', _("Back"), reverse_lazy('network.vlan_list'))
) )
) )
...@@ -289,8 +292,8 @@ class VlanGroupForm(ModelForm): ...@@ -289,8 +292,8 @@ class VlanGroupForm(ModelForm):
) )
), ),
FormActions( FormActions(
Submit('submit', 'Save'), Submit('submit', _("Save")),
LinkButton('back', 'Back', reverse_lazy( LinkButton('back', _("Back"), reverse_lazy(
'network.vlan_group_list')) 'network.vlan_group_list'))
) )
) )
......
...@@ -34,6 +34,7 @@ from .forms import (HostForm, VlanForm, DomainForm, GroupForm, RecordForm, ...@@ -34,6 +34,7 @@ from .forms import (HostForm, VlanForm, DomainForm, GroupForm, RecordForm,
BlacklistItemForm, RuleForm, VlanGroupForm, SwitchPortForm) BlacklistItemForm, RuleForm, VlanGroupForm, SwitchPortForm)
from django.contrib import messages from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.edit import FormMixin from django.views.generic.edit import FormMixin
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from braces.views import LoginRequiredMixin, SuperuserRequiredMixin from braces.views import LoginRequiredMixin, SuperuserRequiredMixin
...@@ -45,22 +46,11 @@ from dashboard.views import AclUpdateView ...@@ -45,22 +46,11 @@ from dashboard.views import AclUpdateView
from dashboard.forms import AclUserOrGroupAddForm from dashboard.forms import AclUserOrGroupAddForm
class SuccessMessageMixin(FormMixin): class InitialOwnerMixin(FormMixin):
""" def get_initial(self):
Adds a success message on successful form submission. initial = super(InitialOwnerMixin, self).get_initial()
From django/contrib/messages/views.py@9a85ad89 initial['owner'] = self.request.user
""" return initial
success_message = ''
def form_valid(self, form):
response = super(SuccessMessageMixin, self).form_valid(form)
success_message = self.get_success_message(form.cleaned_data)
if success_message:
messages.success(self.request, success_message)
return response
def get_success_message(self, cleaned_data):
return self.success_message % cleaned_data
class IndexView(LoginRequiredMixin, SuperuserRequiredMixin, TemplateView): class IndexView(LoginRequiredMixin, SuperuserRequiredMixin, TemplateView):
...@@ -190,7 +180,7 @@ class DomainDetail(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -190,7 +180,7 @@ class DomainDetail(LoginRequiredMixin, SuperuserRequiredMixin,
class DomainCreate(LoginRequiredMixin, SuperuserRequiredMixin, class DomainCreate(LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, CreateView): SuccessMessageMixin, InitialOwnerMixin, CreateView):
model = Domain model = Domain
template_name = "network/domain-create.html" template_name = "network/domain-create.html"
form_class = DomainForm form_class = DomainForm
...@@ -274,7 +264,7 @@ class GroupList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView): ...@@ -274,7 +264,7 @@ class GroupList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView):
class GroupCreate(LoginRequiredMixin, SuperuserRequiredMixin, class GroupCreate(LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, CreateView): SuccessMessageMixin, InitialOwnerMixin, CreateView):
model = Group model = Group
template_name = "network/group-create.html" template_name = "network/group-create.html"
form_class = GroupForm form_class = GroupForm
...@@ -413,7 +403,7 @@ class HostDetail(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -413,7 +403,7 @@ class HostDetail(LoginRequiredMixin, SuperuserRequiredMixin,
class HostCreate(LoginRequiredMixin, SuperuserRequiredMixin, class HostCreate(LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, CreateView): SuccessMessageMixin, InitialOwnerMixin, CreateView):
model = Host model = Host
template_name = "network/host-create.html" template_name = "network/host-create.html"
form_class = HostForm form_class = HostForm
...@@ -497,7 +487,7 @@ class RecordDetail(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -497,7 +487,7 @@ class RecordDetail(LoginRequiredMixin, SuperuserRequiredMixin,
class RecordCreate(LoginRequiredMixin, SuperuserRequiredMixin, class RecordCreate(LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, CreateView): SuccessMessageMixin, InitialOwnerMixin, CreateView):
model = Record model = Record
template_name = "network/record-create.html" template_name = "network/record-create.html"
form_class = RecordForm form_class = RecordForm
...@@ -505,21 +495,21 @@ class RecordCreate(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -505,21 +495,21 @@ class RecordCreate(LoginRequiredMixin, SuperuserRequiredMixin,
success_message = _(u'Successfully created record!') success_message = _(u'Successfully created record!')
def get_initial(self): def get_initial(self):
initial = super(RecordCreate, self).get_initial()
initial['domain'] = self.request.GET.get('domain')
host_pk = self.request.GET.get("host") host_pk = self.request.GET.get("host")
try: try:
host = Host.objects.get(pk=host_pk) host = Host.objects.get(pk=host_pk)
except (Host.DoesNotExist, ValueError): except (Host.DoesNotExist, ValueError):
host = None host = None
initial = {'owner': self.request.user}
if host: if host:
initial.update({ initial.update({
'type': "CNAME", 'type': "CNAME",
'host': host, 'host': host,
'address': host.get_fqdn(), 'address': host.get_fqdn(),
}) })
else:
initial['domain'] = self.request.GET.get('domain')
return initial return initial
...@@ -569,18 +559,19 @@ class RuleDetail(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -569,18 +559,19 @@ class RuleDetail(LoginRequiredMixin, SuperuserRequiredMixin,
class RuleCreate(LoginRequiredMixin, SuperuserRequiredMixin, class RuleCreate(LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, CreateView): SuccessMessageMixin, InitialOwnerMixin, CreateView):
model = Rule model = Rule
template_name = "network/rule-create.html" template_name = "network/rule-create.html"
form_class = RuleForm form_class = RuleForm
success_message = _(u'Successfully created rule!') success_message = _(u'Successfully created rule!')
def get_initial(self): def get_initial(self):
return { initial = super(RuleCreate, self).get_initial()
# 'owner': 1, initial.update({
'host': self.request.GET.get('host'), 'host': self.request.GET.get('host'),
'hostgroup': self.request.GET.get('hostgroup') 'hostgroup': self.request.GET.get('hostgroup')
} })
return initial
class RuleDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView): class RuleDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView):
...@@ -680,7 +671,7 @@ class VlanDetail(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -680,7 +671,7 @@ class VlanDetail(LoginRequiredMixin, SuperuserRequiredMixin,
class VlanCreate(LoginRequiredMixin, SuperuserRequiredMixin, class VlanCreate(LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, CreateView): SuccessMessageMixin, InitialOwnerMixin, CreateView):
model = Vlan model = Vlan
template_name = "network/vlan-create.html" template_name = "network/vlan-create.html"
form_class = VlanForm form_class = VlanForm
...@@ -760,7 +751,7 @@ class VlanGroupDetail(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -760,7 +751,7 @@ class VlanGroupDetail(LoginRequiredMixin, SuperuserRequiredMixin,
class VlanGroupCreate(LoginRequiredMixin, SuperuserRequiredMixin, class VlanGroupCreate(LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, CreateView): SuccessMessageMixin, InitialOwnerMixin, CreateView):
model = VlanGroup model = VlanGroup
template_name = "network/vlan-group-create.html" template_name = "network/vlan-group-create.html"
form_class = VlanGroupForm form_class = VlanGroupForm
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment