Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 2414b958af
Fetching contributors…

Cannot retrieve contributors at this time

146 lines (104 sloc) 4.632 kb
from django.contrib import admin
from django.db.models import URLField
from research.models import Topic, Resource, Question, Answer, RelevantResource
from research.forms import ResourceForm
from research.widgets import WebLinkWidget
def get_user_identification(user):
if user.first_name or user.last_name:
return user.first_name + " " + user.last_name
else:
return user.username
class TopicAdmin(admin.ModelAdmin):
def resources(self, item):
if item.online_resources.all():
num = len(item.online_resources.all())
return '<a href="/admin/research/resource/?topics__id__exact=' + str(item.id) + '">' + str(num) + '</a>'
resources.allow_tags = True
list_display = ('name','resources')
search_fields = ('name',)
class ResourceAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'author':
kwargs['initial'] = request.user
return super(ResourceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
def resource(self, item):
if item.resource_type == 'online':
return '<a target="_blank" href="' + str(item.get_absolute_url()) + '"><div style="min-width:95px;">Go to Resource &raquo;</div></a>'
elif item.resource_type == 'document':
return '<a target="_blank" href="' + str(item.get_absolute_url()) + '"><div style="min-width:95px;">Go to Resource &raquo;</div></a>'
resource.allow_tags = True
def associated_topics(self, item):
html = ''
if item.topics.all():
for topic in item.topics.all():
html = html + '<li><a title="See all ' + topic.name + ' resources" href="/admin/research/resource/?topics__id__exact=' + str(topic.id) + '">' + topic.name + '</a></li>'
html = "<ul>" + html + "</ul>"
return html
else:
return ""
associated_topics.allow_tags = True
def authored_by(self, item):
name = item.author.first_name + " " + item.author.last_name
if name == " ":
name = item.author.username
return '<a href="/admin/research/resource/?author__id__exact=' + str(item.author.id) + '">' + name + '</a>'
authored_by.allow_tags = True
formfield_overrides = {
URLField: {'widget': WebLinkWidget},
}
filter_horizontal = ('topics', )
fields = ('author', 'name', 'resource_type', 'document', 'url', 'topics', 'description')
list_display = ('name', 'authored_by', 'resource_type', 'associated_topics', 'description', 'resource', )
list_filter = ('author', 'topics','resource_type')
search_fields = ('name', 'url', 'document', 'topics__name', 'description')
form = ResourceForm
class AnswerInline(admin.TabularInline):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'author':
kwargs['initial'] = request.user
return super(AnswerInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
raw_id_fields = ('relevant_resources',)
#related_lookup_fields = {
# 'm2m': ['relevant_resources',]
#}
model = Answer
extra = 0
class RelevantResourceInline(admin.TabularInline):
extra = 0
model = RelevantResource
raw_id_fields = ('resource',)
related_lookup_fields = {
'fk': ['resource']
}
class QuestionAdmin(admin.ModelAdmin):
def question_resources(self, item):
html = ""
if item.relevant_resources.exists():
for r in item.relevant_resources.all():
html = html + '<li style="list-style-type:circle;list-style-position:inside;"><a target="_blank" href="' + r.resource.get_absolute_url() + '">' + r.resource.name + '</a></li>'
html = '<ul>' + html + '</ul>'
return html
question_resources.allow_tags = True
def associated_topics(self, item):
html = ''
if item.topics.all():
for topic in item.topics.all():
html = html + '<li><a title="See all ' + topic.name + ' resources" href="/admin/research/question/?topics__id__exact=' + str(topic.id) + '">' + topic.name + '</a></li>'
html = "<ul>" + html + "</ul>"
return html
else:
return ""
associated_topics.allow_tags = True
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'author':
kwargs['initial'] = request.user
return super(QuestionAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
def authored_by(self, item):
return get_user_identification(item.author)
filter_horizontal = ('topics',)
list_filter = ('author', 'is_answered', 'topics')
list_display = ('content', 'authored_by', 'associated_topics', 'question_resources', 'is_answered')
inlines = [RelevantResourceInline, AnswerInline]
admin.site.register(Topic, TopicAdmin)
admin.site.register(Resource, ResourceAdmin)
admin.site.register(Question, QuestionAdmin)
Jump to Line
Something went wrong with that request. Please try again.