aboutsummaryrefslogtreecommitdiff
path: root/stream/admin.py
blob: e8d4ec55fa7f46241f5d736f460f4d9d5143eba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#  gLifestream Copyright (C) 2009, 2010 Wojciech Polak
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 3 of the License, or (at your
#  option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program.  If not, see <http://www.gnu.org/licenses/>.

from django.conf import settings
from django import forms
from django.contrib import admin
from django.contrib.admin.widgets import AdminFileWidget
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from models import Service, Entry, Media, Favorite, List
from glifestream.stream import media


def deactivate(modeladmin, request, queryset):
    queryset.update(active=False)
deactivate.short_description = _("Deactivate item")


def activate(modeladmin, request, queryset):
    queryset.update(active=True)
activate.short_description = _("Activate item")


def truncate_title(self):
    return self.title[0:70]


class ServiceAdmin (admin.ModelAdmin):
    list_display = ('name', 'api', 'url', 'last_modified', 'public',
                    'active', 'home')
    fieldsets = (
        (None, {'fields': ('api', 'name', 'url', 'creds', 'display', 'public',
                           'active', 'home')}),
        (_('Additional, optional fields'),
         {'classes': ('collapse',), 'fields': ('link', 'cls'),
          }),
        (_('Fields updated automatically by gLifestream'),
         {'classes': ('collapse',),
          'fields': ('etag', 'last_modified', 'last_checked'),
          })
    )
    search_fields = ['url', 'name']
    actions = [deactivate, activate]


class EntryAdmin (admin.ModelAdmin):
    list_display = (truncate_title, 'service', 'active',)
    list_filter = ('service',)
    search_fields = ['title', 'content']
    actions = [deactivate, activate]


class AdminImageWidget (AdminFileWidget):
    def render(self, name, value, attrs=None):
        output = []
        file_name = str(value)
        if file_name:
            file_path = '%s/%s' % (settings.MEDIA_URL, file_name)
            if 'thumbs/' in file_name:
                thumbnail = '<img src="%s" alt="[thumbnail]" />' % file_path
            else:
                thumbnail = ''
            output.append('<p><a target="_blank" href="%s">%s</a></p>%s <a target="_blank" href="%s">%s</a><br />%s ' %
                         (file_path, thumbnail, _('Currently:'), file_path, file_path, _('Change:')))
        output.append(super(
            AdminFileWidget, self).render(name, value, attrs))
        return mark_safe(''.join(output))


class MediaForm (forms.ModelForm):
    file = forms.FileField(widget=AdminImageWidget)

    class Meta:
        model = Media


class MediaAdmin (admin.ModelAdmin):
    list_display = ('entry', 'file',)
    raw_id_fields = ('entry',)
    form = MediaForm


class FavoriteAdmin (admin.ModelAdmin):
    list_display = ('user', 'entry',)
    list_filter = ('user',)
    raw_id_fields = ('entry',)


class ListAdmin (admin.ModelAdmin):
    list_display = ('user', 'name',)
    list_filter = ('user',)

admin.site.register(Service, ServiceAdmin)
admin.site.register(Entry, EntryAdmin)
admin.site.register(Media, MediaAdmin)
admin.site.register(Favorite, FavoriteAdmin)
admin.site.register(List, ListAdmin)

Return to:

Send suggestions and report system problems to the System administrator.