Commit b747cade authored by Adrián Ribao's avatar Adrián Ribao
Browse files

cleanups

parent a37a6299
# -*- coding: utf-8 -*-
#from django.contrib.gis import admin
from django.contrib import admin
#from mptt.admin import MPTTModelAdmin
from django.utils.translation import ugettext_lazy as _
from cartero.models import Contact, EmailTemplate, Campaign, ExcludedEmail,\
ShortURL, LinkClick, OpenEmail, Image
from django.conf.urls import patterns, include, url
from django.conf.urls import patterns
from cartero.views import CreateTemplateView, UpdateTemplateView
from cartero.utils.sending import CampaignDeliver
from django.db.models import Count
#StackedInline o TabularInline
#class ModelInline(admin.TabularInline):
#model = Model
class ContactAdmin(admin.ModelAdmin):
date_hierarchy = 'created'
......@@ -38,9 +32,6 @@ class ContactAdmin(admin.ModelAdmin):
'mark_unsent',
'mark_sent',
]
#inlines = [
#]
#prepopulated_fields = {"slug": ("name", )}
def mark_unsent(self, request, queryset):
queryset.update(sent=False)
......@@ -51,6 +42,7 @@ class ContactAdmin(admin.ModelAdmin):
mark_sent.short_description = _(u'Mark as sent')
admin.site.register(Contact, ContactAdmin)
class EmailTemplateAdmin(admin.ModelAdmin):
date_hierarchy = 'created'
list_display = [
......@@ -64,25 +56,25 @@ class EmailTemplateAdmin(admin.ModelAdmin):
'html',
'plain',
]
def get_urls(self):
urls = super(EmailTemplateAdmin, self).get_urls()
my_urls = patterns('',
(r'^add/$', self.admin_site.admin_view(CreateTemplateView.as_view())),
(r'^(?P<pk>\d+)/$', self.admin_site.admin_view(UpdateTemplateView.as_view()))
my_urls = patterns(
'',
(r'^add/$',
self.admin_site.admin_view(CreateTemplateView.as_view())),
(r'^(?P<pk>\d+)/change/$',
self.admin_site.admin_view(UpdateTemplateView.as_view()))
)
return my_urls + urls
admin.site.register(EmailTemplate, EmailTemplateAdmin)
class CampaignAdmin(admin.ModelAdmin):
date_hierarchy = 'start'
#def queryset(self, request):
#qs = super(CampaignAdmin, self).queryset(request)
#qs = qs.annotate(num_contacts=Count('contacts'))
#return qs
def num_contacts(self, obj):
return obj.contacts.all().count()
num_contacts.short_description = '# Contacts'
......@@ -105,9 +97,6 @@ class CampaignAdmin(admin.ModelAdmin):
'subject',
'reply_to',
]
#inlines = [
#]
#prepopulated_fields = {"slug": ("name", )}
actions = [
'send_test',
'send',
......@@ -144,6 +133,7 @@ class CampaignAdmin(admin.ModelAdmin):
admin.site.register(Campaign, CampaignAdmin)
class ExcludedEmailAdmin(admin.ModelAdmin):
date_hierarchy = 'created'
list_display = [
......@@ -158,6 +148,7 @@ class ExcludedEmailAdmin(admin.ModelAdmin):
]
admin.site.register(ExcludedEmail, ExcludedEmailAdmin)
class ShortURLAdmin(admin.ModelAdmin):
date_hierarchy = 'created'
list_display = [
......@@ -172,6 +163,7 @@ class ShortURLAdmin(admin.ModelAdmin):
]
admin.site.register(ShortURL, ShortURLAdmin)
class LinkClickAdmin(admin.ModelAdmin):
date_hierarchy = 'created'
list_display = [
......@@ -187,6 +179,7 @@ class LinkClickAdmin(admin.ModelAdmin):
]
admin.site.register(LinkClick, LinkClickAdmin)
class OpenEmailAdmin(admin.ModelAdmin):
date_hierarchy = 'created'
list_display = [
......@@ -202,6 +195,7 @@ class OpenEmailAdmin(admin.ModelAdmin):
]
admin.site.register(OpenEmail, OpenEmailAdmin)
class ImageAdmin(admin.ModelAdmin):
date_hierarchy = 'created'
list_display = [
......
# -*- coding: utf-8 -*-
from django import template
from django.template.base import Library, Node, Variable, TemplateSyntaxError,\
parse_bits, token_kwargs
#from django.utils.translation import ugettext as _
#from django.shortcuts import get_object_or_404
from inspect import getargspec
from functools import partial
from django.utils import six
#from django.utils.safestring import mark_safe
from django.template.base import Node, TemplateSyntaxError
import bleach
......@@ -23,7 +18,8 @@ class CarteroPlaceholder(Node):
def parse_args(self, args):
if len(args) < 1:
raise TemplateSyntaxError("The tag must have at least one argument with the name")
raise TemplateSyntaxError(
"The tag must have at least one argument with the name")
self.name = args[0]
def render(self, context):
......@@ -44,7 +40,7 @@ class CarteroPlaceholder(Node):
register = template.Library()
#@register.simple_tag
@register.tag
def editable_text(parser, token):
contents = token.split_contents()
......@@ -53,6 +49,7 @@ def editable_text(parser, token):
node = CarteroPlaceholder(tag_name, bits)
return node
@register.tag
def editable_html(parser, token):
contents = token.split_contents()
......@@ -61,6 +58,7 @@ def editable_html(parser, token):
node = CarteroPlaceholder(tag_name, bits)
return node
@register.tag
def editable_image(parser, token):
contents = token.split_contents()
......@@ -69,6 +67,7 @@ def editable_image(parser, token):
node = CarteroPlaceholder(tag_name, bits)
return node
@register.simple_tag
def unsubscribe_url():
return '{{ unsubscribe_url }}'
# -*- coding: utf-8 -*-
from django.views.generic import FormView, DetailView, UpdateView, View, TemplateView, RedirectView
#from django.views.generic.list import ListView
#from django.views.generic.detail import DetailView
from django.views.generic import FormView, DetailView, UpdateView, View, \
TemplateView, RedirectView
from django.http import HttpResponse
from cartero.models import EmailTemplate, ExcludedEmail, ShortURL, LinkClick, Campaign, MailTracks, OpenEmail
from cartero.models import EmailTemplate, ExcludedEmail, ShortURL, LinkClick, \
Campaign, MailTracks, OpenEmail
from cartero.forms import EmailTemplateForm
from django.core.urlresolvers import reverse_lazy, reverse
from cartero.utils.templates import get_nodes
from django.template import loader, Context, RequestContext
from django.template import loader, RequestContext
from django.contrib.sites.models import Site
from django import forms
from django.http import Http404
from lxml import etree
from django.conf import settings
from premailer import Premailer
from StringIO import StringIO
from django.core.signing import Signer, BadSignature
from django.contrib.staticfiles.finders import find
from PIL import Image
import json
import os
import re
# ADMIN VIEWS
class CreateTemplateView(FormView):
template_name = 'cartero/admin/create_email_template.html'
model = EmailTemplate
form_class = EmailTemplateForm
#success_url = reverse_lazy('admin:cartero:emailtemplate_change', args=(object.id,))
#email_template = None
#context = {}
def get_context_data(self, **kwargs):
context = super(CreateTemplateView, self).get_context_data(**kwargs)
#if self.email_template:
#context['preview'] = self.get_preview()
return context
def get_success_url(self):
success_url = reverse('admin:cartero_emailtemplate_change', args=(self.object.id,))
success_url = reverse(
'admin:cartero_emailtemplate_change',
args=(self.object.id,)
)
return success_url
#def get_form(self, form_class):
#template = self.request.POST.get('template')
#if template:
#self.email_template = loader.get_template(template.replace(os.path.join(APP_ROOT, 'templates/'), ''))
#if template:
#nodes = get_nodes(self.email_template)
#for node in nodes:
##print node.tag_name, node.name, node.tag_args
#if node.tag_name == 'editable_text':
##form.fields[node.name] = forms.CharField()
#form_class.base_fields[node.name] = forms.CharField()
#elif node.tag_name == 'editable_html':
##form.fields[node.name] = forms.CharField(widget=forms.Textarea)
#form_class.base_fields[node.name] = forms.CharField(widget=forms.Textarea)
#form = form_class(**self.get_form_kwargs())
#return form
#def get_preview(self):
#c = Context(self.context)
#html = self.email_template.render(c)
#return html
def form_valid(self, form):
data = form.cleaned_data
#self.context.update(data)
# data = form.cleaned_data
emailtemplate = form.save(commit=False)
emailtemplate.site = Site.objects.get_current()
emailtemplate.save()
self.object = emailtemplate
#return self.render_to_response(self.get_context_data(form=form))
response = super(CreateTemplateView, self).form_valid(form)
return response
class UpdateTemplateView(UpdateView):
template_name = 'cartero/admin/create_email_template.html'
model = EmailTemplate
form_class = EmailTemplateForm
success_url = reverse_lazy('admin:cartero_emailtemplate_changelist', args=[])
success_url = reverse_lazy(
'admin:cartero_emailtemplate_changelist',
args=[]
)
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.save()
return super(UpdateTemplateView, self).form_valid(form)
class PreviewTemplate(DetailView):
model = EmailTemplate
......@@ -100,10 +71,12 @@ class PreviewTemplate(DetailView):
return context
def get_preview(self):
email_template = loader.get_template(self.object.template.replace(os.path.join(settings.CARTERO_APP_ROOT, 'templates/'), ''))
email_template = loader.get_template(
self.object.template.replace(
os.path.join(settings.CARTERO_APP_ROOT, 'templates/'), '')
)
c = RequestContext(self.request, self.get_context())
html = email_template.render(c)
#html = self.compile_css(html)
return html
def get_context_data(self, **kwargs):
......@@ -112,6 +85,7 @@ class PreviewTemplate(DetailView):
return context
class SaveTemplateData(View):
http_method_names = ['post', ]
......@@ -144,6 +118,7 @@ class SaveTemplateData(View):
emailtemplate.save()
return HttpResponse('ok')
class UnsubscribeView(TemplateView):
template_name = 'cartero/unsubscribe.html'
......@@ -155,12 +130,15 @@ class UnsubscribeView(TemplateView):
email = signer.unsign(signed_email)
except BadSignature:
raise Http404
excluded_email, created = ExcludedEmail.objects.get_or_create(email=email)
excluded_email, created = ExcludedEmail.objects.get_or_create(
email=email
)
excluded_email.reason = ExcludedEmail.UNSUBSCRIBE
excluded_email.save()
return context
class GotoView(RedirectView):
def get_redirect_url(self, **kwargs):
code = kwargs.get('code')
......@@ -179,13 +157,15 @@ class GotoView(RedirectView):
ip_address = self.request.META['REMOTE_ADDR']
lc = LinkClick(url=short_url,
ip_address=ip_address,
campaign=campaign)
ip_address=ip_address,
campaign=campaign)
lc.save()
return short_url.url
class MailTrackView(View):
http_method_names = ['get', ]
def get(self, request, *args, **kwargs):
mailtrack_id = kwargs.get('mailtrack_id')
try:
......@@ -194,14 +174,13 @@ class MailTrackView(View):
raise Http404
ip_address = self.request.META['REMOTE_ADDR']
openemail = OpenEmail(
ip_address=ip_address,
campaign = mailtrack.campaign,
contact = mailtrack.contact,
)
ip_address=ip_address,
campaign=mailtrack.campaign,
contact=mailtrack.contact,
)
openemail.save()
img = Image.new("RGBA", (4,4))
img = Image.new("RGBA", (4, 4))
response = HttpResponse(mimetype="image/png")
img.save(response, "PNG")
return response
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment