from django.shortcuts import render, get_object_or_404 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, PasswordChangeForm from django.http import HttpResponseRedirect from django.contrib import messages from django.urls import reverse from django.contrib.auth import login as auth_login import datetime from .models import User, Contest, Frequency, Reference, QSO, ShadowCall from .forms import UpdateRefForm, QSOForm, QSOFormWithTime, CustomUserCreationForm, ShadowCallAddForm 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) qrgs = Frequency.objects.filter(band__contest=contest).order_by("channel") return render(request, 'contest/index.html', {"qsoform": qsoform, "contest": contest, "qrgs": qrgs}) @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) form.helper.form_tag = False 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) form.helper.form_tag = False 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"].lower() == "yes": qso.delete() messages.info(request, "QSO has been deleted") return HttpResponseRedirect(reverse("contest:log")) elif request.POST["delete"].lower() == "no": return HttpResponseRedirect(reverse("contest:log")) return render(request, 'contest/logDelete.html', {"qso": qso}) @staff_member_required def registerRefs(request): allUser = User.objects.all() shadows = ShadowCall.objects.all() qsos = QSO.objects.all().order_by("-time") shadowForm = None if request.method == 'POST': shadowForm = ShadowCallAddForm(data=request.POST) if shadowForm.is_valid(): shadowForm.instance.save() messages.success(request, "Successfully added shadow user %s" % (shadowForm.instance.username,)) return HttpResponseRedirect(reverse("contest:registerRefs")) else: shadowForm = ShadowCallAddForm() return render(request, 'contest/registerRefs.html', {'alluser': allUser, "qsos": qsos, "shadowForm": shadowForm, "shadows": shadows}) @staff_member_required def updateRef(request, shadow, uid): user = None form = None if shadow: user = get_object_or_404(ShadowCall, id=uid) else: user = get_object_or_404(User, id=uid) if request.method == 'POST': form = UpdateRefForm(data=request.POST) if form.is_valid(): ref = None if form.cleaned_data["existingRef"]: 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%s ref set to %s" % ("(shadow) " if shadow else "", user, ref)) return HttpResponseRedirect(reverse("contest:registerRefs")) else: form = UpdateRefForm() return render(request, 'contest/updateRef.html', {'userobj': user, 'form': form, "shadow": shadow}) 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): form = None if request.method == 'POST': form = CustomUserCreationForm(data=request.POST) if form.is_valid(): form.instance.save() auth_login(request, form.instance) messages.info(request, "Registration as user %s successfull!" % form.instance.username) return HttpResponseRedirect(reverse("contest:index")) else: form = CustomUserCreationForm() return render(request, 'registration/register.html', {"form": form}) @login_required def profile(request): pwForm = None if request.method == 'POST': pwForm = PasswordChangeForm(user=request.user, data=request.POST) if pwForm.is_valid(): pwForm.save() auth_login(request, pwForm.user) messages.success(request, "Password changed") else: pwForm = PasswordChangeForm(user=request.user) return render(request, 'registration/profile.html', {"pwForm": pwForm})