Better forms!

This commit is contained in:
Sebastian Lohff 2017-01-21 00:16:39 +01:00
parent 86477e1ea8
commit 20f4c203f0
5 changed files with 45 additions and 43 deletions

View File

@ -42,18 +42,6 @@ class QSOForm(forms.ModelForm):
model = QSO model = QSO
fields = ["ownNo", "band", "call", "reportTX", "reportRX", "otherNo", "refStr", "remarks"] fields = ["ownNo", "band", "call", "reportTX", "reportRX", "otherNo", "refStr", "remarks"]
widgets = {
"time": forms.DateTimeInput(attrs={"placeholder": "Current time"})
}
labels = {
'ownNo': 'Nr-S',
'reportTX': 'RST-S',
'reportRX': 'RST-R',
'otherNo': 'Nr-R',
'refStr': 'EXC',
}
def __init__(self, user, *args, **kwargs): def __init__(self, user, *args, **kwargs):
super(QSOForm, self).__init__(*args, **kwargs) super(QSOForm, self).__init__(*args, **kwargs)
self.user = user self.user = user
@ -105,3 +93,9 @@ class QSOForm(forms.ModelForm):
def clean_refStr(self): def clean_refStr(self):
return self.cleaned_data["refStr"].upper() return self.cleaned_data["refStr"].upper()
class QSOFormWithTime(QSOForm):
class Meta:
model = QSO
fields = ["time", "ownNo", "band", "call", "reportTX", "reportRX", "otherNo", "refStr", "remarks"]

View File

@ -52,13 +52,13 @@ class QSO(models.Model):
call = models.CharField(max_length=20) call = models.CharField(max_length=20)
band = models.ForeignKey(Band) band = models.ForeignKey(Band)
reportTX = models.CharField(max_length=7, default=59) reportTX = models.CharField(max_length=7, default=59, verbose_name='RST-S')
reportRX = models.CharField(max_length=7, default=59) reportRX = models.CharField(max_length=7, default=59, verbose_name='RST-R')
ownNo = models.IntegerField() ownNo = models.IntegerField(verbose_name='Nr-S')
otherNo = models.IntegerField() otherNo = models.IntegerField(verbose_name='Nr-R')
refStr = models.CharField(max_length=20) refStr = models.CharField(max_length=20, verbose_name="Exc")
ref = models.ForeignKey(Reference, null=True, blank=True) ref = models.ForeignKey(Reference, null=True, blank=True)
remarks = models.CharField(max_length=50, blank=True) remarks = models.CharField(max_length=50, blank=True)

View File

@ -11,7 +11,7 @@ from django.urls import reverse
import datetime import datetime
from .models import User, Contest, Frequency, Reference, QSO from .models import User, Contest, Frequency, Reference, QSO
from .forms import UpdateRefForm, QSOForm from .forms import UpdateRefForm, QSOForm, QSOFormWithTime
def index(request): def index(request):
if request.user.is_authenticated(): if request.user.is_authenticated():
@ -72,14 +72,14 @@ def logEdit(request, qsoid):
form = None form = None
if request.method == 'POST': if request.method == 'POST':
form = QSOForm(user=request.user, instance=qso, data=request.POST) form = QSOFormWithTime(user=request.user, instance=qso, data=request.POST)
if form.is_valid(): if form.is_valid():
form.instance.save() form.instance.save()
messages.info(request, "QSO has been edited") messages.info(request, "QSO has been edited")
return HttpResponseRedirect(reverse("contest:log")) return HttpResponseRedirect(reverse("contest:log"))
else: else:
form = QSOForm(user=request.user, instance=qso) form = QSOFormWithTime(user=request.user, instance=qso)
return render(request, 'contest/logEdit.html', {'form': form, "qso": qso}) return render(request, 'contest/logEdit.html', {'form': form, "qso": qso})

View File

@ -1,20 +1,20 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %} {% block content %}
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
We have the User {{ user }}, create a ref for them or choose an existing ref! <div class="panel panel-default">
<div class="panel-heading">Edit QSO</div>
<form method="post" action="{% url "contest:logEdit" qso.id %}"> <div class="panel-body">
{% csrf_token %} <form method="post" action="{% url "contest:logEdit" qso.id %}">
<table> {% csrf_token %}
<tr> {{ form|crispy }}
<th>User</th><td>{{ user }}</td> <input type="submit" value="Save">
</tr> </form>
{{ form.as_table }} </div>
</table> </div>
<input type="submit" value="Save">
</form>
</div> </div>
</div> </div>
{% endblock %} {% endblock %}

View File

@ -1,20 +1,28 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %} {% block content %}
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
We have the User {{ user }}, create a ref for them or choose an existing ref! <div class="panel panel-default">
<div class="panel-heading">Edit Exchange for User</div>
<div class="panel-body">
<p>
Create a ref for user {{ user }} or choose an existing ref!
</p>
<form method="post" action="{% url "contest:updateRef" user.id %}"> <form method="post" action="{% url "contest:updateRef" user.id %}">
{% csrf_token %} {% csrf_token %}
<table> <div class="form-group">
<tr> <label class="control-label" for="id_div_user">User</label>
<th>User</th><td>{{ user }}</td> <div id="id_div_user" class="controls readonly">{{ user }}</div>
</tr> </div>
{{ form.as_table }} {{ form|crispy }}
</table> <input type="submit" value="Save">
<input type="submit" value="Save"> </form>
</form> </div>
</div>
</div> </div>
</div> </div>