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

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
from django.core.urlresolvers import reverse

8
from .admin_views import import_vote_with_recommendation, import_vote
Arnaud Fabre's avatar
Arnaud Fabre committed
9
from .models import Recommendation, MemopolDossier
Arnaud Fabre's avatar
Arnaud Fabre committed
10 11 12 13

admin.site.register_view('import_vote', view=import_vote)
admin.site.register_view('import_vote_with_recommendation', view=import_vote_with_recommendation)

Arnaud Fabre's avatar
Arnaud Fabre committed
14 15 16 17 18 19 20 21 22 23 24 25
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)
26

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

Arnaud Fabre's avatar
Arnaud Fabre committed
30
class MemopolDossierAdmin(admin.ModelAdmin):
31

32
    list_display = ('name', 'dossier_ptr')
Arnaud Fabre's avatar
Arnaud Fabre committed
33
    search_fields = ('name',)
34

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

Arnaud Fabre's avatar
Arnaud Fabre committed
38 39

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

    list_display = ('id', 'title', link_to_proposal, 'recommendation','weight')
Arnaud Fabre's avatar
Arnaud Fabre committed
45
    search_fields = ('title', 'recommendation', 'proposal')
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)