from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required from django.core.exceptions import ValidationError from django.db.models import Q # from users.models import User from .models import VendorsData from .forms import VendorEditForm from .functions import vendor_generator import uuid # Create your views here. @login_required(login_url='login') def index(request): context = {} return render(request, 'base/dashboard.html', context) def LoginPage(request): if request.method == 'POST': email = request.POST.get('email') password = request.POST.get('password') user = None error = None try: user = User.objects.get(email=email) except: error = messages.error(request, 'Email not registered') if user is not None and error is None: user = authenticate(request, email=email, password=password) if user is not None: login(request, user) return redirect('dashboard') else: messages.error(request, 'Wrong password') context = {} return render(request, 'base/login.html', context) def LogoutPage(request): logout(request) return redirect('login') @login_required(login_url='login') def VendorPage(request): vendors = VendorsData.objects.filter(Q(vendorid_id=request.user.id) & Q(vendorDeleted=False)) context = {'vendors': vendors} return render(request, 'base/vendor.html', context) @login_required(login_url='login') def VendorEditPage(request, vdr ): vendor = VendorsData.objects.get(vendorUUID=vdr) try: if vendor.vendorid_id is not request.user.id: messages.error(request, "You are not authorized to edit this vendor") return redirect('vendor') except Exception as e: messages.error(request, "Something wrong happened, try again.") return redirect('vendor') form = VendorEditForm() vendor_title = "Edit Vendor" if request.method == 'POST': if request.POST.get('submit') == 'Submit': try: vendors = VendorsData.objects.get(vendorUUID=vdr) form = VendorEditForm(request.POST, instance=vendors) if form.is_valid(): form.save() messages.success(request, "Vendor information saved") return redirect('vendor') else: messages.error(request, "Something wrong happened, try again.") except Exception as e: messages.error(request, "Something wrong happened, try again.") vendors = VendorsData.objects.get(vendorUUID=vdr) context = {'vendors': vendors, 'form': form, 'vendor_title': vendor_title} return render(request, 'base/vendorEdit.html', context) @login_required(login_url='login') def VendorCreatePage(request): vendor_title = "Create Vendor" if request.method == 'POST': try: form = VendorEditForm(request.POST) if form.is_valid(): # Generate vendor new_vendor = form.save(commit=False) try: vdr = vendor_generator() vendor_check = VendorsData.objects.get(vendorUUID=vdr) while vendor_check is vdr: vdr = vendor_generator() vendor_check = VendorsData.objects.get(vendorUUID=vdr) except: pass new_vendor.vendor = vdr new_vendor.vendorSecretKey = uuid.uuid4() new_vendor.vendorUUID = uuid.uuid4() new_vendor.vendorid_id = request.user.id # Save vendor try: new_vendor.save() messages.success(request, "Vendor created") return redirect('vendor') except Exception as e: print(e) else: messages.error(request, "Something wrong happened, try again.") except Exception as e: messages.error(request, "Something wrong happened, try again.") context = {'vendor_title': vendor_title} return render(request, 'base/vendorEdit.html', context)