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.utils.translation import gettext_lazy as _ from django.db.models import Q from django_otp.decorators import otp_required # from users.models import User from .models import VendorsData, CryptoCoins, VendorsAddresses from .forms import VendorEditForm, VendorAddrAddForm from .functions import vendor_generator, checksumCheck import uuid, hashlib # Create your views here. @login_required(login_url='login') #@otp_required() 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/loogin2.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') #@otp_required(login_url='two_factor:login') def VendorEditPage(request, vdr ): try: vendors = VendorsData.objects.get(Q(vendorid_id=request.user.id) & Q(vendorUUID=vdr)) form = VendorEditForm() vendor_title = "Edit Vendor" if request.method == 'POST': if request.POST.get('submit') == 'Submit': try: vendors = VendorsData.objects.get(Q(vendorid_id=request.user.id) & Q(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.") context = {'vendors': vendors, 'form': form, 'vendor_title': vendor_title} return render(request, 'base/vendorEdit.html', context) except Exception as e: messages.error(request, e) return redirect('vendor') @login_required(login_url='login') def VendorCreatePage(request): vendor_title = "Create Vendor" context = {'vendor_title': vendor_title} if request.method == 'POST': try: try: vendor_accounts = len(VendorsData.objects.filter(vendorid_id=request.user.id)) if vendor_accounts > request.user.vendornr: messages.error(request, "You have reached the maximum number of vendor accounts") return redirect('vendor') except: pass 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 secretKey = str(uuid.uuid4()) md5s = hashlib.md5(secretKey.encode()).hexdigest() new_vendor.vendor = vdr new_vendor.vendorSecretKey = md5s new_vendor.vendorSkipScreen = False new_vendor.vendorUUID = uuid.uuid4() new_vendor.vendorid_id = request.user.id # Save vendor try: new_vendor.save() vendor_title = "Congrats!" context = {'vendor_title': vendor_title, "vendor": new_vendor, 'secret': secretKey} return render(request, 'base/vendorSuccess.html', context) except Exception: messages.error(request, "Unable to create vendor") # else: messages.error(request, "The information submited is incomplete") # except Exception as e: messages.error(request, "Something wrong happened, try again.") return render(request, 'base/vendorCreate.html', context) @login_required(login_url='login') def VendorAddrPage(request,vdr): try: coins = CryptoCoins.objects.filter(coinIsActive=True) try: vendors = VendorsData.objects.get(Q(vendorid_id=request.user.id) & Q(vendorUUID=vdr)) except: messages.error(request, "Vendor not found") return redirect('vendor') addresses = VendorsAddresses.objects.filter(Q(vendorUUID=vdr) & Q(vendorid_id=request.user.id)) form = VendorAddrAddForm() vendor_title = "Add a crypto address" if request.method == 'POST': if request.POST.get('submit') == 'Submit': try: if checksumCheck(request.POST.get('coin').lower(), request.POST.get('address')) == False: messages.error(request, "Invalid address") context = {'vendor_title': vendor_title, 'coins': coins, 'vendors': vendors, "addresses": addresses} return render(request, 'base/vendorAddr.html', context) except: # stop here, return nothing, notify us. return redirect('vendorAddr', vdr) ##check if duplicate try: form = VendorAddrAddForm(request.POST) if form.is_valid(): new_address = form.save(commit=False) new_address.vendorUUID = vdr new_address.addrDeleted = False new_address.addrIsActive = True new_address.vendor_id = vendors.id new_address.vendorid_id = request.user.id try: for obj in VendorsAddresses.objects.filter(Q(coin=request.POST.get('coin')) & Q(addrIsActive=True) & Q(vendorid_id=request.user.id)): obj.addrIsActive = False obj.save() new_address.save() messages.success(request, "Vendor address saved") #add email notification context = {'vendor_title': vendor_title, 'coins': coins, 'vendors': vendors, "addresses": addresses} return render(request, 'base/vendorAddr.html', context) except Exception as e: messages.error(request, 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, 'coins': coins, 'vendors': vendors, "addresses": addresses} return render(request, 'base/vendorAddr.html', context) except Exception as e: messages.error(request, "Something wrong happened, try again!") return redirect('vendor')