dynamicweb/membership/views.py

135 lines
5.2 KiB
Python
Raw Permalink Normal View History

2016-03-07 16:49:02 +00:00
from django.shortcuts import render, redirect
from django.views.generic import View
2016-03-11 18:42:45 +00:00
from django.views.decorators.cache import cache_control
from django.conf import settings
from django.db.models import Q
2016-04-23 17:00:20 +00:00
from django.utils.translation import get_language
from django.contrib.auth import logout
from django.http import HttpResponseRedirect
2016-04-28 17:42:01 +00:00
from django.contrib.auth import login
2016-03-03 21:55:23 +00:00
2016-05-01 12:13:12 +00:00
from .models import CustomUser,StripeCustomer
from .forms import LoginForm, RegisterForm, PaymentForm
2016-05-01 12:13:12 +00:00
from utils.stripe_utils import StripeUtils
2016-03-07 16:49:02 +00:00
2016-03-07 16:49:02 +00:00
def validate_email(request, validate_slug):
2016-03-11 18:42:45 +00:00
validated = CustomUser.validate_url(validate_slug)
2016-03-07 16:49:02 +00:00
if validated:
2016-03-16 14:10:48 +00:00
return render(request, 'templates/validated_email.html',{'msg':True})
2016-03-07 16:49:02 +00:00
else:
2016-03-16 14:10:48 +00:00
return render(request, 'templates/error.html',{'msg':'Validation failed.'})
2016-03-11 18:42:45 +00:00
def reset(request,time):
request.session['next']=0
return redirect('payment',time=time)
class CreditCardView(View):
def _get_context(self, request, time):
request.session['time'] = time
context = {}
context['name'] = request.user.name
if time == 'month':
context['time'] = "1 month"
context['price'] = "35"
context['free'] = "1"
elif time == 'year':
context['time'] = '1 year'
context['price'] = '360'
context['free'] = "2"
context['stripe_key'] = settings.STRIPE_API_PUBLIC_KEY
context['form'] = PaymentForm()
return context
@cache_control(no_cache=True,must_revalidate=True)
def get(self, request, time=None):
context = self._get_context(request, time)
next = request.session.get('next')
if next == 1 or next ==0:
template = 'templates/creditcard.html'
request.session['next'] +=1
elif next == 2:
2016-05-01 12:13:12 +00:00
customer = StripeCustomer.get_or_create(email=request.user.email,token=request.session['token'])
stripe_utils = StripeUtils()
charge = stripe_utils.make_charge(request.session['amount'],customer=customer.stripe_id)
2016-03-11 18:42:45 +00:00
template = 'templates/validated.html'
2016-05-04 17:59:25 +00:00
resp = charge.get('response_object')
context['msg'] = resp.get('status',None)
2016-03-11 18:42:45 +00:00
request.session['next'] = None
return render(request, template, context)
def post(self, request, time=None):
form = PaymentForm(request.POST)
stripe_token = request.POST['stripeToken']
if form.is_valid():
ret = form.save(request.user)
amount = 35 if time == 'month' else 360
request.session['token'] = stripe_token
request.session['amount'] = amount
request.session['next'] +=1
return render(request, 'templates/confirm.html',
context={'name': request.user.name, 'email': request.user.email})
else:
context = self._get_context(request, time)
context['form'] = form
return render(request, 'templates/creditcard.html', context=context)
2016-03-07 16:49:02 +00:00
class LoginRegistrationView(View):
def get(self, request):
login_form = LoginForm()
register_form = RegisterForm()
2016-03-11 18:42:45 +00:00
request.session['next'] = None
2016-03-07 16:49:02 +00:00
if request.user.is_authenticated():
return redirect("membership")
else:
2016-03-11 18:42:45 +00:00
return render(request, 'templates/login.html',
{'login_form': login_form, 'register_form': register_form})
2016-03-07 16:49:02 +00:00
def post(self, request):
is_login = request.POST.get('is_login', False)
if not is_login:
form = RegisterForm(request.POST)
if form.is_valid():
email = form.validated_data.get('email')
password = form.validated_data.get('password')
name = form.validated_data.get('name')
user = CustomUser.register(name, password, email)
if user:
return render(request, 'templates/success.html')
else:
return render(request, 'templates/error.html')
2016-03-07 16:49:02 +00:00
else:
login_form = LoginForm()
return render(request, 'templates/login.html',
context={'login_form': login_form, 'register_form': form})
else:
2016-04-28 17:42:01 +00:00
form = LoginForm(request.POST)
if form.is_valid():
user = form.login(request)
if user:
login(request, user)
return redirect('membership')
2016-03-07 16:49:02 +00:00
else:
2016-04-28 17:42:01 +00:00
registration_form = RegisterForm()
return render(request, 'templates/login.html',
context={'login_form': form, 'register_form': registration_form})
2016-03-11 18:42:45 +00:00
2016-03-07 16:49:02 +00:00
class MembershipView(View):
2016-03-11 18:42:45 +00:00
def get(self, request):
2016-04-23 17:00:20 +00:00
#if the user has payed already
2016-03-11 18:42:45 +00:00
member_payed = request.user.creditcards_set.filter(Q(payment_type='month') | Q(payment_type='year'))
if member_payed:
return redirect('/')
request.session['next'] = 0
2016-04-23 17:00:20 +00:00
language = get_language()
return render(request, 'templates/membership.html',context={'language_code':language})
def logout_glarus(request):
logout(request)
2016-05-04 17:45:12 +00:00
return HttpResponseRedirect('/digitalglarus')