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

Jamesie Pic's avatar
Jamesie Pic committed
4
from autocomplete_light import shortcuts as ac
Arnaud Fabre's avatar
Arnaud Fabre committed
5
from django.contrib import admin
Arnaud Fabre's avatar
Arnaud Fabre committed
6 7 8
from django.core.urlresolvers import reverse

from .models import Recommendation, MemopolDossier
Arnaud Fabre's avatar
Arnaud Fabre committed
9

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

        )
        return '&nbsp;<strong><a href="{url}">{obj}</a></strong>'.format(url=url,obj=related_obj)
22

Arnaud Fabre's avatar
Arnaud Fabre committed
23 24
    except:
        return '???'
25

Arnaud Fabre's avatar
Arnaud Fabre committed
26
class MemopolDossierAdmin(admin.ModelAdmin):
27

28
    list_display = ('name', 'dossier_ptr')
Arnaud Fabre's avatar
Arnaud Fabre committed
29
    search_fields = ('name',)
30

31 32
    fields = ('dossier_ptr', 'name')
    readonly_fields = ('dossier_ptr',)
Arnaud Fabre's avatar
Arnaud Fabre committed
33

Arnaud Fabre's avatar
Arnaud Fabre committed
34 35

class RecommendationsAdmin(admin.ModelAdmin):
Arnaud Fabre's avatar
Arnaud Fabre committed
36 37 38 39
    def link_to_proposal(self):
        return link_to_edit(self, 'proposal')
    link_to_proposal.allow_tags = True

Jamesie Pic's avatar
Jamesie Pic committed
40
    list_display = ('id', 'title', 'proposal', 'recommendation','weight')
Arnaud Fabre's avatar
Arnaud Fabre committed
41
    search_fields = ('title', 'recommendation', 'proposal')
Jamesie Pic's avatar
Jamesie Pic committed
42
    form = ac.modelform_factory(Recommendation, exclude=[])
Arnaud Fabre's avatar
Arnaud Fabre committed
43

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