admin.py 1.33 KB
Newer Older
Arnaud Fabre's avatar
Arnaud Fabre committed
1 2 3 4
# coding: utf-8
from __future__ import absolute_import

from django.contrib import admin
Arnaud Fabre's avatar
Arnaud Fabre committed
5 6
from django.core.urlresolvers import reverse

Jamesie Pic's avatar
Quality  
Jamesie Pic committed
7 8 9 10
from autocomplete_light import shortcuts as ac

from .models import MemopolDossier, Recommendation

Arnaud Fabre's avatar
Arnaud Fabre committed
11

Arnaud Fabre's avatar
Arnaud Fabre committed
12 13 14 15 16 17
def link_to_edit(obj, field):
    try:
        related_obj = getattr(obj, field)
        url = reverse(
            'admin:{}_{}_change'.format(
                related_obj._meta.app_label,
Jamesie Pic's avatar
Quality  
Jamesie Pic committed
18
                related_obj._meta.object_name.lower()
Arnaud Fabre's avatar
Arnaud Fabre committed
19 20 21 22
            ),
            args=(related_obj.pk,)

        )
Jamesie Pic's avatar
Quality  
Jamesie Pic committed
23 24
        return '&nbsp;<strong><a href="{url}">{obj}</a></strong>'.format(
            url=url, obj=related_obj)
25

Arnaud Fabre's avatar
Arnaud Fabre committed
26 27
    except:
        return '???'
28 29


Jamesie Pic's avatar
Quality  
Jamesie Pic committed
30
class MemopolDossierAdmin(admin.ModelAdmin):
31
    list_display = ('name', 'dossier_ptr')
Arnaud Fabre's avatar
Arnaud Fabre committed
32
    search_fields = ('name',)
33

34 35
    fields = ('dossier_ptr', 'name')
    readonly_fields = ('dossier_ptr',)
Arnaud Fabre's avatar
Arnaud Fabre committed
36

Arnaud Fabre's avatar
Arnaud Fabre committed
37 38

class RecommendationsAdmin(admin.ModelAdmin):
Jamesie Pic's avatar
Quality  
Jamesie Pic committed
39

Arnaud Fabre's avatar
Arnaud Fabre committed
40 41 42 43
    def link_to_proposal(self):
        return link_to_edit(self, 'proposal')
    link_to_proposal.allow_tags = True

Jamesie Pic's avatar
Quality  
Jamesie Pic committed
44
    list_display = ('id', 'title', 'proposal', 'recommendation', 'weight')
Arnaud Fabre's avatar
Arnaud Fabre committed
45
    search_fields = ('title', 'recommendation', 'proposal')
Jamesie Pic's avatar
Jamesie Pic committed
46
    form = ac.modelform_factory(Recommendation, exclude=[])
Arnaud Fabre's avatar
Arnaud Fabre committed
47

Arnaud Fabre's avatar
Arnaud Fabre committed
48
admin.site.register(MemopolDossier, MemopolDossierAdmin)
Arnaud Fabre's avatar
Arnaud Fabre committed
49
admin.site.register(Recommendation, RecommendationsAdmin)