# Path: grapevine/emails/filters.py
# class OnSpecificDateListFilter(admin.FieldListFilter):
# 
#     format = 'YYYY-MM-DD'
#     endswith = '__date'
# 
#     def __init__(self, field, request, params, model, model_admin, field_path):
#         super(OnSpecificDateListFilter, self).__init__(field, request, params, model, model_admin, field_path)
# 
#     def expected_parameters(self):
#         return [self.field_path + self.endswith]
# 
#     def lookups(self, request, model_admin):
#         return []
# 
#     @property
#     def qs_value(self):
#         return self.field_path + self.endswith
# 
#     def choices(self, cl):
#         for x in range(31):
#             date = timezone.now()
#             # By default, offer 15 days ago to 15 days from now
#             date = date + datetime.timedelta(days=(x - 15))
#             values = {
#                 self.qs_value: '%s-%s-%s' % (date.year, date.month, date.day,)
#             }
#             yield {
#                 'selected': self.used_parameters.get(self.qs_value, None) == values[self.qs_value],
#                 'query_string': cl.get_query_string(values),
#                 'display': date.strftime('%a %b %d, %Y')
#             }
# 
#     def queryset(self, request, queryset):
#         processed_params = self.process_used_parameters()
#         return queryset.filter(**processed_params)
# 
#     def process_used_parameters(self):
#         """
#         Because we accept a querystring value like "my_field__date=2014-01-01",
#         which doesn't make sense by itself to the ORM, we have to do some parsing.
#         """
#         processed_params = {}
#         for key, value in self.used_parameters.items():
#             if key.endswith(self.endswith):
#                 # Split into ``field_name`` and "date" on the "__"
#                 key_field, key_suffix = key.split('__')
# 
#                 # Split the date, too
#                 year, month, day = value.split('-')
# 
#                 processed_params[key_field + '__year'] = int(year)
#                 processed_params[key_field + '__month'] = int(month)
#                 processed_params[key_field + '__day'] = int(day)
#             else:
#                 processed_params[key] = value
#         return processed_params
# 
# Path: grapevine/utils.py
# def render_view(request, template_name, context={}):
#     return render_to_response(template_name, context, context_instance=RequestContext(request))


from django.conf.urls import url
from django.contrib import admin, messages
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404, redirect
from grapevine.emails.filters import OnSpecificDateListFilter
from grapevine.utils import render_view
    @property
    def model_meta_info(self):
        return (self.model._meta.app_label, self.model._meta.model_name,)

    @property
    def admin_view_info(self):
        return '%s_%s' % self.model_meta_info

    def get_urls(self):
        urls = super(SendableAdminMixin, self).get_urls()
        my_urls = [
            url(r'^(.+)/render/$', self.admin_site.admin_view(self.render), name='%s_render' % (self.admin_view_info,)),
            url(r'^(.+)/send-test/$', self.admin_site.admin_view(self.send_test_message), name='%s_send_test_message' % (self.admin_view_info,)),
            url(r'^(.+)/send-real/$', self.admin_site.admin_view(self.send_real_message), name='%s_send_real_message' % (self.admin_view_info,)),
        ]
        return my_urls + urls

    def send_real_message(self, request, obj_id):
        obj = get_object_or_404(self.model, pk=obj_id)
        if request.method == 'GET':

            # Load the recipients
            recipients = obj.get_normalized_recipients()

            context = {
                'obj': obj,
                'recipients': recipients,
                'opts': self.model._meta,
                'title': 'Send Real %s' % (self.message_type_verbose,)
            }

# Path: grapevine/emails/filters.py
# class OnSpecificDateListFilter(admin.FieldListFilter):
# 
#     format = 'YYYY-MM-DD'
#     endswith = '__date'
# 
#     def __init__(self, field, request, params, model, model_admin, field_path):
#         super(OnSpecificDateListFilter, self).__init__(field, request, params, model, model_admin, field_path)
# 
#     def expected_parameters(self):
#         return [self.field_path + self.endswith]
# 
#     def lookups(self, request, model_admin):
#         return []
# 
#     @property
#     def qs_value(self):
#         return self.field_path + self.endswith
# 
#     def choices(self, cl):
#         for x in range(31):
#             date = timezone.now()
#             # By default, offer 15 days ago to 15 days from now
#             date = date + datetime.timedelta(days=(x - 15))
#             values = {
#                 self.qs_value: '%s-%s-%s' % (date.year, date.month, date.day,)
#             }
#             yield {
#                 'selected': self.used_parameters.get(self.qs_value, None) == values[self.qs_value],
#                 'query_string': cl.get_query_string(values),
#                 'display': date.strftime('%a %b %d, %Y')
#             }
# 
#     def queryset(self, request, queryset):
#         processed_params = self.process_used_parameters()
#         return queryset.filter(**processed_params)
# 
#     def process_used_parameters(self):
#         """
#         Because we accept a querystring value like "my_field__date=2014-01-01",
#         which doesn't make sense by itself to the ORM, we have to do some parsing.
#         """
#         processed_params = {}
#         for key, value in self.used_parameters.items():
#             if key.endswith(self.endswith):
#                 # Split into ``field_name`` and "date" on the "__"
#                 key_field, key_suffix = key.split('__')
# 
#                 # Split the date, too
#                 year, month, day = value.split('-')
# 
#                 processed_params[key_field + '__year'] = int(year)
#                 processed_params[key_field + '__month'] = int(month)
#                 processed_params[key_field + '__day'] = int(day)
#             else:
#                 processed_params[key] = value
#         return processed_params
# 
# Path: grapevine/utils.py
# def render_view(request, template_name, context={}):
#     return render_to_response(template_name, context, context_instance=RequestContext(request))


# Path: grapevine/admin/base.py
from django.conf.urls import url
from django.contrib import admin, messages
from django.contrib.contenttypes.models import ContentType
from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import HttpResponse, HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404, redirect
from grapevine.emails.filters import OnSpecificDateListFilter
from grapevine.utils import render_view

    @property
    def model_meta_info(self):
        return (self.model._meta.app_label, self.model._meta.model_name,)

    @property
    def admin_view_info(self):
        return '%s_%s' % self.model_meta_info

    def get_urls(self):
        urls = super(SendableAdminMixin, self).get_urls()
        my_urls = [
            url(r'^(.+)/render/$', self.admin_site.admin_view(self.render), name='%s_render' % (self.admin_view_info,)),
            url(r'^(.+)/send-test/$', self.admin_site.admin_view(self.send_test_message), name='%s_send_test_message' % (self.admin_view_info,)),
            url(r'^(.+)/send-real/$', self.admin_site.admin_view(self.send_real_message), name='%s_send_real_message' % (self.admin_view_info,)),
        ]
        return my_urls + urls

    def send_real_message(self, request, obj_id):
        obj = get_object_or_404(self.model, pk=obj_id)
        if request.method == 'GET':

            # Load the recipients
            recipients = obj.get_normalized_recipients()

            context = {
                'obj': obj,
                'recipients': recipients,
                'opts': self.model._meta,
                'title': 'Send Real %s' % (self.message_type_verbose,)
            }

            return render_view(request, 'admin/send_real.html', context)