Files
goodtimes/manage_referrals/views.py
2024-04-24 13:45:16 +05:30

475 lines
18 KiB
Python

from django.shortcuts import render, get_object_or_404, redirect
from accounts import resource_action
from goodtimes import constants
from django.urls import reverse_lazy
from django.views import generic
from django.utils import timezone
from django.contrib.auth.mixins import LoginRequiredMixin
from manage_referrals.forms import (
ReferralRecordForm,
ReferralRecordRewardForm,
GoodTimeCoinsForm,
ReferralTrackingForm,
)
from manage_referrals.models import (
ReferralCode,
ReferralRecord,
ReferralRecordReward,
GoodTimeCoins,
ReferralTracking,
)
from django.contrib import messages
# Create your views here.
class ReferralRecordCreateOrUpdateView(LoginRequiredMixin, generic.View):
# Set the page_name and resource
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
# Initialize the action as ACTION_CREATE (can change based on logic)
action = resource_action.ACTION_CREATE # Default action
template_name = "manage_referrals/record_add.html"
model = ReferralRecord
form_class = ReferralRecordForm
success_url = reverse_lazy("manage_referrals:record_list")
error_message = "An error occurred while saving the data."
# Determine the success message dynamically based on whether it's an update or create
def get_success_message(self):
self.success_message = (
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
)
return self.success_message
# Get the object (if exists) based on URL parameter 'pk'
def get_object(self):
pk = self.kwargs.get("pk")
return get_object_or_404(self.model, pk=pk) if pk else None
# Add page_name and operation to the context
def get_context_data(self, **kwargs):
context = {
"page_name": self.page_name,
"operation": "Add" if not self.object else "Edit",
}
context.update(kwargs) # Include any additional context data passed to the view
return context
def get(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
print("get method of article is called")
form = self.form_class(instance=self.object)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
print("post method is called")
form = self.form_class(request.POST, request.FILES, instance=self.object)
print("request with files", request.FILES)
if not form.is_valid():
print(form.errors)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
form.save()
messages.success(self.request, self.get_success_message())
return redirect(self.success_url)
class ReferralRecordView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_READ
model = ReferralRecord
template_name = "manage_referrals/record_list.html"
context_object_name = "records"
def get_queryset(self):
return super().get_queryset().filter(active=True, deleted=False)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
class ReferralCodeView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_READ
model = ReferralCode
template_name = "manage_referrals/code_list.html"
context_object_name = "code"
def get_queryset(self):
queryset = (
super().get_queryset().filter(active=True, deleted=False).order_by("-id")
)
print(f"is {queryset}")
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
class ReferralRecordDeleteView(LoginRequiredMixin, generic.View):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_DELETE
model = ReferralRecord
success_url = reverse_lazy("manage_referrals:record_list")
success_message = constants.RECORD_DELETED
error_message = constants.RECORD_NOT_FOUND
def get(self, request, pk):
try:
type_obj = self.model.objects.get(id=pk)
type_obj.deleted = True
type_obj.active = False
type_obj.save()
messages.success(request, self.success_message)
except self.model.DoesNotExist:
messages.success(request, self.error_message)
return redirect(self.success_url)
class ReferralRecordRewardCreateOrUpdateView(LoginRequiredMixin, generic.View):
# Set the page_name and resource
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
# Initialize the action as ACTION_CREATE (can change based on logic)
action = resource_action.ACTION_CREATE # Default action
template_name = "manage_referrals/reward_add.html"
model = ReferralRecordReward
form_class = ReferralRecordRewardForm
success_url = reverse_lazy("manage_referrals:reward_list")
error_message = "An error occurred while saving the data."
# Determine the success message dynamically based on whether it's an update or create
def get_success_message(self):
self.success_message = (
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
)
return self.success_message
# Get the object (if exists) based on URL parameter 'pk'
def get_object(self):
pk = self.kwargs.get("pk")
return get_object_or_404(self.model, pk=pk) if pk else None
# Add page_name and operation to the context
def get_context_data(self, **kwargs):
context = {
"page_name": self.page_name,
"operation": "Add" if not self.object else "Edit",
}
context.update(kwargs) # Include any additional context data passed to the view
return context
def get(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
print("get method of article is called")
form = self.form_class(instance=self.object)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
print("post method is called")
form = self.form_class(request.POST, request.FILES, instance=self.object)
print("request with files", request.FILES)
if not form.is_valid():
print(form.errors)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
form.save()
messages.success(self.request, self.get_success_message())
return redirect(self.success_url)
class ReferralRecordRewardView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_READ
model = ReferralRecordReward
template_name = "manage_referrals/reward_list.html"
context_object_name = "rewards"
def get_queryset(self):
return super().get_queryset().filter(active=True, deleted=False)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
class ReferralRecordRewardDeleteView(LoginRequiredMixin, generic.View):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_DELETE
model = ReferralRecordReward
success_url = reverse_lazy("manage_referrals:reward_list")
success_message = constants.RECORD_DELETED
error_message = constants.RECORD_NOT_FOUND
def get(self, request, pk):
try:
type_obj = self.model.objects.get(id=pk)
type_obj.deleted = True
type_obj.active = False
type_obj.save()
messages.success(request, self.success_message)
except self.model.DoesNotExist:
messages.success(request, self.error_message)
return redirect(self.success_url)
class GTCoinsCreateOrUpdateView(LoginRequiredMixin, generic.View):
# Set the page_name and resource
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
# Initialize the action as ACTION_CREATE (can change based on logic)
action = resource_action.ACTION_CREATE # Default action
template_name = "manage_referrals/coin_add.html"
model = GoodTimeCoins
form_class = GoodTimeCoinsForm
success_url = reverse_lazy("manage_referrals:coin_list")
error_message = "An error occurred while saving the data."
# Determine the success message dynamically based on whether it's an update or create
def get_success_message(self):
self.success_message = (
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
)
return self.success_message
# Get the object (if exists) based on URL parameter 'pk'
def get_object(self):
pk = self.kwargs.get("pk")
return get_object_or_404(self.model, pk=pk) if pk else None
# Add page_name and operation to the context
def get_context_data(self, **kwargs):
context = {
"page_name": self.page_name,
"operation": "Add" if not self.object else "Edit",
}
context.update(kwargs) # Include any additional context data passed to the view
return context
def get(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
print("get method of article is called")
form = self.form_class(instance=self.object)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
else:
# We are adding a new object, not updating
# Find the most recent GoodTimeCoins object
previous_object = GoodTimeCoins.objects.filter(deleted=False).last()
# If a previous object exists, update its end_date to today
if previous_object:
previous_object.end_date = timezone.now().date()
previous_object.save()
form = self.form_class(request.POST, instance=self.object)
if not form.is_valid():
print(form.errors)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
# Save the form (and by extension, the object) as usual
current_object = form.save(commit=False)
# Only set the start_date for a new object
if not self.object:
current_object.start_date = (
timezone.now().date()
) # Set start_date to today for new records only
current_object.save()
messages.success(self.request, self.get_success_message())
return redirect(self.success_url)
class GTCoinsView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_READ
model = GoodTimeCoins
template_name = "manage_referrals/coin_list.html"
context_object_name = "coins"
def get_queryset(self):
return super().get_queryset().filter(active=True, deleted=False)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
class GTCoinsDeleteView(LoginRequiredMixin, generic.View):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_DELETE
model = GoodTimeCoins
success_url = reverse_lazy("manage_referrals:coin_list")
success_message = constants.RECORD_DELETED
error_message = constants.RECORD_NOT_FOUND
def get(self, request, pk):
try:
type_obj = self.model.objects.get(id=pk)
if type_obj.end_date is not None:
type_obj.deleted = True
type_obj.active = False
type_obj.save()
messages.success(request, self.success_message)
else:
messages.warning(
request, "Cannot delete current GT coin"
)
except self.model.DoesNotExist:
messages.warning(request, self.error_message)
return redirect(self.success_url)
class ReferralTrackingCreateOrUpdateView(LoginRequiredMixin, generic.View):
# Set the page_name and resource
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
# Initialize the action as ACTION_CREATE (can change based on logic)
action = resource_action.ACTION_CREATE # Default action
template_name = "manage_referrals/track_add.html"
model = ReferralTracking
form_class = ReferralTrackingForm
success_url = reverse_lazy("manage_referrals:track_list")
error_message = "An error occurred while saving the data."
# Determine the success message dynamically based on whether it's an update or create
def get_success_message(self):
self.success_message = (
constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
)
return self.success_message
# Get the object (if exists) based on URL parameter 'pk'
def get_object(self):
pk = self.kwargs.get("pk")
return get_object_or_404(self.model, pk=pk) if pk else None
# Add page_name and operation to the context
def get_context_data(self, **kwargs):
context = {
"page_name": self.page_name,
"operation": "Add" if not self.object else "Edit",
}
context.update(kwargs) # Include any additional context data passed to the view
return context
def get(self, request, *args, **kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
print("get method of article is called")
form = self.form_class(instance=self.object)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
def post(self, request, args, *kwargs):
self.object = self.get_object()
# If an object is found, change action to ACTION_UPDATE
if self.object is not None:
self.action = resource_action.ACTION_UPDATE
print("post method is called")
form = self.form_class(request.POST, request.FILES, instance=self.object)
print("request with files", request.FILES)
if not form.is_valid():
print(form.errors)
context = self.get_context_data(form=form)
return render(request, self.template_name, context=context)
form.save()
messages.success(self.request, self.get_success_message())
return redirect(self.success_url)
class ReferralTrackingView(LoginRequiredMixin, generic.ListView):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_READ
model = ReferralTracking
template_name = "manage_referrals/track_list.html"
context_object_name = "tracks"
def get_queryset(self):
return super().get_queryset().filter(active=True, deleted=False)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["page_name"] = self.page_name
return context
class ReferralTrackingDeleteView(LoginRequiredMixin, generic.View):
page_name = resource_action.RESOURCE_MANAGE_REFERRALS
resource = resource_action.RESOURCE_MANAGE_REFERRALS
action = resource_action.ACTION_DELETE
model = ReferralTracking
success_url = reverse_lazy("manage_referrals:track_list")
success_message = constants.RECORD_DELETED
error_message = constants.RECORD_NOT_FOUND
def get(self, request, pk):
try:
type_obj = self.model.objects.get(id=pk)
type_obj.deleted = True
type_obj.active = False
type_obj.save()
messages.success(request, self.success_message)
except self.model.DoesNotExist:
messages.success(request, self.error_message)
return redirect(self.success_url)