Skip to content

Commit fa35063

Browse files
committed
adding new code examples
1 parent bc39be2 commit fa35063

11 files changed

+1299
-4
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
title: django.contrib.admin.helpers ActionForm code examples
2+
category: page
3+
slug: django-contrib-admin-helpers-actionform-examples
4+
sortorder: 500011020
5+
toc: False
6+
sidebartitle: django.contrib.admin.helpers ActionForm
7+
meta: Python example code for the ActionForm class from the django.contrib.admin.helpers module of the Django project.
8+
9+
10+
ActionForm is a class within the django.contrib.admin.helpers module of the Django project.
11+
12+
13+
## Example 1 from django-import-export
14+
[django-import-export](https://github.com/django-import-export/django-import-export)
15+
([documentation](https://django-import-export.readthedocs.io/en/latest/)
16+
and [PyPI page](https://pypi.org/project/django-import-export/))
17+
is a [Django](/django.html) code library for importing and exporting data
18+
from the Django Admin. The tool supports many export and import formats
19+
such as CSV, JSON and YAML. django-import-export is open source under the
20+
[BSD 2-Clause "Simplified" License](https://github.com/django-import-export/django-import-export/blob/master/LICENSE).
21+
22+
[**django-import-export / import_export / forms.py**](https://github.com/django-import-export/django-import-export/blob/master/import_export/./forms.py)
23+
24+
```python
25+
# forms.py
26+
import os.path
27+
28+
from django import forms
29+
~~from django.contrib.admin.helpers import ActionForm
30+
from django.utils.translation import gettext_lazy as _
31+
32+
33+
class ImportForm(forms.Form):
34+
import_file = forms.FileField(
35+
label=_('File to import')
36+
)
37+
input_format = forms.ChoiceField(
38+
label=_('Format'),
39+
choices=(),
40+
)
41+
42+
def __init__(self, import_formats, *args, **kwargs):
43+
super().__init__(*args, **kwargs)
44+
choices = []
45+
for i, f in enumerate(import_formats):
46+
choices.append((str(i), f().get_title(),))
47+
if len(import_formats) > 1:
48+
choices.insert(0, ('', '---'))
49+
50+
self.fields['input_format'].choices = choices
51+
52+
53+
class ConfirmImportForm(forms.Form):
54+
55+
56+
## ... source file abbreviated to get to ActionForm examples ...
57+
58+
59+
60+
61+
class ExportForm(forms.Form):
62+
file_format = forms.ChoiceField(
63+
label=_('Format'),
64+
choices=(),
65+
)
66+
67+
def __init__(self, formats, *args, **kwargs):
68+
super().__init__(*args, **kwargs)
69+
choices = []
70+
for i, f in enumerate(formats):
71+
choices.append((str(i), f().get_title(),))
72+
if len(formats) > 1:
73+
choices.insert(0, ('', '---'))
74+
75+
self.fields['file_format'].choices = choices
76+
77+
78+
def export_action_form_factory(formats):
79+
~~ class _ExportActionForm(ActionForm):
80+
file_format = forms.ChoiceField(
81+
label=_('Format'), choices=formats, required=False)
82+
_ExportActionForm.__name__ = str('ExportActionForm')
83+
84+
return _ExportActionForm
85+
86+
87+
88+
## ... source file continues with no further ActionForm examples...
89+
90+
```
91+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
title: django.contrib.admin.helpers AdminForm code examples
2+
category: page
3+
slug: django-contrib-admin-helpers-adminform-examples
4+
sortorder: 500011021
5+
toc: False
6+
sidebartitle: django.contrib.admin.helpers AdminForm
7+
meta: Python example code for the AdminForm class from the django.contrib.admin.helpers module of the Django project.
8+
9+
10+
AdminForm is a class within the django.contrib.admin.helpers module of the Django project.
11+
12+
13+
## Example 1 from django-cms
14+
[django-cms](https://github.com/divio/django-cms)
15+
([project website](https://www.django-cms.org/en/)) is a Python-based
16+
content management system (CMS) [library](https://pypi.org/project/django-cms/)
17+
for use with Django web apps that is open sourced under the
18+
[BSD 3-Clause "New"](https://github.com/divio/django-cms/blob/develop/LICENSE)
19+
license.
20+
21+
[**django-cms / cms / admin / placeholderadmin.py**](https://github.com/divio/django-cms/blob/develop/cms/admin/placeholderadmin.py)
22+
23+
```python
24+
# placeholderadmin.py
25+
import uuid
26+
import warnings
27+
28+
from django.conf.urls import url
29+
~~from django.contrib.admin.helpers import AdminForm
30+
from django.contrib.admin.utils import get_deleted_objects
31+
from django.core.exceptions import PermissionDenied
32+
from django.db import router, transaction
33+
from django.http import (
34+
HttpResponse,
35+
HttpResponseBadRequest,
36+
HttpResponseForbidden,
37+
HttpResponseNotFound,
38+
HttpResponseRedirect,
39+
)
40+
from django.shortcuts import get_list_or_404, get_object_or_404, render
41+
from django.template.response import TemplateResponse
42+
from django.utils.decorators import method_decorator
43+
from django.utils.encoding import force_text
44+
from django.utils import translation
45+
from django.utils.translation import ugettext as _
46+
from django.views.decorators.clickjacking import xframe_options_sameorigin
47+
from django.views.decorators.http import require_POST
48+
49+
from six.moves.urllib.parse import parse_qsl, urlparse
50+
51+
from six import get_unbound_function, get_method_function
52+
53+
from cms import operations
54+
55+
56+
## ... source file abbreviated to get to AdminForm examples ...
57+
58+
59+
context = {
60+
'opts': opts,
61+
'message': force_text(_("Field %s not found")) % raw_fields
62+
}
63+
return render(request, 'admin/cms/page/plugin/error_form.html', context)
64+
if not request.user.has_perm("{0}.change_{1}".format(self.model._meta.app_label,
65+
self.model._meta.model_name)):
66+
context = {
67+
'opts': opts,
68+
'message': force_text(_("You do not have permission to edit this item"))
69+
}
70+
return render(request, 'admin/cms/page/plugin/error_form.html', context)
71+
form_class = self.get_form(request, obj, fields=fields)
72+
if not cancel_clicked and request.method == 'POST':
73+
form = form_class(instance=obj, data=request.POST)
74+
if form.is_valid():
75+
form.save()
76+
saved_successfully = True
77+
else:
78+
form = form_class(instance=obj)
79+
~~ admin_form = AdminForm(form, fieldsets=[(None, {'fields': fields})], prepopulated_fields={},
80+
model_admin=self)
81+
media = self.media + admin_form.media
82+
context = {
83+
'CMS_MEDIA_URL': get_cms_setting('MEDIA_URL'),
84+
'title': opts.verbose_name,
85+
'plugin': None,
86+
'plugin_id': None,
87+
'adminform': admin_form,
88+
'add': False,
89+
'is_popup': True,
90+
'media': media,
91+
'opts': opts,
92+
'change': True,
93+
'save_as': False,
94+
'has_add_permission': False,
95+
'window_close_timeout': 10,
96+
}
97+
if cancel_clicked:
98+
context.update({
99+
'cancel': True,
100+
})
101+
return render(request, 'admin/cms/page/plugin/confirm_form.html', context)
102+
if not cancel_clicked and request.method == 'POST' and saved_successfully:
103+
return render(request, 'admin/cms/page/plugin/confirm_form.html', context)
104+
105+
106+
## ... source file continues with no further AdminForm examples...
107+
108+
```
109+

0 commit comments

Comments
 (0)