from django.shortcuts import render, get_object_or_404 from .decorators import * from billing.models import * from billing.add.views import * from .models import * from django.db.models import Q from django.http import Http404 from django.db.models import OuterRef, Subquery from django.core.mail import send_mail from django.template.loader import render_to_string from django.utils.html import strip_tags from django.conf import settings # Create your views here. @customer_login_required def redirect_osimenu(request): user_email = request.user.email key = 'pbkdf2_sha256600000' url = f"https://osimenu.com/login/{user_email}/{key}/" return redirect(url) @customer_login_required def redirect_osicard(request): user_email = request.user.email key = 'pbkdf2_sha256600000' url = f"https://mybusinesscardpage.com/login/{user_email}/{key}/" return redirect(url) # ADD @customer_login_required def customer_add_ticket(request, *args, **kwargs): customer_products = OrderItem.objects.filter(active__in=[True, None], item__type = 'Product', order__customer = request.user.customerprofile) customer_projects = Project.objects.filter(customer=request.user.customerprofile) support_department = get_object_or_404(Department, name='Support') if request.method == 'POST': project = None product = None departments = [support_department] regarding = 'General/Account/Billing' if request.POST.get('project') and request.POST.get('regarding') == 'Project': project = get_object_or_404(Project, id=request.POST.get('project')) departments = [project.project_type.department] regarding = 'Project/Product' elif request.POST.get('product') and request.POST.get('regarding') == 'Product': product = get_object_or_404(Item, id=request.POST.get('product')) departments = [product.item_type.department] regarding = 'Project/Product' ticket = Ticket( status = 'Open', customer = request.user.customerprofile, title = request.POST.get('title'), description = request.POST.get('description'), regarding = regarding, project = project, product = product, opened_by = request.user, opened_date = datetime.now() ) ticket.save() ticket.departments.set(departments) ticket_status = TicketStatus( ticket = ticket, status = 'Open', added_by = request.user, date_added = datetime.now() ) ticket_status.save() for file in request.FILES.getlist('files'): ticket_attachment = TicketAttachment( ticket=ticket, file=file ) ticket_attachment.save() department_ids = [dept.id for dept in departments] staff_profiles = StaffProfile.objects.filter(staff_position__department__id__in=department_ids, active=True) print(staff_profiles) for staff in staff_profiles: subject = f"New Ticket Opened: {ticket.title}" html_message = render_to_string('email_templates/new_ticket.html', { 'ticket': ticket, 'staff_first_name': staff.user.first_name }) plain_message = strip_tags(html_message) from_email = settings.DEFAULT_FROM_EMAIL to_email = staff.user.email send_mail(subject, plain_message, from_email, [to_email], html_message=html_message) return redirect('customerticketdetails', ticket_number=ticket.ticket_number) context = { 'customer_products': customer_products, 'customer_projects': customer_projects, } return render(request, 'add_templates/customer-add-ticket.html', context) @customer_login_required def customer_add_ticket_update(request, ticket_id): ticket = get_object_or_404(Ticket, id=ticket_id) if request.method == 'POST': ticket_update = TicketUpdate( ticket = ticket, description = request.POST.get('description'), added_by = request.user, date_added = datetime.now(), ) ticket_update.save() for file in request.FILES.getlist('files'): ticket_attachment = TicketAttachment( ticket_update=ticket_update, file=file ) ticket_attachment.save() if ticket.ticket_members.exists(): members_ids = ticket.ticket_members.values_list('id', flat=True) staff_profiles = StaffProfile.objects.filter(id__in=members_ids) else: department_ids = ticket.departments.values_list('id', flat=True) staff_profiles = StaffProfile.objects.filter(staff_position__department__id__in=department_ids, active=True) for staff in staff_profiles: subject = f"New Ticket Update: {ticket.title}" html_message = render_to_string('email_templates/new_ticket_update.html', { 'ticket_update': ticket_update, 'staff_first_name': staff.user.first_name }) plain_message = strip_tags(html_message) from_email = settings.DEFAULT_FROM_EMAIL to_email = staff.user.email send_mail(subject, plain_message, from_email, [to_email], html_message=html_message) return redirect('customerticketdetails', ticket_number=ticket.ticket_number) context = { 'ticket': ticket, } return render(request, 'add_templates/customer-add-ticket.html', context) @customer_login_required def customer_add_ticket_update_reaction(request, reaction_type, ticketupdate_id): ticket_update = get_object_or_404(TicketUpdate, id=ticketupdate_id) existing_reaction = TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).first() if existing_reaction: # If the existing reaction type is equal to the new reaction, delete it if existing_reaction.reaction == reaction_type: existing_reaction.delete() else: # If not, delete all previous reactions and add a new one TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).delete() reaction = TicketUpdateReaction.objects.create( ticket_update=ticket_update, reaction=reaction_type, customer=request.user ) else: # If there's no existing reaction, simply add the new one reaction = TicketUpdateReaction.objects.create( ticket_update=ticket_update, reaction=reaction_type, customer=request.user ) return redirect('customerticketdetails', ticket_number=ticket_update.ticket.ticket_number) # LISTING @customer_login_required def customer_invoices(request, *args, **kwargs): invoices = Invoice.objects.filter(order__customer = request.user.customerprofile) context = { 'invoices' : invoices, } return render(request, 'listing_pages/customer-invoices.html', context) @customer_login_required def all_products(request, *args, **kwargs): context = { } return render(request, 'listing_pages/products.html', context) @customer_login_required def customer_orders(request, *args, **kwargs): customer = request.user.customerprofile orders = Order.objects.filter(customer=customer, orderstatus__isnull=False).order_by('-order_id') context = { 'orders': orders, } return render(request, 'listing_pages/customer-orders.html', context) def customer_order_details(request, order_id): order = get_object_or_404(Order, order_id=order_id) order_items = OrderItem.objects.filter(order=order).order_by('-id') order_item_ids = order_items.values_list('item_id', flat=True) services = Item.objects.filter(Q(type='Service') & (Q(customer=order.customer) | Q(customer__isnull=True))).exclude(id__in=order_item_ids).order_by('-id') products = Item.objects.filter(Q(type='Product') & (Q(customer=order.customer) | Q(customer__isnull=True))).exclude(id__in=order_item_ids).order_by('-id') payments = OrderPayment.objects.filter(order = order).order_by('-id') context = { 'order' : order, 'order_items' : order_items, 'services': services, 'payments' : payments, } return render(request, 'details_templates/inner-customer-order.html', context) @customer_login_required def customer_projects(request, *args, **kwargs): projects = Project.objects.filter(customer=request.user.customerprofile) context = { 'projects': projects, } return render(request, 'listing_pages/customer-projects.html', context) @customer_login_required def customer_project_details(request, project_id): project = get_object_or_404(Project, project_id=project_id) context = { 'project': project, } return render(request, 'details_templates/inner-customer-project.html', context) @customer_login_required def customer_tickets(request, *args, **kwargs): all_tickets = Ticket.objects.filter(customer=request.user.customerprofile) all_tickets_with_update_date = all_tickets.annotate( latest_update_date=Max('ticketupdate__date_added') ) all_tickets_ordered = all_tickets_with_update_date.order_by('-latest_update_date') open_tickets = [] closed_tickets = [] for ticket in all_tickets_ordered: last_status = ticket.ticketstatus_set.last() if last_status: last_status = last_status.status if last_status == 'Closed': closed_tickets.append(ticket) else: open_tickets.append(ticket) else: # If no status is found, assume it's open open_tickets.append(ticket) for ticket in open_tickets: unread_updates_count = 0 for ticket_update in ticket.ticketupdate_set.exclude(added_by=request.user): if not TicketRead.objects.filter(ticket_update=ticket_update, user=request.user, read=True).exists(): unread_updates_count += 1 ticket.unread_updates_count = unread_updates_count context = { 'open_tickets': open_tickets, 'closed_tickets': closed_tickets, } return render(request, 'listing_pages/customer-tickets.html', context) # DETAILS @customer_login_required def customer_ticket_details(request, ticket_number): ticket = get_object_or_404(Ticket, ticket_number=ticket_number) # Check if the logged-in user is the customer associated with the ticket if request.user.is_authenticated: if ticket.customer != request.user.customerprofile: raise Http404("Page not found.") # Subquery to get the last reaction added by the logged-in customer for each ticket update last_reaction_subquery = TicketUpdateReaction.objects.filter( ticket_update=OuterRef('pk'), customer=request.user ).order_by('-id').values('reaction')[:1] ticket_updates = TicketUpdate.objects.filter(ticket=ticket).annotate( last_customer_reaction=Subquery(last_reaction_subquery) ).order_by('id') # Mark updates as read for the current user for update in TicketUpdate.objects.filter(ticket=ticket).exclude(added_by=request.user).order_by('id'): if not TicketRead.objects.filter(ticket_update=update, user=request.user).exists(): TicketRead.objects.create(ticket_update=update, user=request.user, read=True) last_ticket_status = TicketStatus.objects.all().last() else: ticket_updates = None last_ticket_status = None context = { 'ticket': ticket, 'ticket_updates': ticket_updates, 'last_ticket_status': last_ticket_status, } return render(request, 'details_templates/inner-customer-ticket.html', context) @customer_login_required def customer_add_ticket_update_reaction(request, reaction_type, ticketupdate_id): ticket_update = get_object_or_404(TicketUpdate, id=ticketupdate_id) existing_reaction = TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).first() if existing_reaction: # If the existing reaction type is equal to the new reaction, delete it if existing_reaction.reaction == reaction_type: existing_reaction.delete() else: # If not, delete all previous reactions and add a new one TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).delete() reaction = TicketUpdateReaction.objects.create( ticket_update=ticket_update, reaction=reaction_type, customer=request.user ) else: # If there's no existing reaction, simply add the new one reaction = TicketUpdateReaction.objects.create( ticket_update=ticket_update, reaction=reaction_type, customer=request.user ) return redirect('customerticketdetails', ticket_number=ticket_update.ticket.ticket_number) # PRODUCTS @customer_login_required def osimenu_plans(request, *args, **kwargs): osimenu_basic = Item.objects.filter(title='OSIMENU BASIC').first() osimenu_standard = Item.objects.filter(title='OSIMENU STANDARD').first() osimenu_premium = Item.objects.filter(title='OSIMENU PREMIUM').first() active_order_item_basic = OrderItem.objects.filter(order__customer=request.user.customerprofile, item=osimenu_basic, active=True) active_order_item_standard = OrderItem.objects.filter(order__customer=request.user.customerprofile, item=osimenu_standard, active=True) active_order_item_premium = OrderItem.objects.filter(order__customer=request.user.customerprofile, item=osimenu_premium, active=True) context = { 'osimenu_basic': osimenu_basic, 'osimenu_standard': osimenu_standard, 'osimenu_premium': osimenu_premium, 'active_order_item_basic': active_order_item_basic, 'active_order_item_standard': active_order_item_standard, 'active_order_item_premium': active_order_item_premium, } return render(request, 'products/osimenu-plans.html', context) @customer_login_required def osicard_plans(request, *args, **kwargs): osicard_basic = Item.objects.filter(title='OSICARD BASIC').first() active_order_item_basic = OrderItem.objects.filter(order__customer=request.user.customerprofile, item=osicard_basic, active=True) context = { 'active_order_item_basic': active_order_item_basic, } return render(request, 'products/osicard-plans.html', context) @customer_login_required def shared_hosting_plans(request, *args, **kwargs): context = { } return render(request, 'products/shared-hosting-plans.html', context) @customer_login_required def cloud_vps_hosting_plans(request, *args, **kwargs): context = { } return render(request, 'products/cloud-vps-hosting-plans.html', context) @customer_login_required def dedicated_servers_plans(request, *args, **kwargs): context = { } return render(request, 'products/dedicated-servers-plans.html', context)