summaryrefslogtreecommitdiff
path: root/gstudio/admin/relationtype.py
blob: f83f1087de89fb1277486685f11a8c1c14de185d (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""RelationtypeAdmin for Gstudio"""
from datetime import datetime
from django.forms import Media
from django.contrib import admin
from django.contrib.auth.models import User
from django.utils.html import strip_tags
from django.utils.text import truncate_words
from django.conf.urls.defaults import url
from django.conf.urls.defaults import patterns
from django.conf import settings as project_settings
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse, NoReverseMatch
from markitup.widgets import AdminMarkItUpWidget

from tagging.models import Tag

import reversion
from gstudio import settings
from gstudio.managers import HIDDEN
from gstudio.managers import PUBLISHED
from gstudio.ping import DirectoryPinger
from gstudio.admin.forms import RelationtypeAdminForm
from gstudio.settings import GSTUDIO_VERSIONING


if GSTUDIO_VERSIONING == True:
    parent_class = reversion.VersionAdmin
else:
    parent_class = admin.ModelAdmin 

class RelationtypeAdmin(parent_class):
    """Admin for Relationtype model"""
    form = RelationtypeAdminForm
    date_hierarchy = 'creation_date'
    fieldsets = ((
            (_('Neighbourhood Definiton'), {'fields': (
                        'title',
                        'inverse', 
                        'altnames',
                        'parent',
                        'slug',
                        
                        'left_subjecttype',
                        'left_applicable_nodetypes',
                        'left_cardinality',
                        'right_subjecttype',
                        'right_applicable_nodetypes',
                        'right_cardinality',
                        'is_symmetrical',
                        'is_reflexive',
                        'is_transitive') 
                                            }),
            (_('Content'), {'fields': ('content', 'image',), 
                            'classes': ('collapse', 'collapse-closed')}),
            
            
            (_('Dependency'), {'fields': ('prior_nodes', 'posterior_nodes',), 
                               'classes': ('collapse', 'collapse-closed')}),
            (_('Options'), {'fields': ('featured', 'excerpt', 'template',
                                       'authors',
                                       'creation_date',
                                       'start_publication',
                                       'end_publication'),
                            'classes': ('collapse', 'collapse-closed')}),
            ))
            

    list_filter = ('parent','metatypes', 'authors', 'status', 'featured',
                   'login_required', 'comment_enabled', 'pingback_enabled',
                   'creation_date', 'start_publication',
                   'end_publication', 'sites')
    list_display = ('get_title', 'get_authors', 'get_metatypes',
                    'get_tags', 'get_sites',
                    'get_comments_are_open', 'pingback_enabled',
                    'get_is_actual', 'get_is_visible', 'get_link',
                    'get_short_url', 'creation_date')
    radio_fields = {'template': admin.VERTICAL}
    filter_horizontal = ('metatypes', 'authors')
    prepopulated_fields = {'slug': ('title', )}
    search_fields = ('title', 'excerpt', 'content', 'tags')
    actions = ['make_mine', 'make_published', 'make_hidden',
               'close_comments', 'close_pingbacks',
               'ping_directories', 'make_tweet', 'put_on_top']
    actions_on_top = True
    actions_on_bottom = True
    
    def __init__(self, model, admin_site):
                     self.form.admin_site = admin_site
                     super(RelationtypeAdmin, self).__init__(model, admin_site)

    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'content':
            kwargs['widget'] = AdminMarkItUpWidget()
        return super(RelationtypeAdmin, self).formfield_for_dbfield(db_field, **kwargs)
                 
                 # Custom Display
    def get_title(self, relationtype):
        """Return the title with word count and number of comments"""
        title = _('%(title)s (%(word_count)i words)') % \
            {'title': relationtype.title, 'word_count': relationtype.word_count}
        comments = relationtype.comments.count()
        if comments:
            return _('%(title)s (%(comments)i comments)') % \
                {'title': title, 'comments': comments}
        return title
    get_title.short_description = _('title')
    
    def get_authors(self, relationtype):
        """Return the authors in HTML"""
        try:
            authors = ['<a href="%s" target="blank">%s</a>' %
                       (reverse('gstudio_author_detail',
                                args=[author.username]),
                        author.username) for author in relationtype.authors.all()]
        except NoReverseMatch:
            authors = [author.username for author in relationtype.authors.all()]
        return ', '.join(authors)
    get_authors.allow_tags = True
    get_authors.short_description = _('author(s)')
        
    def get_metatypes(self, relationtype):
        """Return the metatypes linked in HTML"""
        try:
            metatypes = ['<a href="%s" target="blank">%s</a>' %
                          (metatype.get_absolute_url(), metatype.title)
                          for metatype in relationtype.metatypes.all()]
        except NoReverseMatch:
            metatypes = [metatype.title for metatype in
                          relationtype.metatypes.all()]
        return ', '.join(metatypes)
    get_metatypes.allow_tags = True
    get_metatypes.short_description = _('metatype(s)')

    def get_tags(self, relationtype):
        """Return the tags linked in HTML"""
        try:
            return ', '.join(['<a href="%s" target="blank">%s</a>' %
                              (reverse('gstudio_tag_detail',
                                       args=[tag.name]), tag.name)
                              for tag in Tag.relations.get_for_relation(relationtype)])
        except NoReverseMatch:
            return relationtype.tags
    get_tags.allow_tags = True
    get_tags.short_description = _('tag(s)')

    def get_sites(self, relationtype):
        """Return the sites linked in HTML"""
        return ', '.join(
            ['<a href="http://%(domain)s" target="blank">%(name)s</a>' %
             site.__dict__ for site in relationtype.sites.all()])
    get_sites.allow_tags = True
    get_sites.short_description = _('site(s)')

    def get_comments_are_open(self, relationtype):
        """Admin wrapper for relationtype.comments_are_open"""
        return relationtype.comments_are_open
    get_comments_are_open.boolean = True
    get_comments_are_open.short_description = _('comment enabled')

    def get_is_actual(self, relationtype):
        """Admin wrapper for relationtype.is_actual"""
        return relationtype.is_actual
    get_is_actual.boolean = True
    get_is_actual.short_description = _('is actual')

    def get_is_visible(self, relationtype):
        """Admin wrapper for relationtype.is_visible"""
        return relationtype.is_visible
    get_is_visible.boolean = True
    get_is_visible.short_description = _('is visible')

    def get_link(self, relationtype):
        """Return a formated link to the relationtype"""
        return u'<a href="%s" target="blank">%s</a>' % (
            relationtype.get_absolute_url(), _('View'))
    get_link.allow_tags = True
    get_link.short_description = _('View on site')

    def get_short_url(self, relationtype):
        """Return the short url in HTML"""
        short_url = relationtype.short_url
        if not short_url:
            return _('Unavailable')
        return '<a href="%(url)s" target="blank">%(url)s</a>' % \
               {'url': short_url}
    get_short_url.allow_tags = True
    get_short_url.short_description = _('short url')

    # Custom Methods
    def save_model(self, request, relationtype, form, change):
        """Save the authors, update time, make an excerpt"""
        if not form.cleaned_data.get('excerpt') and relationtype.status == PUBLISHED:
            relationtype.excerpt = truncate_words(strip_tags(relationtype.content), 50)

        if relationtype.pk and not request.user.has_perm('gstudio.can_change_author'):
            form.cleaned_data['authors'] = relationtype.authors.all()

        if not form.cleaned_data.get('authors'):
            form.cleaned_data['authors'].append(request.user)

        relationtype.last_update = datetime.now()
        relationtype.save()

    def queryset(self, request):
        """Make special filtering by user permissions"""
        queryset = super(RelationtypeAdmin, self).queryset(request)
        if request.user.has_perm('gstudio.can_view_all'):
            return queryset
        return request.user.relationtypes.all()

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        """Filters the disposable authors"""
        if db_field.name == 'authors':
            if request.user.has_perm('gstudio.can_change_author'):
                kwargs['queryset'] = User.objects.filter(is_staff=True)
            else:
                kwargs['queryset'] = User.objects.filter(pk=request.user.pk)

        return super(RelationtypeAdmin, self).formfield_for_manytomany(
            db_field, request, **kwargs)

    def get_actions(self, request):
        """Define user actions by permissions"""
        actions = super(RelationtypeAdmin, self).get_actions(request)
        if not request.user.has_perm('gstudio.can_change_author') \
           or not request.user.has_perm('gstudio.can_view_all'):
            del actions['make_mine']
        if not settings.PING_DIRECTORIES:
            del actions['ping_directories']
        if not settings.USE_TWITTER:
            del actions['make_tweet']

        return actions

    # Custom Actions
    def make_mine(self, request, queryset):
        """Set the relationtypes to the user"""
        for relationtype in queryset:
            if request.user not in relationtype.authors.all():
                relationtype.authors.add(request.user)
        self.message_user(
            request, _('The selected relationtypes now belong to you.'))
    make_mine.short_description = _('Set the relationtypes to the user')

    def make_published(self, request, queryset):
        """Set relationtypes selected as published"""
        queryset.update(status=PUBLISHED)
        self.ping_directories(request, queryset, messages=False)
        self.message_user(
            request, _('The selected relationtypes are now marked as published.'))
    make_published.short_description = _('Set relationtypes selected as published')

    def make_hidden(self, request, queryset):
        """Set relationtypes selected as hidden"""
        queryset.update(status=HIDDEN)
        self.message_user(
            request, _('The selected relationtypes are now marked as hidden.'))
    make_hidden.short_description = _('Set relationtypes selected as hidden')

    def make_tweet(self, request, queryset):
        """Post an update on Twitter"""
        import tweepy
        auth = tweepy.OAuthHandler(settings.TWITTER_CONSUMER_KEY,
                                   settings.TWITTER_CONSUMER_SECRET)
        auth.set_access_token(settings.TWITTER_ACCESS_KEY,
                              settings.TWITTER_ACCESS_SECRET)
        api = tweepy.API(auth)
        for relationtype in queryset:
            short_url = relationtype.short_url
            message = '%s %s' % (relationtype.title[:139 - len(short_url)], short_url)
            api.update_status(message)
        self.message_user(
            request, _('The selected relationtypes have been tweeted.'))
    make_tweet.short_description = _('Tweet relationtypes selected')

    def close_comments(self, request, queryset):
        """Close the comments for selected relationtypes"""
        queryset.update(comment_enabled=False)
        self.message_user(
            request, _('Comments are now closed for selected relationtypes.'))
    close_comments.short_description = _('Close the comments for '\
                                         'selected relationtypes')

    def close_pingbacks(self, request, queryset):
        """Close the pingbacks for selected relationtypes"""
        queryset.update(pingback_enabled=False)
        self.message_user(
            request, _('Linkbacks are now closed for selected relationtypes.'))
    close_pingbacks.short_description = _(
        'Close the linkbacks for selected relationtypes')

    def put_on_top(self, request, queryset):
        """Put the selected relationtypes on top at the current date"""
        queryset.update(creation_date=datetime.now())
        self.ping_directories(request, queryset, messages=False)
        self.message_user(request, _(
            'The selected relationtypes are now set at the current date.'))
    put_on_top.short_description = _(
        'Put the selected relationtypes on top at the current date')

    def ping_directories(self, request, queryset, messages=True):
        """Ping Directories for selected relationtypes"""
        for directory in settings.PING_DIRECTORIES:
            pinger = DirectoryPinger(directory, queryset)
            pinger.join()
            if messages:
                success = 0
                for result in pinger.results:
                    if not result.get('flerror', True):
                        success += 1
                    else:
                        self.message_user(request,
                                          '%s : %s' % (directory,
                                                       result['message']))
                if success:
                    self.message_user(
                        request,
                        _('%(directory)s directory succesfully ' \
                          'pinged %(success)d relationtypes.') %
                        {'directory': directory, 'success': success})
    ping_directories.short_description = _(
        'Ping Directories for selected relationtypes')

    def get_urls(self):
        relationtype_admin_urls = super(RelationtypeAdmin, self).get_urls()
        urls = patterns(
            'django.views.generic.simple',
            url(r'^autocomplete_tags/$', 'direct_to_template',
                {'template': 'admin/gstudio/relationtype/autocomplete_tags.js',
                 'mimetype': 'application/javascript'},
                name='gstudio_relationtype_autocomplete_tags'),
            url(r'^wymeditor/$', 'direct_to_template',
                {'template': 'admin/gstudio/relationtype/wymeditor.js',
                 'mimetype': 'application/javascript'},
                name='gstudio_relationtype_wymeditor'),
            url(r'^markitup/$', 'direct_to_template',
                {'template': 'admin/gstudio/relationtype/markitup.js',
                 'mimetype': 'application/javascript'},
                name='gstudio_relationtype_markitup'),)
        return urls + relationtype_admin_urls

    def _media(self):
        STATIC_URL = '%sgstudio/' % project_settings.STATIC_URL
        media = super(RelationtypeAdmin, self).media + Media(
            css={'all': ('%scss/jquery.autocomplete.css' % STATIC_URL,)},
            js=('%sjs/jquery.js' % STATIC_URL,
                '%sjs/jquery.bgiframe.js' % STATIC_URL,
                '%sjs/jquery.autocomplete.js' % STATIC_URL,
                reverse('admin:gstudio_relationtype_autocomplete_tags'),))

        if settings.WYSIWYG == 'wymeditor':
            media += Media(
                js=('%sjs/wymeditor/jquery.wymeditor.pack.js' % STATIC_URL,
                    '%sjs/wymeditor/plugins/hovertools/'
                    'jquery.wymeditor.hovertools.js' % STATIC_URL,
                    reverse('admin:gstudio_relationtype_wymeditor')))
        elif settings.WYSIWYG == 'tinymce':
            from tinymce.widgets import TinyMCE
            media += TinyMCE().media + Media(
                js=(reverse('tinymce-js', args=('admin/gstudio/relationtype',)),))
        elif settings.WYSIWYG == 'markitup':
            media += Media(
                js=('%sjs/markitup/jquery.markitup.js' % STATIC_URL,
                    '%sjs/markitup/sets/%s/set.js' % (
                        STATIC_URL, settings.MARKUP_LANGUAGE),
                    reverse('admin:gstudio_relationtype_markitup')),
                css={'all': (
                    '%sjs/markitup/skins/django/style.css' % STATIC_URL,
                    '%sjs/markitup/sets/%s/style.css' % (
                        STATIC_URL, settings.MARKUP_LANGUAGE))})
        return media
    media = property(_media)