cqtu/contest/views.py

175 lines
5.3 KiB
Python
Raw Normal View History

2017-01-18 02:25:12 +01:00
from django.shortcuts import render
2017-01-19 18:42:11 +01:00
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from django.db.models import Q
2017-01-22 22:54:57 +01:00
from django.contrib.auth.forms import AuthenticationForm, PasswordChangeForm
2017-01-19 18:42:11 +01:00
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.urls import reverse
2017-01-21 03:16:27 +01:00
from django.contrib.auth import login as auth_login
2017-01-18 02:25:12 +01:00
2017-01-19 18:42:11 +01:00
import datetime
from .models import User, Contest, Frequency, Reference, QSO
2017-01-21 03:16:27 +01:00
from .forms import UpdateRefForm, QSOForm, QSOFormWithTime, CustomUserCreationForm
2017-01-18 02:25:12 +01:00
def index(request):
2017-01-19 18:42:11 +01:00
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)
2017-01-20 14:28:17 +01:00
contest = Contest.objects.get(id=1)
2017-01-19 18:42:11 +01:00
2017-01-20 14:28:17 +01:00
return render(request, 'contest/index.html', {"qsoform": qsoform, "contest": contest})
2017-01-19 18:42:11 +01:00
@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)
2017-01-23 00:00:57 +01:00
form.helper.form_tag = False
2017-01-19 18:42:11 +01:00
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",
}
2017-01-20 23:36:55 +01:00
if qsos.count() > 0:
data["band"] = qsos[0].band
2017-01-19 18:42:11 +01:00
form = QSOForm(request.user, initial=data)
2017-01-23 00:00:57 +01:00
form.helper.form_tag = False
2017-01-19 18:42:11 +01:00
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':
2017-01-21 00:16:39 +01:00
form = QSOFormWithTime(user=request.user, instance=qso, data=request.POST)
2017-01-19 18:42:11 +01:00
if form.is_valid():
form.instance.save()
messages.info(request, "QSO has been edited")
return HttpResponseRedirect(reverse("contest:log"))
else:
2017-01-21 00:16:39 +01:00
form = QSOFormWithTime(user=request.user, instance=qso)
2017-01-19 18:42:11 +01:00
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:
2017-01-21 01:08:40 +01:00
if request.POST["delete"].lower() == "yes":
2017-01-19 18:42:11 +01:00
qso.delete()
messages.info(request, "QSO has been deleted")
return HttpResponseRedirect(reverse("contest:log"))
2017-01-21 01:08:40 +01:00
elif request.POST["delete"].lower() == "no":
2017-01-19 18:42:11 +01:00
return HttpResponseRedirect(reverse("contest:log"))
return render(request, 'contest/logDelete.html', {"qso": qso})
2017-01-18 02:25:12 +01:00
@staff_member_required
def registerRefs(request):
2017-01-19 18:42:11 +01:00
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})
2017-01-18 02:25:12 +01:00
2017-01-19 18:42:11 +01:00
def register(request):
2017-01-21 03:16:27 +01:00
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})
2017-01-22 22:54:57 +01:00
@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})