You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
6.1 KiB
177 lines
6.1 KiB
# -*- coding: utf-8 -*-
|
|
import json
|
|
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from django.db.models import Count
|
|
from django.views.generic import TemplateView, FormView
|
|
from django.http import HttpResponse
|
|
from django.shortcuts import redirect
|
|
|
|
from emencia.django.newsletter.forms import ContactForm
|
|
from emencia.django.newsletter.models import Contact, MailingList
|
|
from emencia.django.newsletter.forms import MailingSettingsForm
|
|
from accounts.views import GetUserMixin
|
|
from functions.http import JsonResponse
|
|
from city.models import City
|
|
from theme.models import Theme
|
|
|
|
|
|
class SubscribeView(GetUserMixin, FormView):
|
|
"""
|
|
Представление для подписки не/авторизованных пользователей
|
|
"""
|
|
template_name = 'client/newsletters/mailing_settings.html'
|
|
form_class = MailingSettingsForm
|
|
|
|
def get_object(self):
|
|
return self.get_user()
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
self.object = self.get_object()
|
|
return super(SubscribeView, self).get(request, *args, **kwargs)
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
self.object = self.get_object()
|
|
return super(SubscribeView, self).post(request, *args, **kwargs)
|
|
|
|
def form_valid(self, form):
|
|
if self.request.POST.get('save'):
|
|
contact = form.save()
|
|
if not self.request.user.is_authenticated():
|
|
contact.send_activation()
|
|
|
|
if self.request.is_ajax():
|
|
data = {'success': True}
|
|
if self.request.POST.get('save'):
|
|
data['redirect_url'] = str(self.get_success_url())
|
|
return JsonResponse(data)
|
|
|
|
return redirect(self.get_success_url())
|
|
|
|
def form_invalid(self, form):
|
|
if self.request.is_ajax():
|
|
data = {
|
|
'form_errors': form.errors,
|
|
}
|
|
return JsonResponse(data, status=400)
|
|
return self.render_to_response(self.get_context_data(form=form))
|
|
|
|
def get_initial(self):
|
|
data = super(SubscribeView, self).get_initial()
|
|
if self.request.GET.get('email'):
|
|
data['email'] = self.request.GET['email']
|
|
if self.request.GET.get('first_name'):
|
|
data['first_name'] = self.request.GET['first_name']
|
|
return data
|
|
|
|
def get_form_kwargs(self):
|
|
kwargs = super(SubscribeView, self).get_form_kwargs()
|
|
if self.request.user.is_authenticated():
|
|
kwargs.update({'instance': self.object})
|
|
return kwargs
|
|
|
|
def get_success_url(self):
|
|
if not self.request.user.is_authenticated():
|
|
return reverse_lazy('subscription_activation_send')
|
|
return reverse_lazy('newsletter_subscription')
|
|
|
|
def get_context_data(self, **kwargs):
|
|
ctx = super(SubscribeView, self).get_context_data(**kwargs)
|
|
ctx['object'] = self.object
|
|
ctx['r_cities'] = self.get_russian_cities()
|
|
ctx['checked_th'] = self.get_checked_th()
|
|
ctx['popular_themes'] = self.get_popular_themes()
|
|
|
|
if self.object is not None:
|
|
if self.request.GET.get('unsibscribe') and self.object.subscriber:
|
|
self.object.unsubscribe()
|
|
ctx['unsubscribe_success'] = True
|
|
elif not self.object.subscriber:
|
|
ctx['unsubscribed'] = True
|
|
|
|
return ctx
|
|
|
|
def get_russian_cities(self):
|
|
"""
|
|
:return: города России
|
|
"""
|
|
return City.used.russia()
|
|
|
|
def get_checked_th(self):
|
|
"""
|
|
:return: выбранные пользователем темы
|
|
"""
|
|
if self.object is not None:
|
|
return self.object.themes.values_list('pk', flat=True)
|
|
return []
|
|
|
|
def get_popular_themes(self):
|
|
"""
|
|
:return: 7 популярных тем (где больше всего подписчиков)
|
|
"""
|
|
qs = Theme.objects.annotate(c=Count('contact'))\
|
|
.values('pk', 'c')\
|
|
.order_by('-c')[:7]
|
|
return Theme.objects.language()\
|
|
.filter(pk__in=[x['pk'] for x in qs])\
|
|
.order_by('name')
|
|
|
|
|
|
class ActivationView(TemplateView):
|
|
http_method_names = ['get']
|
|
template_name = 'registration/activate.html'
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
activated_contact = self.activate(request, *args, **kwargs)
|
|
# add to announce list
|
|
m = MailingList.objects.get(id=1)
|
|
m.subscribers.add(activated_contact)
|
|
if activated_contact:
|
|
success_url = self.get_success_url(request, activated_contact)
|
|
try:
|
|
to, args, kwargs = success_url
|
|
return redirect(to, *args, **kwargs)
|
|
except ValueError:
|
|
return redirect(success_url)
|
|
return super(ActivationView, self).get(request, *args, **kwargs)
|
|
|
|
def activate(self, request, activation_key):
|
|
"""
|
|
Implement account-activation logic here.
|
|
|
|
"""
|
|
activated_contact = Contact.objects.activate(activation_key)
|
|
return activated_contact
|
|
|
|
|
|
def get_success_url(self, request, user):
|
|
return ('subscription_activation_complete', (), {})
|
|
|
|
|
|
def popup_validate(request):
|
|
response = {'success': False}
|
|
if request.GET:
|
|
form = ContactForm(request.GET)
|
|
if form.is_valid():
|
|
contact = form.save()
|
|
contact.send_activation()
|
|
response['success'] = True
|
|
response['redirect'] = True
|
|
response['redirect_url'] = SubscribeView.success_url
|
|
else:
|
|
response['errors'] = form.errors
|
|
|
|
return HttpResponse(json.dumps(response, indent=2), content_type='application/json')
|
|
|
|
|
|
def landing_partisipation_validate(request):
|
|
response = {'success': False}
|
|
if request.method == 'POST' and request.is_ajax():
|
|
form = ContactForm(request.POST)
|
|
if form.is_valid():
|
|
contact = form.save()
|
|
contact.send_activation()
|
|
response['success'] = True
|
|
else:
|
|
response['errors'] = form.errors
|
|
return HttpResponse(json.dumps(response), content_type='application/json')
|
|
|