cqtu/contest/views.py

189 lines
5.7 KiB
Python
Raw Normal View History

2017-01-25 01:53:22 +01:00
from django.shortcuts import render, get_object_or_404
2017-01-18 02:25:12 +01:00
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
2017-01-25 01:53:22 +01:00
#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
2017-01-25 01:53:22 +01:00
from .models import User, Contest, Frequency, Reference, QSO, ShadowCall
from .forms import UpdateRefForm, QSOForm, QSOFormWithTime, CustomUserCreationForm, ShadowCallAddForm
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-23 01:09:04 +01:00
qrgs = Frequency.objects.filter(band__contest=contest).order_by("channel")
2017-01-19 18:42:11 +01:00
2017-01-23 01:09:04 +01:00
return render(request, 'contest/index.html', {"qsoform": qsoform, "contest": contest, "qrgs": qrgs})
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()
2017-01-25 01:53:22 +01:00
shadows = ShadowCall.objects.all()
2017-01-19 18:42:11 +01:00
qsos = QSO.objects.all().order_by("-time")
2017-01-25 01:53:22 +01:00
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})
2017-01-19 18:42:11 +01:00
@staff_member_required
2017-01-25 01:53:22 +01:00
def updateRef(request, shadow, uid):
user = None
2017-01-19 18:42:11 +01:00
form = None
2017-01-25 01:53:22 +01:00
if shadow:
user = get_object_or_404(ShadowCall, id=uid)
else:
user = get_object_or_404(User, id=uid)
2017-01-19 18:42:11 +01:00
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()
2017-01-25 01:53:22 +01:00
messages.success(request, "%s%s ref set to %s" % ("(shadow) " if shadow else "", user, ref))
2017-01-19 18:42:11 +01:00
return HttpResponseRedirect(reverse("contest:registerRefs"))
else:
form = UpdateRefForm()
2017-01-25 01:53:22 +01:00
return render(request, 'contest/updateRef.html', {'userobj': user, 'form': form, "shadow": shadow})
2017-01-19 18:42:11 +01:00
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})