from django.shortcuts import render from django.contrib.auth.decorators import login_required from django.contrib.admin.views.decorators import staff_member_required from django.db.models import Q from django.contrib.auth.forms import AuthenticationForm from django.http import HttpResponseRedirect from django.contrib import messages from django.urls import reverse import datetime from .models import User, Contest, Frequency, Reference, QSO from .forms import UpdateRefForm, QSOForm, QSOFormWithTime def index(request): if request.user.is_authenticated(): return HttpResponseRedirect(reverse("contest:index")) return render(request, "index.html", {"loginForm": AuthenticationForm()}) @login_required def contestIndex(request): #messages.debug(request, "Debug GLITCHHHHH") #messages.info(request, "This info is very educational") #messages.warning(request, "You got a warning") #messages.error(request, "Error!!!") #messages.success(request, "Great Success") qsoform = QSOForm(request.user) contest = Contest.objects.get(id=1) return render(request, 'contest/index.html', {"qsoform": qsoform, "contest": contest}) @login_required def log(request): form = None qsos = QSO.objects.filter(owner=request.user).order_by("-ownNo") if request.method == 'POST': form = QSOForm(user=request.user, data=request.POST) if form.is_valid(): l = form.instance if not l.time: # set current time l.time = datetime.datetime.now() l.owner = request.user l.save() messages.success(request, "QSO saved!") return HttpResponseRedirect(reverse("contest:log")) else: # FIXME: data initial my qso number data = { "ownNo": qsos[0].ownNo + 1 if len(qsos) > 0 else 1, "reportRX": "59", "reportTX": "59", } if qsos.count() > 0: data["band"] = qsos[0].band form = QSOForm(request.user, initial=data) return render(request, 'contest/log.html', {'form': form, 'qsos': qsos}) @login_required def logEdit(request, qsoid): qso = QSO.objects.get(id=qsoid, owner=request.user) form = None if request.method == 'POST': form = QSOFormWithTime(user=request.user, instance=qso, data=request.POST) if form.is_valid(): form.instance.save() messages.info(request, "QSO has been edited") return HttpResponseRedirect(reverse("contest:log")) else: form = QSOFormWithTime(user=request.user, instance=qso) return render(request, 'contest/logEdit.html', {'form': form, "qso": qso}) def logDelete(request, qsoid): qso = QSO.objects.get(id=qsoid, owner=request.user) if request.method == 'POST': if "delete" in request.POST: if request.POST["delete"] == "yes": qso.delete() messages.info(request, "QSO has been deleted") return HttpResponseRedirect(reverse("contest:log")) elif request.POST["delete"] == "no": return HttpResponseRedirect(reverse("contest:log")) return render(request, 'contest/logDelete.html', {"qso": qso}) @staff_member_required def registerRefs(request): allUser = User.objects.all() refsMissingUser = User.objects.filter(ref=None).order_by("username") refsNotMissingUser = User.objects.filter(~Q(ref=None)).order_by("username") qsos = QSO.objects.all().order_by("-time") return render(request, 'contest/registerRefs.html', {'alluser': allUser, 'refsMissingUser': refsMissingUser, "refsNotMissinguser": refsNotMissingUser, "qsos": qsos}) @staff_member_required def updateRef(request, uid): user = User.objects.get(id=uid) form = None if request.method == 'POST': form = UpdateRefForm(data=request.POST) if form.is_valid(): ref = None if form.cleaned_data["existingRef"]: print("Got an existing Ref") ref = form.cleaned_data["existingRef"] else: ref = Reference(name=form.cleaned_data["newRefName"]) ref.save() messages.info(request, "New Ref '%s' created" % ref) user.ref = ref user.save() messages.success(request, "%s ref set to %s" % (user, ref)) return HttpResponseRedirect(reverse("contest:registerRefs")) else: form = UpdateRefForm() return render(request, 'contest/updateRef.html', {'user': user, 'form': form}) def overview(request): # FIXME: Hardcoded for cqtu... everywhere c = Contest.objects.get(id=1) qrgs = Frequency.objects.filter(band__contest=c).order_by("channel") return render(request, 'contest/overview.html', {'contest': c, 'qrgs': qrgs}) def register(request): return render(request, 'registration/register.html', {})