Added DonatorStatus model in order to know if an donator has canceled or not his monthly donation, Now we create a DonatorStatus for the user after receiving his first donation. Added DonatorStatus view. Added donator_status.html in order to allow an user view his donation status , Added action to allow user to cancel his monthly donations. Now the user can logout using navbar. added Donation model to admin.Added command make_donations_charges in order to create stripe current monthly donations from all donators
This commit is contained in:
parent
cb520f6b58
commit
4580a75f89
22 changed files with 611 additions and 232 deletions
|
|
@ -1,15 +1,20 @@
|
|||
from django.views.generic import TemplateView, CreateView, FormView
|
||||
from django.views.generic import TemplateView, CreateView, FormView, DetailView, UpdateView
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.shortcuts import render
|
||||
from django.core.urlresolvers import reverse_lazy, reverse
|
||||
from django.contrib.auth import authenticate, login
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
|
||||
from membership.models import CustomUser
|
||||
|
||||
from membership.models import CustomUser, StripeCustomer
|
||||
from utils.stripe_utils import StripeUtils
|
||||
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin
|
||||
from utils.forms import PasswordResetRequestForm, BillingAddressForm
|
||||
from utils.forms import PasswordResetRequestForm
|
||||
|
||||
from .forms import LoginForm, SignupForm
|
||||
from .forms import LoginForm, SignupForm, DonationForm, DonationBillingForm
|
||||
from .models import Donation, DonatorStatus
|
||||
|
||||
|
||||
class LandingView(TemplateView):
|
||||
|
|
@ -74,7 +79,8 @@ class PasswordResetConfirmView(PasswordResetConfirmViewMixin):
|
|||
class DonationView(LoginRequiredMixin, FormView):
|
||||
template_name = 'nosystemd/donation.html'
|
||||
login_url = reverse_lazy('nosystemd:login')
|
||||
form_class = BillingAddressForm
|
||||
form_class = DonationBillingForm
|
||||
success_url = reverse_lazy('nosystemd:donations')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(DonationView, self).get_context_data(**kwargs)
|
||||
|
|
@ -84,86 +90,103 @@ class DonationView(LoginRequiredMixin, FormView):
|
|||
|
||||
return context
|
||||
|
||||
# def post(self, request, *args, **kwargs):
|
||||
# form = self.get_form()
|
||||
def post(self, request, *args, **kwargs):
|
||||
form = self.get_form()
|
||||
|
||||
# if form.is_valid():
|
||||
# context = self.get_context_data()
|
||||
# specifications = request.session.get('vm_specs')
|
||||
# vm_type = specifications.get('hosting_company')
|
||||
# vm = VirtualMachineType.objects.get(hosting_company=vm_type)
|
||||
# final_price = vm.calculate_price(specifications)
|
||||
if form.is_valid():
|
||||
context = self.get_context_data()
|
||||
token = form.cleaned_data.get('token')
|
||||
donation_amount = form.cleaned_data.get('donation_amount')
|
||||
|
||||
# plan_data = {
|
||||
# 'vm_type': vm,
|
||||
# 'cores': specifications.get('cores'),
|
||||
# 'memory': specifications.get('memory'),
|
||||
# 'disk_size': specifications.get('disk_size'),
|
||||
# 'configuration': specifications.get('configuration'),
|
||||
# 'price': final_price
|
||||
# }
|
||||
# token = form.cleaned_data.get('token')
|
||||
# Get or create stripe customer
|
||||
customer = StripeCustomer.get_or_create(email=self.request.user.email,
|
||||
token=token)
|
||||
if not customer:
|
||||
form.add_error("__all__", "Invalid credit card")
|
||||
return self.render_to_response(self.get_context_data(form=form))
|
||||
|
||||
# # Get or create stripe customer
|
||||
# customer = StripeCustomer.get_or_create(email=self.request.user.email,
|
||||
# token=token)
|
||||
# if not customer:
|
||||
# form.add_error("__all__", "Invalid credit card")
|
||||
# return self.render_to_response(self.get_context_data(form=form))
|
||||
# Create Billing Address
|
||||
billing_address = form.save()
|
||||
|
||||
# # Create Virtual Machine Plan
|
||||
# plan = VirtualMachinePlan.create(plan_data, request.user)
|
||||
# Make stripe charge to a customer
|
||||
stripe_utils = StripeUtils()
|
||||
stripe_utils.CURRENCY = 'usd'
|
||||
charge_response = stripe_utils.make_charge(amount=donation_amount,
|
||||
customer=customer.stripe_id)
|
||||
charge = charge_response.get('response_object')
|
||||
|
||||
# # Create Billing Address
|
||||
# billing_address = form.save()
|
||||
# Check if the payment was approved
|
||||
if not charge:
|
||||
context.update({
|
||||
'paymentError': charge_response.get('error'),
|
||||
'form': form
|
||||
})
|
||||
return render(request, self.template_name, context)
|
||||
|
||||
# # Create a Hosting Order
|
||||
# order = HostingOrder.create(vm_plan=plan, customer=customer,
|
||||
# billing_address=billing_address)
|
||||
# Create a donation
|
||||
charge = charge_response.get('response_object')
|
||||
donation_data = request.POST.copy()
|
||||
donation_data.update({
|
||||
'cc_brand': charge.source.brand,
|
||||
'stripe_charge_id': charge.id,
|
||||
'last4': charge.source.last4,
|
||||
'billing_address': billing_address.id,
|
||||
'donator': customer.id,
|
||||
'donation': donation_amount
|
||||
})
|
||||
donation_form = DonationForm(donation_data)
|
||||
if donation_form.is_valid():
|
||||
donation = donation_form.save()
|
||||
return HttpResponseRedirect(reverse('nosystemd:donations',
|
||||
kwargs={'pk': donation.id}))
|
||||
else:
|
||||
self.form_invalid(donation_form)
|
||||
|
||||
# # Make stripe charge to a customer
|
||||
# stripe_utils = StripeUtils()
|
||||
# charge_response = stripe_utils.make_charge(amount=final_price,
|
||||
# customer=customer.stripe_id)
|
||||
# charge = charge_response.get('response_object')
|
||||
|
||||
# # Check if the payment was approved
|
||||
# if not charge:
|
||||
# context.update({
|
||||
# 'paymentError': charge_response.get('error'),
|
||||
# 'form': form
|
||||
# })
|
||||
# return render(request, self.template_name, context)
|
||||
|
||||
# charge = charge_response.get('response_object')
|
||||
|
||||
# # Associate an order with a stripe payment
|
||||
# order.set_stripe_charge(charge)
|
||||
|
||||
# # If the Stripe payment was successed, set order status approved
|
||||
# order.set_approved()
|
||||
|
||||
# # Send notification to ungleich as soon as VM has been booked
|
||||
# # TODO send email using celery
|
||||
|
||||
# context = {
|
||||
# 'vm': plan,
|
||||
# 'order': order,
|
||||
# 'base_url': "{0}://{1}".format(request.scheme, request.get_host())
|
||||
|
||||
# }
|
||||
# email_data = {
|
||||
# 'subject': 'New VM request',
|
||||
# 'to': request.user.email,
|
||||
# 'context': context,
|
||||
# 'template_name': 'new_booked_vm',
|
||||
# 'template_path': 'hosting/emails/'
|
||||
# }
|
||||
# email = BaseEmail(**email_data)
|
||||
# email.send()
|
||||
|
||||
# return HttpResponseRedirect(reverse('hosting:orders', kwargs={'pk': order.id}))
|
||||
# else:
|
||||
# return self.form_invalid(form)
|
||||
else:
|
||||
return self.form_invalid(form)
|
||||
|
||||
|
||||
class DonationDetailView(LoginRequiredMixin, DetailView):
|
||||
template_name = "nosystemd/donation_detail.html"
|
||||
context_object_name = "donation"
|
||||
login_url = reverse_lazy('nosystemd:login')
|
||||
model = Donation
|
||||
|
||||
|
||||
class DonatorStatusDetailView(LoginRequiredMixin, TemplateView):
|
||||
template_name = "nosystemd/donator_status.html"
|
||||
login_url = reverse_lazy('nosystemd:login')
|
||||
model = DonatorStatus
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(DonatorStatusDetailView, self).get_context_data(**kwargs)
|
||||
context.update({
|
||||
'donator_status': self.request.user.donatorstatus
|
||||
if self.request.user.donatorstatus else None
|
||||
})
|
||||
return context
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
if not request.user.donatorstatus:
|
||||
HttpResponseRedirect('nosystemd:landing')
|
||||
return super(DonatorStatusDetailView, self).get(request, *args, **kwargs)
|
||||
|
||||
|
||||
class ChangeDonatorStatusDetailView(LoginRequiredMixin, UpdateView):
|
||||
template_name = "nosystemd/donator_status.html"
|
||||
context_object_name = "donator_status"
|
||||
login_url = reverse_lazy('nosystemd:login')
|
||||
model = DonatorStatus
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
return self.request.user.donatorstatus
|
||||
|
||||
def post(self, *args, **kwargs):
|
||||
donator_status = self.get_object()
|
||||
|
||||
donator_status.status = DonatorStatus.ACTIVE \
|
||||
if donator_status.status == DonatorStatus.CANCELED else DonatorStatus.CANCELED
|
||||
|
||||
donator_status.save()
|
||||
messages.success(self.request, 'Your monthly donation status has been changed.')
|
||||
return HttpResponseRedirect(reverse_lazy('nosystemd:donator_status'))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue