From 7efd74cace744b346d65c57e8caf3db25e9acd61 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Sat, 31 Mar 2018 15:32:19 +0200 Subject: [PATCH] admin: add oauth2 models, improve search (fixes #22950) We also add default ordering on creation_date for most models. --- fargo/fargo/admin.py | 5 +-- fargo/fargo/migrations/0017_auto_20180331_1532.py | 19 ++++++++++++ fargo/fargo/models.py | 5 ++- fargo/locale/fr/LC_MESSAGES/django.po | 35 ++++++++++++++++++--- fargo/oauth2/admin.py | 36 +++++++++++++++++++++- fargo/oauth2/migrations/0005_auto_20180331_1532.py | 27 ++++++++++++++++ fargo/oauth2/models.py | 19 ++++++++++-- 7 files changed, 135 insertions(+), 11 deletions(-) create mode 100644 fargo/fargo/migrations/0017_auto_20180331_1532.py create mode 100644 fargo/oauth2/migrations/0005_auto_20180331_1532.py diff --git a/fargo/fargo/admin.py b/fargo/fargo/admin.py index 08c8f4a..1e939b0 100644 --- a/fargo/fargo/admin.py +++ b/fargo/fargo/admin.py @@ -8,10 +8,10 @@ from . import models class UserDocumentAdmin(admin.ModelAdmin): list_display = ['user', 'filename', 'thumbnail', 'created', 'origin'] - fields = ['user', 'filename', 'thumbnail', 'created', 'origin'] + fields = ['id', 'user', 'filename', 'thumbnail', 'created', 'origin'] readonly_fields = ['created', 'thumbnail'] search_fields = ['user__first_name', 'user__last_name', 'user__email', 'filename', - 'origin__label'] + 'origin__label', 'document__content_hash'] def thumbnail(self, instance): return instance.document.thumbnail_img_tag @@ -22,6 +22,7 @@ class DocumentAdmin(admin.ModelAdmin): fields = ['content_hash', 'thumbnail', 'users'] list_display = ['content_hash', 'thumbnail', 'users'] readonly_fields = ['thumbnail', 'users'] + search_fields = ['content_hash', 'content'] def users(self, instance): User = get_user_model() diff --git a/fargo/fargo/migrations/0017_auto_20180331_1532.py b/fargo/fargo/migrations/0017_auto_20180331_1532.py new file mode 100644 index 0000000..8840cc5 --- /dev/null +++ b/fargo/fargo/migrations/0017_auto_20180331_1532.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.11 on 2018-03-31 13:32 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('fargo', '0016_auto_20180330_2248'), + ] + + operations = [ + migrations.AlterModelOptions( + name='document', + options={'ordering': ('creation_date',), 'verbose_name': 'document', 'verbose_name_plural': 'documents'}, + ), + ] diff --git a/fargo/fargo/models.py b/fargo/fargo/models.py index 5a68d38..4f0ceee 100644 --- a/fargo/fargo/models.py +++ b/fargo/fargo/models.py @@ -212,10 +212,13 @@ class Document(models.Model): thumbnail.height, data_url) + def __unicode__(self): + return u'%s %s' % (os.path.basename(self.content.name), self.content_hash[:6]) + class Meta: verbose_name = _('document') verbose_name_plural = _('documents') - ordering = ('content_hash',) + ordering = ('creation_date',) @receiver(post_delete, sender=Document) diff --git a/fargo/locale/fr/LC_MESSAGES/django.po b/fargo/locale/fr/LC_MESSAGES/django.po index e4a1d6b..ea19ce9 100644 --- a/fargo/locale/fr/LC_MESSAGES/django.po +++ b/fargo/locale/fr/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: fargo 0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2018-03-31 15:53+0200\n" +"POT-Creation-Date: 2018-03-31 15:54+0200\n" "PO-Revision-Date: 2018-03-17 17:31+0100\n" "Last-Translator: Frederic Peters \n" "Language: French\n" @@ -16,7 +16,8 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n" -#: fargo/fargo/admin.py:18 fargo/fargo/admin.py:33 +#: fargo/fargo/admin.py:18 fargo/fargo/admin.py:34 fargo/oauth2/admin.py:39 +#: fargo/oauth2/admin.py:53 msgid "thumbnail" msgstr "vignette" @@ -80,7 +81,7 @@ msgstr "Slug" msgid "user" msgstr "utilisateur" -#: fargo/fargo/models.py:49 fargo/fargo/models.py:216 +#: fargo/fargo/models.py:49 fargo/fargo/models.py:219 msgid "document" msgstr "document" @@ -149,7 +150,7 @@ msgstr "créateur" msgid "%(label)s: %(value)s" msgstr "%(label)s : %(value)s" -#: fargo/fargo/models.py:217 +#: fargo/fargo/models.py:220 msgid "documents" msgstr "documents" @@ -178,10 +179,34 @@ msgstr "client_id/client_secret invalides." msgid "Document" msgstr "Document" -#: fargo/oauth2/models.py:54 +#: fargo/oauth2/models.py:52 msgid "redirect URIs" msgstr "URIs de redirection" +#: fargo/oauth2/models.py:71 +msgid "OAUTH2 client" +msgstr "" + +#: fargo/oauth2/models.py:72 +msgid "OAUTH2 clients" +msgstr "" + +#: fargo/oauth2/models.py:93 +msgid "OAUTH2 authorization" +msgstr "" + +#: fargo/oauth2/models.py:94 +msgid "OAUTH2 authorizations" +msgstr "" + +#: fargo/oauth2/models.py:121 +msgid "OAUTH2 temporary file" +msgstr "" + +#: fargo/oauth2/models.py:122 +msgid "OAUTH2 temporary files" +msgstr "" + #: fargo/oauth2/views.py:231 msgid "This document is already in your portfolio" msgstr "Ce document est déjà enregistré." diff --git a/fargo/oauth2/admin.py b/fargo/oauth2/admin.py index 9709d16..a14ee2f 100644 --- a/fargo/oauth2/admin.py +++ b/fargo/oauth2/admin.py @@ -15,13 +15,47 @@ # along with this program. If not, see . from django.contrib import admin +from django.utils.translation import ugettext_lazy as _ -from .models import OAuth2Client +from .models import OAuth2Client, OAuth2Authorize, OAuth2TempFile class OAuth2ClientAdmin(admin.ModelAdmin): fields = ('client_name', 'client_id', 'client_secret', 'redirect_uris') list_display = ['client_name', 'client_id', 'client_secret', 'redirect_uris'] +class OAuth2AuthorizeAdmin(admin.ModelAdmin): + list_display = ['id', 'client_name', 'user_document', 'thumbnail', + 'access_token', 'code', 'creation_date'] + raw_id_fields = ['user_document'] + search_fields = ['client__client_name', 'user_document__user__email', + 'user_document__user__first_name', + 'user_document__user__last_name', + 'user_document__filename', + 'user_document__user__contenat_has'] + + def thumbnail(self, instance): + return instance.user_document.document.thumbnail_img_tag + thumbnail.short_description = _('thumbnail') + + def client_name(self, instance): + return instance.client.client_name + + +class OAuth2TempFileAdmin(admin.ModelAdmin): + list_display = ['uuid', 'client_name', 'filename', 'thumbnail', 'creation_date'] + raw_id_fields = ['document'] + search_fields = ['filename', 'uuid', 'client__client_name'] + + + def thumbnail(self, instance): + return instance.document.thumbnail_img_tag + thumbnail.short_description = _('thumbnail') + + def client_name(self, instance): + return instance.client.client_name + admin.site.register(OAuth2Client, OAuth2ClientAdmin) +admin.site.register(OAuth2Authorize, OAuth2AuthorizeAdmin) +admin.site.register(OAuth2TempFile, OAuth2TempFileAdmin) diff --git a/fargo/oauth2/migrations/0005_auto_20180331_1532.py b/fargo/oauth2/migrations/0005_auto_20180331_1532.py new file mode 100644 index 0000000..fa91181 --- /dev/null +++ b/fargo/oauth2/migrations/0005_auto_20180331_1532.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.11 on 2018-03-31 13:32 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('oauth2', '0004_auto_20180326_1330'), + ] + + operations = [ + migrations.AlterModelOptions( + name='oauth2authorize', + options={'ordering': ('creation_date',), 'verbose_name': 'OAUTH2 authorization', 'verbose_name_plural': 'OAUTH2 authorizations'}, + ), + migrations.AlterModelOptions( + name='oauth2client', + options={'ordering': ('client_name',), 'verbose_name': 'OAUTH2 client', 'verbose_name_plural': 'OAUTH2 clients'}, + ), + migrations.AlterModelOptions( + name='oauth2tempfile', + options={'ordering': ('creation_date',), 'verbose_name': 'OAUTH2 temporary file', 'verbose_name_plural': 'OAUTH2 temporary files'}, + ), + ] diff --git a/fargo/oauth2/models.py b/fargo/oauth2/models.py index 6165ab4..afa3dc9 100644 --- a/fargo/oauth2/models.py +++ b/fargo/oauth2/models.py @@ -47,12 +47,12 @@ def validate_https_url(data): class OAuth2Client(models.Model): - client_secret = models.CharField(max_length=255, default=generate_uuid) - client_id = models.CharField(max_length=255, default=generate_uuid) client_name = models.CharField(max_length=255) redirect_uris = models.TextField( verbose_name=_('redirect URIs'), validators=[validate_https_url]) + client_id = models.CharField(max_length=255, default=generate_uuid) + client_secret = models.CharField(max_length=255, default=generate_uuid) def __repr__(self): return 'OAuth2Client name: %s with id: %s' % (self.client_name, self.client_id) @@ -66,6 +66,11 @@ class OAuth2Client(models.Model): def __unicode__(self): return self.client_name + class Meta: + ordering = ('client_name',) + verbose_name = _('OAUTH2 client') + verbose_name_plural = _('OAUTH2 clients') + class CleanupQuerySet(QuerySet): def cleanup(self, n=None): @@ -83,6 +88,11 @@ class OAuth2Authorize(models.Model): objects = CleanupQuerySet.as_manager() + class Meta: + ordering = ('creation_date',) + verbose_name = _('OAUTH2 authorization') + verbose_name_plural = _('OAUTH2 authorizations') + @classmethod def get_lifetime(cls): return max( @@ -105,3 +115,8 @@ class OAuth2TempFile(models.Model): @classmethod def get_lifetime(cls): return settings.FARGO_OAUTH2_TEMPFILE_LIFETIME + + class Meta: + ordering = ('creation_date',) + verbose_name = _('OAUTH2 temporary file') + verbose_name_plural = _('OAUTH2 temporary files') -- 2.16.3