<commit_before>from django.contrib import admin
from .models import Quote
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse
from django.conf.urls import patterns, url
from django.contrib.admin.util import unquote
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from functools import update_wrapper

class VoteableModelAdmin(admin.ModelAdmin):
  """
  Admin support for voteable models.
  """

  def upvote(self, obj):
    """
    Provide a button to upvote instance.
    """

    url = reverse("admin:{}_{}_upvote".format(self.model._meta.app_label, self.model._meta.model_name), args=(obj.id, ))
    return mark_safe('<a href="{}"><div class="arrow-up"></div></a>'.format(url))
  upvote.short_description = 'Upvote'
  upvote.allow_tags = True

  def downvote(self, obj):
    """
    Provide a button to downvote instance.
    """
    
    url = reverse("admin:{}_{}_downvote".format(self.model._meta.app_label, self.model._meta.model_name), args=(obj.id, ))
    return mark_safe('<a href="{}"><div class="arrow-down"></div></a>'.format(url))
  downvote.short_description = 'Downvote'
  downvote.allow_tags = True

  def upvote_view(self, request, obj_id):
    """
    Admin view to upvote object.
    """

    obj = get_object_or_404(self.model, pk=unquote(obj_id))
    obj.upvote()

    return HttpResponseRedirect('../../')

  def downvote_view(self, request, obj_id):
    """
    Admin view to downvote object.
    """

    obj = get_object_or_404(self.model, pk=unquote(obj_id))
    obj.downvote()

    return HttpResponseRedirect('../../')

  def get_urls(self):
    """
    Return default urls as well as those
    to upvote and downvote object.
    """

    urls = super().get_urls()

    def wrap(view):
      def wrapper(*args, **kwargs):
        return self.admin_site.admin_view(view)(*args, **kwargs)
      return update_wrapper(wrapper, view)

    return patterns('',
      url(r'^(.+)/upvote/$', wrap(self.upvote_view), name="{}_{}_upvote".format(self.model._meta.app_label, self.model._meta.model_name)),
      url(r'^(.+)/downvote/$', wrap(self.downvote_view), name="{}_{}_downvote".format(self.model._meta.app_label, self.model._meta.model_name)),
    ) + urls

  class Media:
    css = {
      'all': ('quotetron/css/voteable.css',)
    }


class QuoteAdmin(VoteableModelAdmin):

  fields = ('quote', )
  list_display = ('id', 'quote', 'upvote', 'up_votes', 'downvote', 'down_votes', 'votes', 'total_votes', )<commit_msg>Updated Quotetron interface.<commit_after>from django.contrib import admin
from .models import Quote
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse
from django.conf.urls import patterns, url
from django.contrib.admin.util import unquote
from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect
from functools import update_wrapper

class VoteableModelAdmin(admin.ModelAdmin):
  """
  Admin support for voteable models.
  """

  def upvote(self, obj):
    """
    Provide a button to upvote instance.
    """

    url = reverse("admin:{}_{}_upvote".format(self.model._meta.app_label, self.model._meta.model_name), args=(obj.id, ))
    return mark_safe('<a href="{}"><div class="arrow-up"></div></a>'.format(url))
  upvote.short_description = 'Upvote'
  upvote.allow_tags = True

  def downvote(self, obj):
    """
    Provide a button to downvote instance.
    """
    
    url = reverse("admin:{}_{}_downvote".format(self.model._meta.app_label, self.model._meta.model_name), args=(obj.id, ))
    return mark_safe('<a href="{}"><div class="arrow-down"></div></a>'.format(url))
  downvote.short_description = 'Downvote'
  downvote.allow_tags = True

  def upvote_view(self, request, obj_id):
    """
    Admin view to upvote object.
    """

    obj = get_object_or_404(self.model, pk=unquote(obj_id))
    obj.upvote()

    return HttpResponseRedirect(request.META['HTTP_REFERER'])

  def downvote_view(self, request, obj_id):
    """
    Admin view to downvote object.
    """

    obj = get_object_or_404(self.model, pk=unquote(obj_id))
    obj.downvote()

    return HttpResponseRedirect(request.META['HTTP_REFERER'])

  def get_urls(self):
    """
    Return default urls as well as those
    to upvote and downvote object.
    """

    urls = super().get_urls()

    def wrap(view):
      def wrapper(*args, **kwargs):
        return self.admin_site.admin_view(view)(*args, **kwargs)
      return update_wrapper(wrapper, view)

    return patterns('',
      url(r'^(.+)/upvote/$', wrap(self.upvote_view), name="{}_{}_upvote".format(self.model._meta.app_label, self.model._meta.model_name)),
      url(r'^(.+)/downvote/$', wrap(self.downvote_view), name="{}_{}_downvote".format(self.model._meta.app_label, self.model._meta.model_name)),
    ) + urls

  class Media:
    css = {
      'all': ('quotetron/css/voteable.css',)
    }


class QuoteAdmin(VoteableModelAdmin):

  fields = ('id', 'quote', 'up_votes', 'down_votes', 'upvote', 'downvote', 'votes', 'total_votes', )
  readonly_fields = ('id', 'up_votes', 'down_votes', 'upvote', 'downvote', 'votes', 'total_votes', )
  list_display = ('id', 'quote', 'up_votes', 'down_votes', 'upvote', 'downvote', 'votes', 'total_votes', )
{'fraction_non_alphanumeric': 0.15821347464042393, 'fraction_numerical': 0.0034065102195306586, 'mean_word_length': 4.076849183477425, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2084949', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1744, 'n_words': 361}