Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.6 #6

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
26 changes: 14 additions & 12 deletions supertagging/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
__version_info__ = {
'major': 0,
'minor': 5,
'micro': 4,
'releaselevel': 'final',
'serial': 1
'minor': 6,
'micro': 0,
'releaselevel': 'beta',
'serial': 3
}

def get_version():
vers = ["%(major)i.%(minor)i" % __version_info__, ]

if __version_info__['micro']:
def get_version(short=False):
assert __version_info__['releaselevel'] in ('alpha', 'beta', 'final')
vers = ["%(major)i.%(minor)i" % __version_info__, ]
if __version_info__['micro'] and not short:
vers.append(".%(micro)i" % __version_info__)
if __version_info__['releaselevel'] != 'final':
vers.append('%(releaselevel)s%(serial)i' % __version_info__)
if __version_info__['releaselevel'] != 'final' and not short:
vers.append('%s%i' % (__version_info__['releaselevel'][0], __version_info__['serial']))
return ''.join(vers)

__version__ = get_version()


class AlreadyRegistered(Exception):
"""
An attempt was made to register a model more than once.
Expand Down Expand Up @@ -60,9 +62,9 @@ def register(model, tag_descriptor_attr='supertags',

# Add custom manager
ModelTaggedItemManager().contribute_to_class(model, tagged_item_manager_attr)

# Add a reverse generic relationship to supertaggeditems
GenericRelation(SuperTaggedItem).contribute_to_class(model, 'supertaggeditem_set')

# Finally register in registry
registry.append(model)
registry.append(model)
87 changes: 45 additions & 42 deletions supertagging/admin.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import django
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import ugettext, ugettext_lazy
from django.utils.encoding import force_unicode, smart_str
from django.utils.translation import ugettext
from django.utils.encoding import force_unicode
from django.http import HttpResponseRedirect

from supertagging.models import SuperTag, SuperTaggedItem, SuperTagRelation
from supertagging.models import SuperTaggedRelationItem, SuperTagProcessQueue

from supertagging.settings import INCLUDE_DISPLAY_FIELDS
from django.contrib.admin.views.main import (ChangeList, ALL_VAR, ORDER_VAR,
ORDER_TYPE_VAR, PAGE_VAR, SEARCH_VAR, TO_FIELD_VAR,
IS_POPUP_VAR, ERROR_FLAG)
from django.contrib.admin.views.main import (ChangeList, ALL_VAR, PAGE_VAR,
SEARCH_VAR, TO_FIELD_VAR, IS_POPUP_VAR, ERROR_FLAG)


class SupertagChangeList(ChangeList):
"""
Lets list_editable work even if it is a popup
"""
def __init__(self, request, model, list_display, list_display_links, list_filter, date_hierarchy, search_fields, list_select_related, list_per_page, list_editable, model_admin):
def __init__(self, request, model, list_display, list_display_links,
list_filter, date_hierarchy, search_fields, list_select_related,
list_per_page, list_max_show_all, list_editable, model_admin):
self.model = model
self.opts = model._meta
self.lookup_opts = self.opts
Expand All @@ -29,8 +30,9 @@ def __init__(self, request, model, list_display, list_display_links, list_filter
self.search_fields = search_fields
self.list_select_related = list_select_related
self.list_per_page = list_per_page
self.list_max_show_all = list_max_show_all
self.model_admin = model_admin

# Get search parameters from the query string.
try:
self.page_num = int(request.GET.get(PAGE_VAR, 0))
Expand All @@ -44,89 +46,90 @@ def __init__(self, request, model, list_display, list_display_links, list_filter
del self.params[PAGE_VAR]
if ERROR_FLAG in self.params:
del self.params[ERROR_FLAG]

self.list_editable = list_editable
self.order_field, self.order_type = self.get_ordering()
self.query = request.GET.get(SEARCH_VAR, '')
self.query_set = self.get_query_set()
self.query_set = self.get_query_set(request)
self.get_results(request)
self.title = (self.is_popup and ugettext('Select %s') % force_unicode(self.opts.verbose_name) or ugettext('Select %s to change') % force_unicode(self.opts.verbose_name))
self.filter_specs, self.has_filters = self.get_filters(request)
self.pk_attname = self.lookup_opts.pk.attname


def lock_items(modeladmin, request, queryset):
queryset.update(locked=True)
lock_items.short_description = "Lock selected Queue Items"


def unlock_items(modeladmin, request, queryset):
queryset.update(locked=False)
unlock_items.short_description = "Unlock selected Queue Items"


class SuperTagAdmin(admin.ModelAdmin):
list_display = ('name', 'enabled', 'substitute', 'stype')
ordering = ('name', )
search_fields = ('stype', 'name', )
list_filter = ('stype', )

actions = ['disable_tag', 'enable_tag']
raw_id_fields = ['substitute',]

raw_id_fields = ['substitute', ]
if INCLUDE_DISPLAY_FIELDS:
raw_id_fields.append('related')

def disable_tag(self, request, queryset):
message_bit = ""
for tag in queryset:
message_bit = "%s %s," % (message_bit, tag.name)
tag.enabled=False
tag.enabled = False
tag.save()

self.message_user(request, "Tag(s): %s were Disabled." % message_bit)
disable_tag.short_description = "Disable selected tags"



def enable_tag(self, request, queryset):
message_bit = ""
for tag in queryset:
message_bit = "%s %s," % (message_bit, tag.name)
tag.enabled=True
tag.enabled = True
tag.save()

self.message_user(request, "Tag(s): %s were Enabled." % message_bit)
enable_tag.short_description = "Enable selected tags"



class SuperTaggedItemAdmin(admin.ModelAdmin):
list_display = ('tag_name', 'tag_type', 'field', 'relevance_bar', 'ignore')
# if django.VERSION[1] > 1:
# list_filter = ('field', 'tag__stype')
# else:
# list_filter = ('field', )

#search_fields = ('tag__name',)
raw_id_fields = ('tag',)
list_editable = ('ignore',)

class Media:
css = {'all': ('css/supertagloading.css',)}
js = ('js/jquery.loading.1.6.4.min.js',)

def tag_name(self, obj):
if INCLUDE_DISPLAY_FIELDS:
return obj.tag.display_name
return obj.tag.name

def tag_type(self, obj):
return obj.tag.stype

def get_changelist(self, request, **kwargs):
"""
Returns the ChangeList class for use on the changelist page.
"""
return SupertagChangeList

def changelist_view(self, request, extra_context=None):
if request.method == 'POST' and '_update_tags' in request.POST:
ctype_id = request.GET.get(u'content_type__id', [False,])
ctype_id = request.GET.get(u'content_type__id', [False, ])
obj_id = request.GET.get('object_id', [False])
if ctype_id == False or obj_id == False:
return HttpResponseRedirect(request.get_full_path())
Expand All @@ -139,49 +142,49 @@ def changelist_view(self, request, extra_context=None):
return HttpResponseRedirect(request.get_full_path())
else:
return super(SuperTaggedItemAdmin, self).changelist_view(request, extra_context)

def relevance_bar(self, obj):
from django.template import Context
from django.template.loader import get_template
if obj.relevance is not None:
relevance = "%d%%" % (obj.relevance / 10.0)
else:
relevance = "0"

tmpl = get_template("admin/supertagging/relevancebar.html")
ctxt = Context({'relevance': relevance})
return tmpl.render(ctxt)
relevance_bar.allow_tags = True
relevance_bar.admin_order_field = 'relevance'
relevance_bar.short_description = 'Relevance'

def get_actions(self, request):
return []


class SuperTaggedRelationItemAdmin(admin.ModelAdmin):
list_display = ('content_object', 'relation', 'field', 'process_type', 'item_date')
list_display = ('content_object', 'relation', 'field', 'process_type', 'item_date')
list_filter = ('process_type', 'field')

raw_id_fields = ('relation',)


class SuperTagRelationAdmin(admin.ModelAdmin):
list_display = ('tag', 'name', 'stype',)
ordering = ('tag', )
search_fields = ('stype', 'name', 'tag__name')
list_filter = ('stype', 'name', )

raw_id_fields = ('tag',)


class SuperTagProcessQueueAdmin(admin.ModelAdmin):
list_display = ('__unicode__', 'locked')
actions = [lock_items, unlock_items]


admin.site.register(SuperTag, SuperTagAdmin)
admin.site.register(SuperTaggedItem, SuperTaggedItemAdmin)
admin.site.register(SuperTagRelation, SuperTagRelationAdmin)
admin.site.register(SuperTaggedRelationItem, SuperTaggedRelationItemAdmin)
admin.site.register(SuperTagProcessQueue, SuperTagProcessQueueAdmin)
admin.site.register(SuperTagProcessQueue, SuperTagProcessQueueAdmin)
Loading