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.
176 lines
6.3 KiB
176 lines
6.3 KiB
# -*- coding: utf-8 -*-
|
|
import json
|
|
|
|
from django.core.urlresolvers import reverse_lazy
|
|
from django.views.generic import TemplateView, FormView
|
|
from django.http import HttpResponseRedirect, 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 (
|
|
SubscribeAssideForm, MailingSettingsForm
|
|
)
|
|
from accounts.models import User
|
|
from accounts.views import GetUserMixin
|
|
from functions.custom_views import ContextMixin
|
|
from city.models import City
|
|
|
|
|
|
class SubscribeView(GetUserMixin, ContextMixin, FormView):
|
|
form_class = ContactForm
|
|
template_name = 'client/newsletters/subcribe.html'
|
|
success_url = reverse_lazy('subscription_activation_send')
|
|
|
|
def get_form(self, form_class):
|
|
if self.request.POST:
|
|
email = self.request.POST.get('email')
|
|
if email:
|
|
try:
|
|
contact = Contact.objects.get(email=email)
|
|
return form_class(instance=contact,
|
|
**self.get_form_kwargs())
|
|
except Contact.DoesNotExist:
|
|
pass
|
|
|
|
return form_class(**self.get_form_kwargs())
|
|
else:
|
|
return form_class(**self.get_form_kwargs())
|
|
|
|
def form_valid(self, form):
|
|
contact = form.save()
|
|
contact.send_activation()
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
def get_initial(self):
|
|
data = super(SubscribeView, self).get_initial()
|
|
if self.request.user.is_authenticated():
|
|
email = getattr(self.request.user, 'email')
|
|
data['email'] = email
|
|
data['first_name'] = getattr(self.request.user, 'first_name')
|
|
if self.request.GET:
|
|
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_context_data(self, **kwargs):
|
|
ctx = super(SubscribeView, self).get_context_data(**kwargs)
|
|
ctx['mailsettings_object'] = self.get_mailsettings_object()
|
|
ctx['mailsettings_form'] = MailingSettingsForm(
|
|
instance=self.get_user()
|
|
)
|
|
return ctx
|
|
|
|
def get_mailsettings_object(self):
|
|
"""
|
|
передаём контекст в шаблон по городам, странам, а так же выбранным
|
|
:return: instance of mail settings
|
|
"""
|
|
self.extra_ctx.update({
|
|
'r_cities': City.used.russia(),
|
|
})
|
|
instance = self.get_user()
|
|
if instance is not None:
|
|
self.extra_ctx.update({
|
|
'checked_f_countries': list(
|
|
instance.f_countries.values_list('pk', flat=True)),
|
|
'checked_r_cities': list(
|
|
instance.r_cities.values_list('pk', flat=True)),
|
|
'checked_tg': list(instance.tags.values_list('pk', flat=True)),
|
|
'checked_th': list(
|
|
instance.themes.values_list('pk', flat=True)),
|
|
'contact': instance,
|
|
})
|
|
if not instance.subscriber:
|
|
self.extra_ctx.update({'unsubscribed': True})
|
|
return instance
|
|
|
|
|
|
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 subscribe_aside(request):
|
|
if request.POST:
|
|
response = {'success': False}
|
|
form = SubscribeAssideForm(request.POST)
|
|
if form.is_valid():
|
|
email = form.cleaned_data['email']
|
|
try:
|
|
user = User.objects.get(username=email)
|
|
except User.DoesNotExist:
|
|
user = None
|
|
contact = Contact.objects.create_contact(email, user)
|
|
contact_setting = form.save(commit=False)
|
|
contact_setting.contact = contact
|
|
contact_setting.exponent_practicum = True
|
|
contact_setting.organiser_practicum = True
|
|
contact_setting.save()
|
|
form.save_m2m()
|
|
response['success'] = True
|
|
else:
|
|
errors = form.errors
|
|
if 'theme' in errors:
|
|
errors['id_subscription_theme'] = errors['theme']
|
|
response['errors'] = errors
|
|
return HttpResponse(json.dumps(response, indent=2), content_type='application/json')
|
|
else:
|
|
return HttpResponse('not form request')
|
|
|
|
|
|
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')
|
|
|