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
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):
super(QSOForm, self).__init__(*args, **kwargs)
self.user = user
@ -105,3 +93,9 @@ class QSOForm(forms.ModelForm):
def clean_refStr(self):
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)
band = models.ForeignKey(Band)
reportTX = models.CharField(max_length=7, default=59)
reportRX = 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, verbose_name='RST-R')
ownNo = models.IntegerField()
otherNo = models.IntegerField()
ownNo = models.IntegerField(verbose_name='Nr-S')
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)
remarks = models.CharField(max_length=50, blank=True)

View File

@ -11,7 +11,7 @@ from django.urls import reverse
import datetime
from .models import User, Contest, Frequency, Reference, QSO
from .forms import UpdateRefForm, QSOForm
from .forms import UpdateRefForm, QSOForm, QSOFormWithTime
def index(request):
if request.user.is_authenticated():
@ -72,14 +72,14 @@ def logEdit(request, qsoid):
form = None
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():
form.instance.save()
messages.info(request, "QSO has been edited")
return HttpResponseRedirect(reverse("contest:log"))
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})

View File

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

View File

@ -1,20 +1,28 @@
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="row">
<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 %}">
{% csrf_token %}
<table>
<tr>
<th>User</th><td>{{ user }}</td>
</tr>
{{ form.as_table }}
</table>
<input type="submit" value="Save">
</form>
<form method="post" action="{% url "contest:updateRef" user.id %}">
{% csrf_token %}
<div class="form-group">
<label class="control-label" for="id_div_user">User</label>
<div id="id_div_user" class="controls readonly">{{ user }}</div>
</div>
{{ form|crispy }}
<input type="submit" value="Save">
</form>
</div>
</div>
</div>
</div>