Created
December 30, 2020 07:48
-
-
Save LowerDeez/c574f8985a9a37cd21b563ef11e025f5 to your computer and use it in GitHub Desktop.
Django. Custom views for admin 2
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
from django import forms | |
from django.db.models import FilteredRelation, Q | |
from django.urls import path | |
from django.contrib import admin | |
from django.contrib.auth import get_user_model | |
from django.http import HttpResponseRedirect | |
from django.template.response import TemplateResponse | |
from django.urls import reverse | |
from django.utils.http import urlencode | |
from django.utils.translation import ugettext_lazy as _ | |
from import_export.admin import ExportActionMixin | |
from import_export.formats.base_formats import CSV, XLSX, XLS, JSON | |
from apps.accounts.admin import UserAdmin | |
from apps.accounts.tasks import import_users_to_esputnik | |
from apps.esputnik.models import UserForImport | |
from apps.esputnik.resources import UserExportResource | |
from apps.orders.const import DELIVERED_ORDER | |
from shared.utils import admin_changelist_url | |
User = get_user_model() | |
class ESputnikImportForm(forms.Form): | |
users = forms.ModelMultipleChoiceField( | |
queryset=User.objects.all(), | |
widget=forms.HiddenInput(), | |
required=False | |
) | |
title = forms.CharField(max_length=512, label=_('Название группы')) | |
# any other fields | |
def make_import(self): | |
users = self.cleaned_data['users'] | |
title = self.cleaned_data['title'] | |
import_users_to_esputnik.delay(list(users.values_list('id', flat=True)), title) | |
def esputnik_import(modeladmin, request, queryset): | |
data = {'ids': list(queryset.values_list('id', flat=True))} | |
url = reverse('admin:esputnik-import') | |
return HttpResponseRedirect( | |
f"{url}?{urlencode(data, True)}" | |
) | |
esputnik_import.short_description = _('Esputnik import') | |
@admin.register(UserForImport) | |
class ImportUserAdmin(ExportActionMixin): | |
# other admin staff | |
actions = [esputnik_import] | |
def get_urls(self): | |
urls = super().get_urls() | |
# register view | |
custom_urls = [ | |
path( | |
'esputnik/import/', | |
self.admin_site.admin_view(self.esputnik_import), | |
name='esputnik-import', | |
) | |
] | |
return custom_urls + urls | |
def esputnik_import(self, request, **kwargs): | |
if request.method != 'POST': | |
form = ESputnikImportForm() | |
else: | |
data = request.POST.copy() | |
data['users'] = request.GET.getlist('ids', []) | |
form = ESputnikImportForm(data) | |
if form.is_valid(): | |
form.make_import() | |
self.message_user(request, _('Success. Users are imported.')) | |
return HttpResponseRedirect(admin_changelist_url(UserForImport)) | |
context = self.admin_site.each_context(request) | |
context['opts'] = self.model._meta | |
context['form'] = form | |
context['title'] = _('E-Sputnik import') | |
return TemplateResponse( | |
request, | |
'admin/custom_action_form.html', | |
context, | |
) |
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
{% extends "admin/change_form.html" %} | |
{% load i18n admin_static admin_modify %} | |
{% block content %} | |
<div id="content-main"> | |
<form action="" method="POST"> | |
{% csrf_token %} | |
{% if form.non_field_errors|length > 0 %} | |
<p class="errornote"> | |
"Please correct the errors below." | |
</p> | |
{{ form.non_field_errors }} | |
{% endif %} | |
{% for hidden_field in form.hidden_fields %} | |
{% if hidden_field.errors %} | |
<ul> | |
{% for error in hidden_field.errors %} | |
<li>(Hidden field {{ hidden_field.name }}) {{ error }}</li> | |
{% endfor %} | |
</ul> | |
{% endif %} | |
{{ hidden_field }} | |
{% endfor %} | |
<fieldset class="module aligned"> | |
{% for field in form.visible_fields %} | |
<div class="form-row"> | |
{{ field.errors }} | |
{{ field.label_tag }} | |
{{ field }} | |
{% if field.field.help_text %} | |
<p class="help"> | |
{{ field.field.help_text|safe }} | |
</p> | |
{% endif %} | |
</div> | |
{% endfor %} | |
</fieldset> | |
<div class="submit-row"> | |
<input type="submit" class="default" value="Submit"> | |
</div> | |
</form> | |
</div> | |
{% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment