Source code for addons.views

from django.http.response import HttpResponse, JsonResponse
from django.shortcuts import render,redirect,get_object_or_404
from core.models import Country,Region,SubRegion,Addition,Account,Style
from .models import Vintages,Addons
from django.core import serializers
from django.contrib import messages
import json
from django.db.models import Q
import uuid
from addons.functions import *

# Create your views here.

[docs] def getdata(request): country = Country.objects.all() vintages = Vintages.objects.all() context = {'country':country, } return render(request, 'addons/vintages.html',context)
[docs] def regions(request): country_select = request.GET.get('country') regions = Region.objects.filter(country_id=country_select) context = {'regions': regions} return render(request, 'partials/regions.html', context)
[docs] def subregions(request): #Used by vintages to get the subregions region_select = request.GET.get('regions') print('region select =',region_select) subregions = SubRegion.objects.filter(region_id=region_select) context = {'subregions': subregions} return render(request, 'partials/subregions.html', context)
[docs] def addon_subregions(request): #Used when adding the region to addons region_select = request.GET.get('regions') print('region select =',region_select) subregions = SubRegion.objects.filter(region_id=region_select) context = {'subregions': subregions} return render(request, 'partials/addon_subregions.html', context)
[docs] def years(request): subregion_select = request.GET.get('subregions') years = Vintages.objects.filter(subregion_id=subregion_select) # Note there can be multiple years for a vintage 1 for each type e.g. Red and White. # The html template only displays 1 year using ifchanged tag context = {'years': years} return render(request, 'partials/years.html', context)
[docs] def vintage_details(request): year_select = request.GET.get('years') # year_select returns the key of one vintage record # we use this to filter on year and subregion # which may return multiple vintage records year = Vintages.objects.get(id=year_select) print('year id = ',year.id) print('year = ',year) vintage = Vintages.objects.filter(year = year.year,subregion_id=year.subregion_id) print(vintage) # Assumption is that there will only be a max of 2 years per sub-region (Red and White). context = {'vintage': vintage} return render(request, 'partials/vintage_details.html', context)
[docs] def select_addon(request): # Display a list of all addon records waiting to be loaded # i.e an addition record but no addon : filtered by account # Filtered by system # get addon records cellar = Addition.objects.filter(account='1') for w in cellar: # check if addon record exists if w.addition_status == 'Drunk': continue else: try: Addons.objects.get(wine_id=w.id) except: # if not create one if is_valid_year(w.year): #check if year is valid Addons.objects.create(wine_id = w.id, load_status = '1') info = Addons.objects.select_related('wine').order_by('wine__country','wine__year') addon_count = info.count() context = {'cellar_count': addon_count, 'addons':info} return render(request, 'addons/update.html', context)
[docs] def add_subregion(request): # Used to return the subregion for the wine # Values returned are : Country,Reqion,Subregion and year if request.method == "POST": # validate form and if valid add wine print('Details posted =',request.POST) if request.POST['subregion']=='Select SubRegion': message = 'No subregion provided, you must select a subregion' messages.error(request, message) next = 'select_addon' return redirect(next) f_subregion = request.POST['subregion'] f_wine = request.POST['wine'] print('Details posted =',request.POST) f_region = request.POST['region'] item = Addition.objects.get(id=f_wine) print('item=',item) addon = Addons.objects.get(wine_id = item.id) print('addon=',addon) vintage = get_vintage(item.year,item.country,f_region,f_subregion) region = Region.objects.get(id=f_region) update_check = update_addon_details(item.id,addon.id,vintage.id) if update_check == 1: message = 'Vintage details added successfully' messages.success(request, message) else: message = 'Error adding vintage details' messages.error(request, message) next = 'select_addon' return redirect(next) # redirect to addon list after processing
[docs] def get_region(request,int): # wine selected is passed through the int parameter # enables us to get country and year for the wine # to populate the vintage details user needs to select region and subregion # display the add-region form and the process the detais processed print('int=',int) addon = Addons.objects.select_related('wine').get(pk=int) if request.method == "POST": # process region submission f_wine = request.POST['wine'] print('wine=',f_wine) item = Addition.objects.get(id=f_wine) f_region = request.POST['regions'] if f_region == 'Select Region': message = 'No region provided, you must select a region' messages.error(request, message) next = 'select_addon' return redirect(next) print('region=',f_region) region = Region.objects.get(id=f_region) subregions = SubRegion.objects.filter(region_id=f_region) subregions_count= subregions.count() if subregions_count >1 :# User needs to identify which subregion context = {'item': item, 'region':region, 'subregions':subregions, } return render(request, 'addons/add_subregions.html',context) else:# We now have all Addon details needed for our update subregions = SubRegion.objects.get(region_id=f_region) details = Vintages.objects.get(country=item.country,year=item.year,subregion=subregions.id) update_addon_details(item.id,addon.id,details.id) else: # Get region from user print('check vintage exists=',vintage_exists(addon.wine.country,addon.wine.year)) if vintage_exists(addon.wine.country_id,addon.wine.year) == 0: #display error message message = 'No vintage details found for this wine' messages.error(request, message) print('No vintage details found for this wine') next = 'select_addon' return redirect(next) else: regions = Region.objects.filter(country_id=addon.wine.country) subregions= '0' #initialise an empty queryset subregions = SubRegion.objects.filter(region_id = '99999') for t in regions: #print('id=',t.id) subregions = subregions | SubRegion.objects.filter(region_id = t.id) context = {'item': addon, 'regions':regions, 'subregions':subregions, } return render(request, 'addons/add_regions.html',context) next = 'update_addons' return redirect(next) # redirect to put away list after process
[docs] def report_addons(request): # Display a list of all addon records waiting to be loaded # i.e an addition record but no addon : filtered by account # Filtered by system # get addon records report = Addons.objects.filter(load_status='2') report_details =report.select_related('wine') context = {'addons':report_details} return render(request, 'addons/report.html', context)