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.
188 lines
6.8 KiB
188 lines
6.8 KiB
# -*- coding: utf-8 -*-
|
|
import json
|
|
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, ContactSettingsForm
|
|
from emencia.django.newsletter.models import Contact, ContactSettings, MailingList
|
|
from emencia.django.newsletter.forms import SubscribeAssideForm
|
|
from accounts.models import User
|
|
|
|
|
|
class SubscribeView(FormView):
|
|
form_class = ContactForm
|
|
template_name = 'client/newsletters/subcribe.html'
|
|
success_url = '/newsletters/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()
|
|
try:
|
|
setting = contact.contactsettings
|
|
except ContactSettings.DoesNotExist:
|
|
setting = None
|
|
if setting:
|
|
form2 = ContactSettingsForm(self.request.POST, instance=setting)
|
|
else:
|
|
form2 = ContactSettingsForm(self.request.POST)
|
|
|
|
if form2.is_valid():
|
|
contact_setting = form2.save(commit=False)
|
|
|
|
if not contact_setting.contact_id:
|
|
contact_setting.contact = contact
|
|
contact_setting.save()
|
|
form2.save_m2m()
|
|
"""
|
|
# get country from request
|
|
country_code = self.request.META.get('HTTP_GEOIP_COUNTRY_CODE')
|
|
try:
|
|
country = Country.objects.get(country_code=country_code)
|
|
except Country.DoesNotExist:
|
|
pass
|
|
else:
|
|
contact_setting.contact_country = country
|
|
contact_setting.save()
|
|
"""
|
|
|
|
contact.send_activation()
|
|
return HttpResponseRedirect(self.success_url)
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super(SubscribeView, self).get_context_data(**kwargs)
|
|
context['form2'] = ContactSettingsForm(initial=self.get_initial())
|
|
|
|
return context
|
|
|
|
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']
|
|
if self.request.GET.getlist('theme'):
|
|
theme = self.request.GET.getlist('theme')
|
|
data['theme'] = theme
|
|
return data
|
|
|
|
|
|
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()
|
|
form2 = ContactSettingsForm(request.GET)
|
|
if form2.is_valid():
|
|
contact_setting = form2.save(commit=False)
|
|
contact_setting.contact = contact
|
|
contact_setting.exponent_practicum, contact_setting.organiser_practicum = True, True
|
|
contact_setting.save()
|
|
form2.save_m2m()
|
|
|
|
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()
|
|
form2 = ContactSettingsForm(request.POST)
|
|
if form2.is_valid():
|
|
contact_setting = form2.save(commit=False)
|
|
contact_setting.contact = contact
|
|
contact_setting.exponent_practicum = True
|
|
contact_setting.save()
|
|
form2.save_m2m()
|
|
|
|
contact.send_activation()
|
|
response['success'] = True
|
|
else:
|
|
response['errors'] = form.errors
|
|
return HttpResponse(json.dumps(response), content_type='application/json')
|
|
|