Edited membership_order_list.html to allow “become a member” option.Edited membership_order_list.html for new deactivate membership flow.Fixed get_current_membership function .Fixed Reactivated membership view.Fixed Mixin to know if an user is an active member or not.
This commit is contained in:
parent
ba2e231078
commit
56776e8894
5 changed files with 105 additions and 14 deletions
|
@ -1,5 +1,11 @@
|
||||||
|
from django.core.urlresolvers import reverse_lazy
|
||||||
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
|
from django.views.generic import UpdateView
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
|
from django.contrib import messages
|
||||||
|
|
||||||
|
|
||||||
from membership.models import StripeCustomer
|
from membership.models import StripeCustomer
|
||||||
from utils.models import BillingAddress
|
from utils.models import BillingAddress
|
||||||
|
|
||||||
|
@ -50,3 +56,27 @@ class Ordereable(models.Model):
|
||||||
instance.cc_brand = stripe_charge.source.brand
|
instance.cc_brand = stripe_charge.source.brand
|
||||||
instance.save()
|
instance.save()
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
|
||||||
|
class ChangeMembershipStatusMixin(LoginRequiredMixin, UpdateView):
|
||||||
|
success_message = None
|
||||||
|
success_url = reverse_lazy('digitalglarus:membership_orders_list')
|
||||||
|
login_url = reverse_lazy('digitalglarus:login')
|
||||||
|
fields = '__all__'
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
from .models import MembershipOrder
|
||||||
|
membership_order = MembershipOrder.objects.\
|
||||||
|
filter(customer__user=self.request.user).last()
|
||||||
|
if not membership_order:
|
||||||
|
raise AttributeError("Membership does not exists")
|
||||||
|
membership = membership_order.membership
|
||||||
|
return membership
|
||||||
|
|
||||||
|
def post(self, *args, **kwargs):
|
||||||
|
membership = self.get_object()
|
||||||
|
membership.activate()
|
||||||
|
|
||||||
|
messages.add_message(self.request, messages.SUCCESS, self.success_message)
|
||||||
|
|
||||||
|
return HttpResponseRedirect(self.success_url)
|
||||||
|
|
|
@ -65,6 +65,19 @@ class Membership(models.Model):
|
||||||
start_date = models.DateField()
|
start_date = models.DateField()
|
||||||
end_date = models.DateField()
|
end_date = models.DateField()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_current_membership(cls, user):
|
||||||
|
|
||||||
|
has_order_current_month = Q(membershiporder__customer__user=user,
|
||||||
|
membershiporder__created_at__month=datetime.today().month)
|
||||||
|
# import pdb;pdb.set_trace()
|
||||||
|
return cls.objects.\
|
||||||
|
filter(has_order_current_month).last()
|
||||||
|
|
||||||
|
# def get_current_active_membership(cls, user):
|
||||||
|
# membership = cls.get_current_membership(user)
|
||||||
|
# return membership if membership and membership.active else None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_by_user(cls, user):
|
def get_by_user(cls, user):
|
||||||
return cls.objects.\
|
return cls.objects.\
|
||||||
|
@ -75,15 +88,22 @@ class Membership(models.Model):
|
||||||
instance = cls.objects.create(**data)
|
instance = cls.objects.create(**data)
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def activate_or_crete(cls, data, user):
|
||||||
|
membership = cls.get_by_user(user)
|
||||||
|
obj, created = cls.objects.update_or_create(id=membership.id, defaults=data)
|
||||||
|
return obj
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def is_digitalglarus_active_member(cls, user):
|
def is_digitalglarus_active_member(cls, user):
|
||||||
past_month = (datetime.today() - relativedelta(months=1)).month
|
# past_month = (datetime.today() - relativedelta(months=1)).month
|
||||||
has_order_current_month = Q(membershiporder__customer__user=user,
|
has_order_current_month = Q(membershiporder__customer__user=user,
|
||||||
membershiporder__created_at__month=datetime.today().month)
|
membershiporder__created_at__month=datetime.today().month)
|
||||||
has_order_past_month = Q(membershiporder__customer__user=user,
|
# has_order_past_month = Q(membershiporder__customer__user=user,
|
||||||
membershiporder__created_at__month=past_month)
|
# membershiporder__created_at__month=past_month)
|
||||||
active_membership = Q(active=True)
|
active_membership = Q(active=True)
|
||||||
return cls.objects.filter(has_order_past_month | has_order_current_month).\
|
# return cls.objects.filter(has_order_past_month | has_order_current_month).\
|
||||||
|
return cls.objects.filter(has_order_current_month).\
|
||||||
filter(active_membership).exists()
|
filter(active_membership).exists()
|
||||||
|
|
||||||
def update_dates(self, start_date, end_date):
|
def update_dates(self, start_date, end_date):
|
||||||
|
@ -95,6 +115,10 @@ class Membership(models.Model):
|
||||||
self.active = False
|
self.active = False
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
def activate(self):
|
||||||
|
self.active = True
|
||||||
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
class MembershipOrder(Ordereable, models.Model):
|
class MembershipOrder(Ordereable, models.Model):
|
||||||
membership = models.ForeignKey(Membership)
|
membership = models.ForeignKey(Membership)
|
||||||
|
|
|
@ -59,22 +59,40 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<hr class="greyline-long">
|
<hr class="greyline-long">
|
||||||
<h2 class="order-head">Your Next Membership</h2>
|
<h2 class="order-head">Your Next Membership</h2>
|
||||||
{% if next_membership_start_date and next_membership_end_date%}
|
{% if next_membership_start_date and next_membership_end_date and current_membership.active %}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<h2 class="history-name">
|
<h2 class="history-name">
|
||||||
Dates: {{next_membership_start_date|date}} - {{next_membership_end_date|date}}<br>
|
Dates: {{next_membership_start_date|date}} - {{next_membership_end_date|date}}<br>
|
||||||
</h2>
|
</h2>
|
||||||
|
{% elif current_membership.active == False %}
|
||||||
|
<h2 class="history-name">
|
||||||
|
Your membership is deactivated
|
||||||
|
</h2>
|
||||||
{% else %}
|
{% else %}
|
||||||
<h2 class="history-name">
|
<h2 class="history-name">
|
||||||
You are not a member yet
|
You are not a member.
|
||||||
</h2>
|
</h2>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<div class="edit-button">
|
{% if not current_membership == None %}
|
||||||
|
{% if current_membership.active %}
|
||||||
<a class="btn btn-primary btn-grey btn-deactivate print" href="{% url 'digitalglarus:membership_deactivate' %}">Deactivate</a>
|
<div class="edit-button">
|
||||||
</div>
|
<a class="btn btn-primary btn-grey btn-deactivate print" href="{% url 'digitalglarus:membership_deactivate' %}">Deactivate</a>
|
||||||
|
</div>
|
||||||
|
{% elif not current_membership.active %}
|
||||||
|
<form method="POST" action="{% url 'digitalglarus:membership_reactivate' %}">
|
||||||
|
{% csrf_token %}
|
||||||
|
<div class="edit-button">
|
||||||
|
<button type="submit" class="btn btn-primary btn-grey btn-deactivate print" href="{% url 'digitalglarus:membership_reactivate' %}">Reactivate</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{% endif %}
|
||||||
|
{% else %}
|
||||||
|
<div class="edit-button">
|
||||||
|
<a class="btn btn-primary btn-blue" href="{% url 'digitalglarus:membership_pricing' %}">Become a member</a>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
<hr class="greyline-long">
|
<hr class="greyline-long">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-12 notice-box">
|
<div class="col-md-12 notice-box">
|
||||||
|
|
|
@ -6,7 +6,8 @@ from .views import ContactView, IndexView, AboutView, HistoryView, LoginView, Si
|
||||||
PasswordResetView, PasswordResetConfirmView, MembershipPaymentView, MembershipActivatedView,\
|
PasswordResetView, PasswordResetConfirmView, MembershipPaymentView, MembershipActivatedView,\
|
||||||
MembershipPricingView, BookingSelectDatesView, BookingPaymentView, OrdersBookingDetailView,\
|
MembershipPricingView, BookingSelectDatesView, BookingPaymentView, OrdersBookingDetailView,\
|
||||||
BookingOrdersListView, MembershipOrdersListView, OrdersMembershipDetailView, \
|
BookingOrdersListView, MembershipOrdersListView, OrdersMembershipDetailView, \
|
||||||
MembershipDeactivateView, MembershipDeactivateSuccessView, UserBillingAddressView
|
MembershipDeactivateView, MembershipDeactivateSuccessView, UserBillingAddressView, \
|
||||||
|
MembershipReactivateView
|
||||||
# from membership.views import LoginRegistrationView
|
# from membership.views import LoginRegistrationView
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
|
@ -33,6 +34,8 @@ urlpatterns = [
|
||||||
name='membership_activated'),
|
name='membership_activated'),
|
||||||
url(_(r'membership/deactivate/?$'), MembershipDeactivateView.as_view(),
|
url(_(r'membership/deactivate/?$'), MembershipDeactivateView.as_view(),
|
||||||
name='membership_deactivate'),
|
name='membership_deactivate'),
|
||||||
|
url(_(r'membership/reactivate/?$'), MembershipReactivateView.as_view(),
|
||||||
|
name='membership_reactivate'),
|
||||||
url(_(r'membership/deactivate/success/?$'), MembershipDeactivateSuccessView.as_view(),
|
url(_(r'membership/deactivate/success/?$'), MembershipDeactivateSuccessView.as_view(),
|
||||||
name='membership_deactivate_success'),
|
name='membership_deactivate_success'),
|
||||||
url(_(r'membership/pricing/?$'), MembershipPricingView.as_view(),
|
url(_(r'membership/pricing/?$'), MembershipPricingView.as_view(),
|
||||||
|
|
|
@ -17,6 +17,7 @@ from django.views.generic import View, DetailView, ListView
|
||||||
|
|
||||||
|
|
||||||
from .models import Supporter
|
from .models import Supporter
|
||||||
|
from .mixins import ChangeMembershipStatusMixin
|
||||||
from utils.forms import ContactUsForm
|
from utils.forms import ContactUsForm
|
||||||
from utils.mailer import BaseEmail
|
from utils.mailer import BaseEmail
|
||||||
|
|
||||||
|
@ -357,13 +358,14 @@ class MembershipPaymentView(LoginRequiredMixin, IsNotMemberMixin, FormView):
|
||||||
# Get membership dates
|
# Get membership dates
|
||||||
membership_start_date, membership_end_date = membership_type.first_month_range
|
membership_start_date, membership_end_date = membership_type.first_month_range
|
||||||
|
|
||||||
# Create membership plan
|
# Create or update membership plan
|
||||||
membership_data = {
|
membership_data = {
|
||||||
'type': membership_type,
|
'type': membership_type,
|
||||||
|
'active': True,
|
||||||
'start_date': membership_start_date,
|
'start_date': membership_start_date,
|
||||||
'end_date': membership_end_date
|
'end_date': membership_end_date
|
||||||
}
|
}
|
||||||
membership = Membership.create(membership_data)
|
membership = Membership.activate_or_crete(membership_data, self.request.user)
|
||||||
|
|
||||||
# Create membership order
|
# Create membership order
|
||||||
order_data = {
|
order_data = {
|
||||||
|
@ -446,6 +448,16 @@ class MembershipDeactivateView(LoginRequiredMixin, UpdateView):
|
||||||
return HttpResponseRedirect(self.success_url)
|
return HttpResponseRedirect(self.success_url)
|
||||||
|
|
||||||
|
|
||||||
|
class MembershipReactivateView(ChangeMembershipStatusMixin):
|
||||||
|
success_message = "Your membership has been reactivate :)"
|
||||||
|
template_name = "digitalglarus/membership_orders_list.html"
|
||||||
|
|
||||||
|
def post(self, request, *args, **kwargs):
|
||||||
|
membership = self.get_object()
|
||||||
|
membership.activate()
|
||||||
|
return super(MembershipReactivateView, self).post(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class UserBillingAddressView(LoginRequiredMixin, UpdateView):
|
class UserBillingAddressView(LoginRequiredMixin, UpdateView):
|
||||||
model = UserBillingAddress
|
model = UserBillingAddress
|
||||||
form_class = UserBillingAddressForm
|
form_class = UserBillingAddressForm
|
||||||
|
@ -504,12 +516,16 @@ class MembershipOrdersListView(LoginRequiredMixin, ListView):
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(MembershipOrdersListView, self).get_context_data(**kwargs)
|
context = super(MembershipOrdersListView, self).get_context_data(**kwargs)
|
||||||
start_date, end_date = MembershipOrder.current_membership_dates(self.request.user)
|
current_membership = Membership.get_current_membership(self.request.user)
|
||||||
|
start_date, end_date = (current_membership.start_date, current_membership.end_date)\
|
||||||
|
if current_membership else [None, None]
|
||||||
|
|
||||||
next_start_date, next_end_date = MembershipOrder.next_membership_dates(self.request.user)
|
next_start_date, next_end_date = MembershipOrder.next_membership_dates(self.request.user)
|
||||||
current_billing_address = self.request.user.billing_addresses.filter(current=True).last()
|
current_billing_address = self.request.user.billing_addresses.filter(current=True).last()
|
||||||
context.update({
|
context.update({
|
||||||
'membership_start_date': start_date,
|
'membership_start_date': start_date,
|
||||||
'membership_end_date': end_date,
|
'membership_end_date': end_date,
|
||||||
|
'current_membership': current_membership,
|
||||||
'next_membership_start_date': next_start_date,
|
'next_membership_start_date': next_start_date,
|
||||||
'next_membership_end_date': next_end_date,
|
'next_membership_end_date': next_end_date,
|
||||||
'billing_address': current_billing_address
|
'billing_address': current_billing_address
|
||||||
|
|
Loading…
Reference in a new issue