-
-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
455 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
max-line-length: 79 | ||
max-line-length: 119 | ||
uses: | ||
- django |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.utils import six | ||
from django.utils.encoding import force_str | ||
|
||
__all__ = ( | ||
'ReadOnlyFieldsMixin', | ||
'new_readonly_form' | ||
) | ||
|
||
|
||
class ReadOnlyFieldsMixin(object): | ||
readonly_fields = () | ||
all_fields = False | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(ReadOnlyFieldsMixin, self).__init__(*args, **kwargs) | ||
|
||
for field in (field for field_name, field in six.iteritems(self.fields) | ||
if field_name in self.readonly_fields | ||
or self.all_fields is True): | ||
field.widget.attrs['disabled'] = 'true' | ||
field.required = False | ||
|
||
def clean(self): | ||
cleaned_data = super(ReadOnlyFieldsMixin, self).clean() | ||
if self.all_fields: | ||
for field_name, field in six.iteritems(self.fields): | ||
cleaned_data[field_name] = getattr(self.instance, field_name) | ||
return cleaned_data | ||
else: | ||
for field_name in self.readonly_fields: | ||
cleaned_data[field_name] = getattr(self.instance, field_name) | ||
return cleaned_data | ||
|
||
|
||
def new_readonly_form(klass, all_fields=True, readonly_fields=()): | ||
name = force_str("ReadOnly{}".format(klass.__name__)) | ||
klass_fields = {'all_fields': all_fields, 'readonly_fields': readonly_fields} | ||
return type(name, (ReadOnlyFieldsMixin, klass), klass_fields) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django import forms | ||
|
||
from simple_history.forms import ReadOnlyFieldsMixin, new_readonly_form | ||
|
||
from .models import Poll | ||
|
||
|
||
class PollRevertForm(forms.ModelForm): | ||
class Meta: | ||
model = Poll | ||
fields = '__all__' | ||
|
||
|
||
class ReadOnlyPollRevertForm(ReadOnlyFieldsMixin, PollRevertForm): | ||
pass | ||
|
||
|
||
GeneratedByFunctionReadOnlyPollRevertForm = new_readonly_form(PollRevertForm, readonly_fields=()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from __future__ import unicode_literals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.core.urlresolvers import reverse_lazy | ||
from django.views import generic | ||
|
||
from simple_history.views import (HistoryRecordListViewMixin, | ||
RevertFromHistoryRecordViewMixin) | ||
|
||
from .forms import (GeneratedByFunctionReadOnlyPollRevertForm, PollRevertForm, | ||
ReadOnlyPollRevertForm) | ||
from .models import Place, Poll | ||
|
||
|
||
class PollListView(generic.ListView): | ||
model = Poll | ||
|
||
|
||
class PollHistoryVersionsView(HistoryRecordListViewMixin, generic.DetailView): | ||
model = Poll | ||
|
||
|
||
class PollHistoryVersions2(HistoryRecordListViewMixin, generic.UpdateView): | ||
model = Poll | ||
success_url = reverse_lazy('poll_list') | ||
|
||
|
||
class PollRevertView(RevertFromHistoryRecordViewMixin, generic.UpdateView): | ||
model = Poll | ||
fields = '__all__' | ||
success_url = reverse_lazy('poll_list') | ||
|
||
|
||
class PollRevertViewWithNormalForm(RevertFromHistoryRecordViewMixin, generic.UpdateView): | ||
model = Poll | ||
form_class = PollRevertForm | ||
success_url = reverse_lazy('poll_list') | ||
|
||
|
||
class PollRevertViewWithReadOnlyForm(RevertFromHistoryRecordViewMixin, generic.UpdateView): | ||
model = Poll | ||
form_class = ReadOnlyPollRevertForm | ||
success_url = reverse_lazy('poll_list') | ||
|
||
|
||
class PollRevertViewWithGeneratedByFunctionReadOnlyPollRevertForm(RevertFromHistoryRecordViewMixin, | ||
generic.UpdateView): | ||
model = Poll | ||
form_class = GeneratedByFunctionReadOnlyPollRevertForm | ||
success_url = reverse_lazy('poll_list') | ||
|
||
|
||
class PlaceListView(generic.ListView): | ||
model = Place | ||
|
||
|
||
class PlaceRevertViewWithMissingHistoryRecordsFieldException(RevertFromHistoryRecordViewMixin, | ||
generic.UpdateView): | ||
model = Place | ||
fields = '__all__' | ||
success_url = reverse_lazy('place_list') |
Oops, something went wrong.