Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
Making the admin work
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Greenfeld committed Jul 30, 2015
1 parent 08ea0f2 commit f2280da
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
31 changes: 30 additions & 1 deletion spam/admin.py
@@ -1,5 +1,34 @@
from django.contrib import admin
from django.utils.safestring import mark_safe

from .behaviors import Spammable
from .exceptions import SpamNotFound
from .models import SpammyPosting
from .utils import spammables

admin.site.register(SpammyPosting)
class SpammyPostingAdmin(admin.ModelAdmin):
model = SpammyPosting
readonly_fields = ('source', 'created', 'modified',)

def source(self, instance):

for spammable in spammables():
try:
content = spammable.objects.get(spam_flag=instance.pk)
except SpammyPosting.DoesNotExist:
continue
break
else:
raise SpamNotFound(spammable)

response = """<a href="{0}">{1}</a>""".format(
content.get_absolute_url(),
content
)

return mark_safe(response)

source.short_description = "Source URL"
source.allow_tags = True

admin.site.register(SpammyPosting, SpammyPostingAdmin)
2 changes: 2 additions & 0 deletions spam/exceptions.py
@@ -0,0 +1,2 @@
class SpamNotFound(Exception):
"""When the system can't find spam the user defined"""
3 changes: 3 additions & 0 deletions spam/models.py
Expand Up @@ -27,3 +27,6 @@ class SpammyPosting(models.Model):

class Meta:
app_label = 'spam'

def __str__(self):
return self.get_status_display()
2 changes: 0 additions & 2 deletions spam/utils.py
Expand Up @@ -13,11 +13,9 @@ def spammables():
flaggables.append(model)
return flaggables


def is_spammable(app, model):
model_class = apps.get_model("{}.{}".format(app, model))
return model_class in spammables()


def get_app_name(model_class_or_instance):
return model_class_or_instance._meta.app_config.name.split('.')[-1]
2 changes: 1 addition & 1 deletion spam/views.py
Expand Up @@ -57,7 +57,7 @@ class ThankYouView(TemplateView):

class SpammableMixin(object):
def spam_report_url(self):
return reverse('spam:report', kwargs={
return reverse_lazy('spam:report', kwargs={
'app': get_app_name(self.object),
'model': self.object._meta.object_name,
'pk': self.object.pk
Expand Down

0 comments on commit f2280da

Please sign in to comment.