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 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.utils import timezone import datetime from .models import User, Contest, Frequency, Reference, QSO, ShadowCall from .forms import UpdateRefForm, QSOForm, QSOFormWithTime, CustomUserCreationForm, ShadowCallAddForm, UpdateCategoryForm 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): 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): if not request.user.ref: return HttpResponseRedirect(reverse("contest:index")) 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: 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): if not request.user.ref: return HttpResponseRedirect(reverse("contest:index")) 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): if not request.user.ref: return HttpResponseRedirect(reverse("contest:index")) 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")[0:10] 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}) def getPage(paginator, pageNo): try: return paginator.page(pageNo) except PageNotAnInteger: return paginator.page(1) except EmptyPage: return paginator.page(paginator.num_pages) @staff_member_required def recheckAllQSOs(request): if request.method == "POST": cnt = 0 for qso in QSO.objects.all(): if qso.checkQSOData(): print(qso) qso.save() cnt += 1 messages.success(request, "Allo QSO have been checked against each other (%d QSOs modified)" % (cnt,)) return HttpResponseRedirect(reverse("contest:registerRefs")) return render(request, "contest/checkAllQSOs.html", {}) @staff_member_required def viewUserQSOs(request, uid, page=1): user = get_object_or_404(User, id=uid) qsos = QSO.objects.filter(owner=user).order_by("-time") qsoPager = Paginator(qsos, 50) qsoPage = getPage(qsoPager, request.GET.get('page')) userRefs = set(map(lambda _x: _x["refStr"], user.qso_set.filter(ref__isnull=False).values("ref", "refStr"))) return render(request, "contest/viewUserQSOs.html", {'owner': user, 'qsos': qsos, 'qsoPager': qsoPager, 'qsoPage': qsoPage, 'userRefs': userRefs}) @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.regTime = form.cleaned_data["regTime"] user.location = form.cleaned_data["location"] user.opName = form.cleaned_data["opName"] user.save() messages.success(request, "%s%s ref set to %s" % ("(shadow) " if shadow else "", user, ref)) return HttpResponseRedirect(reverse("contest:registerRefs")) else: data = {} if user.ref: data["existingRef"] = user.ref if user.regTime: data["regTime"] = user.regTime else: data["regTime"] = timezone.now() data["opName"] = user.opName data["location"] = user.location form = UpdateRefForm(initial=data) return render(request, 'contest/updateRef.html', {'userobj': user, 'form': form, "shadow": shadow}) @staff_member_required def viewAllQSOs(request, page=1): qsos = QSO.objects.all().order_by("-time") qsoPager = Paginator(qsos, 10) qsoPage = getPage(qsoPager, request.GET.get('page')) return render(request, 'contest/viewAllQSOs.html', {'qsoPager': qsoPager, 'qsoPage': qsoPage}) 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 catForm = None if request.method == 'POST': if request.POST.get("submit", None) == "pwchange": pwForm = PasswordChangeForm(user=request.user, data=request.POST) if pwForm.is_valid(): pwForm.save() auth_login(request, pwForm.user) messages.success(request, "Password changed") return HttpResponseRedirect(reverse("profile")) else: catForm = UpdateCategoryForm(data=request.POST) if catForm.is_valid(): request.user.cat = catForm.cleaned_data["entry"] request.user.save() messages.success(request, "Entry category set") return HttpResponseRedirect(reverse("profile")) if not pwForm: pwForm = PasswordChangeForm(user=request.user) if not catForm: catForm = UpdateCategoryForm(initial={'entry': request.user.cat}) return render(request, 'registration/profile.html', {"pwForm": pwForm, "catForm": catForm})