This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
129 additions
and
11 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,52 @@ | ||
# encoding: utf-8 | ||
import datetime | ||
from django.contrib import admin | ||
from subscription.models import Subscription | ||
from django.utils.translation import ugettext as _ | ||
from django.utils.translation import ungettext | ||
from django.http import HttpResponse | ||
from django.conf.urls.defaults import patterns, url | ||
|
||
class SubscriptionAdmin(admin.ModelAdmin): | ||
list_display = ('name', 'email', 'phone', 'created_at', 'subscribed_today') | ||
list_display = ('name', 'email', 'phone', 'created_at', 'subscribed_today', 'paid') | ||
date_hierarchy = 'created_at' | ||
search_fields = ('name', 'email', 'phone', 'cpf', 'created_at') | ||
list_filter = ['created_at'] | ||
list_filter = ['created_at', 'paid'] | ||
|
||
actions = ['mark_as_paid'] | ||
|
||
def subscribed_today(self, obj): | ||
return obj.created_at.date() == datetime.date.today() | ||
|
||
subscribed_today.short_description = 'Inscrito hoje?' | ||
subscribed_today.boolean = True | ||
|
||
def mark_as_paid(self, request, queryset): | ||
count = queryset.update(paid=True) | ||
msg = ungettext ( | ||
u'%(count)d inscrição foi marcada como paga.', | ||
u'%(count)d inscrições foram marcadas como pagas.', | ||
count) % {'count': count} | ||
self.message_user(request, msg) | ||
|
||
mark_as_paid.short_description = _(u'Marcar como pagas') | ||
|
||
def export_subscriptions(self, request): | ||
subscriptions = self.model.objects.all() | ||
rows = [','.join([s.name, s.email]) for s in subscriptions] #list comprehension | ||
response = HttpResponse('\r\n'.join(rows)) | ||
response.mimetype = 'text/csv' | ||
response['Content-Disposition'] = 'attachment; filename=inscritos.csv' | ||
|
||
return response | ||
|
||
def get_urls(self): | ||
default_urls = super(SubscriptionAdmin, self).get_urls() | ||
new_urls = patterns('', | ||
url(r'^exportar-inscricoes/$', self.admin_site.admin_view(self.export_subscriptions), | ||
name='export_subscriptions')) | ||
|
||
return new_urls + default_urls | ||
|
||
|
||
admin.site.register(Subscription, SubscriptionAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,56 @@ | ||
# encoding: utf-8 | ||
|
||
from django import forms | ||
from subscription.models import Subscription | ||
from django.utils.translation import ugettext as _ | ||
from subscription import validators | ||
from django.core.validators import EMPTY_VALUES | ||
|
||
class PhoneWidget(forms.MultiWidget): | ||
def __init__(self, attrs=None): | ||
widgets = ( | ||
forms.TextInput(attrs=attrs), | ||
forms.TextInput(attrs=attrs)) | ||
super(PhoneWidget, self).__init__(widgets, attrs) | ||
|
||
def decompress(self, value): | ||
if value: | ||
return value.split('‐') | ||
return [None, None] | ||
|
||
class PhoneField(forms.MultiValueField): | ||
|
||
widget = PhoneWidget | ||
|
||
def __init__(self, *args, **kwargs): | ||
fields = (forms.IntegerField(), forms.IntegerField()) | ||
super(PhoneField, self).__init__(fields, *args, **kwargs) | ||
|
||
def compress(self, data_list): | ||
if data_list: | ||
if data_list[0] in EMPTY_VALUES: | ||
raise forms.ValidationError(_(u'DDD inválido.')) | ||
if data_list[1] in EMPTY_VALUES: | ||
raise forms.ValidationError(_(u'Número inválido.')) | ||
return '%s‐%s' % tuple(data_list) | ||
return "" | ||
|
||
class SubscriptionForm(forms.ModelForm): | ||
|
||
phone = PhoneField(required=False) | ||
|
||
class Meta: | ||
model = Subscription | ||
exclude= ("created_at",) | ||
exclude= ('created_at', 'paid') | ||
|
||
def clean(self): | ||
super(SubscriptionForm, self).clean() | ||
if not self.cleaned_data.get('email') and not self.cleaned_data.get('phone'): | ||
raise forms.ValidationError(_(u'Você deve informar pelo menos o e-mail ou o telefone.')) | ||
return self.cleaned_data | ||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# encoding: utf-8 | ||
from django.utils.translation import ugettext as _ | ||
from django.core.exceptions import ValidationError | ||
|
||
def CpfValidator(value): | ||
if not value.isdigit(): | ||
raise ValidationEror(_(u'O CPF deve conter apenas números')) | ||
if len(value) != 11: | ||
raise ValidationError(_(u'O CPF deve conter 11 dígitos')) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# coding: utf-8 | ||
# encoding: utf-8 | ||
# Create your views here. | ||
|
||
from django.http import HttpResponseRedirect | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% extends 'admin/change_list.html' %} | ||
{% block object-tools %} | ||
|
||
<ul class="object-tools" style="margin-right: 150px;"> | ||
<li> | ||
<a href="{% url admin:export_subscriptions %}" class="addlink"> | ||
Exportar inscritos | ||
</a> | ||
</li> | ||
</ul> | ||
{{ block.super }} | ||
{% endblock object-tools %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters