dynamicweb/membership/views.py

114 lines
4.2 KiB
Python
Raw 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.contrib.auth import authenticate, login
from django.views.decorators.cache import cache_control
from django.conf import settings
from django.db.models import Q
2016-03-03 21:55:23 +00:00
2016-03-07 16:49:02 +00:00
from .models import CustomUser
2016-03-11 18:42:45 +00:00
from .forms import (LoginForm, RegisterForm, PaymentForm)
from .payment import StripePayment
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-11 18:42:45 +00:00
return render(request, 'templates/validated.html')
2016-03-07 16:49:02 +00:00
else:
2016-03-11 18:42:45 +00:00
return render(request, 'templates/error.html')
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:
msg = StripePayment.make_payment(request.user, request.session['amount'],
request.session['token'],request.session['time'])
template = 'templates/validated.html'
context['msg'] = msg
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
amount = amount * 100 # payments are in 'cents'
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):
email = request.POST.get('email')
password = request.POST.get('password')
name = request.POST.get('name')
if name and email and password:
2016-03-11 18:42:45 +00:00
user = CustomUser.register(name, password, email)
2016-03-07 16:49:02 +00:00
if user:
return render(request, 'templates/success.html')
else:
return render(request, 'templates/error.html')
elif email and password and not name:
2016-03-07 18:25:01 +00:00
user = authenticate(email=email, password=password)
2016-03-07 16:49:02 +00:00
if user:
2016-03-11 18:42:45 +00:00
login(request, user)
2016-03-07 16:49:02 +00:00
return redirect('membership')
else:
2016-03-11 18:42:45 +00:00
return redirect('login')
2016-03-07 16:49:02 +00:00
class MembershipView(View):
2016-03-11 18:42:45 +00:00
def get(self, request):
#if the user has payed allready
member_payed = request.user.creditcards_set.filter(Q(payment_type='month') | Q(payment_type='year'))
if member_payed:
return redirect('/')
request.session['next'] = 0
return render(request, 'templates/membership.html')