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.
98 lines
3.5 KiB
98 lines
3.5 KiB
from django.views.generic import TemplateView, View, FormView
|
|
from django.http import HttpResponseRedirect
|
|
from django.shortcuts import redirect
|
|
from emencia.django.newsletter.forms import ContactForm, ContactSettingsForm
|
|
from emencia.django.newsletter.models import Contact, ContactSettings
|
|
|
|
class SubscribeView(FormView):
|
|
form_class = ContactForm
|
|
template_name = 'client/newsletters/subcribe.html'
|
|
success_url = '/'
|
|
|
|
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:
|
|
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)
|
|
|
|
ccc = 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()
|
|
|
|
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)
|
|
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.
|
|
|
|
"""
|
|
# todo: add country and city from geoip
|
|
activated_contact = Contact.objects.activate(activation_key)
|
|
return activated_contact
|
|
|
|
|
|
def get_success_url(self, request, user):
|
|
return ('subscription_activation_complete', (), {})
|
|
|
|
|
|
|