Browse Source

Still working to get the entry from to auto select the current date when the month selected is the current month. Also need to resolved submission error on the edit entry form.

Adam Day 4 years ago
parent
commit
6efbfb9935

+ 34 - 27
app/forms.py

@@ -3,7 +3,6 @@ from django.core.validators import RegexValidator, MaxLengthValidator, MinLength
 from . models import Project, Setting
 from calendar import monthrange
 
-
 numeric = RegexValidator(r'^[0-9+]', 'Only numeric characters.')
 time_max_length = MaxLengthValidator(4, 'Length limit exceeds 4 characters.')
 max_daily_hours_length = MaxLengthValidator(2, 'Only 2 place values allowed.')
@@ -35,6 +34,39 @@ except Exception as e:
     for hour in range(0, 9):
         hours.append(("%i" % hour, "%i" % hour))
 
+days = (
+    ('1', '1'),
+    ('2', '2'),
+    ('3', '3'),
+    ('4', '4'),
+    ('5', '5'),
+    ('6', '6'),
+    ('7', '7'),
+    ('8', '8'),
+    ('9', '9'),
+    ('10', '10'),
+    ('11', '11'),
+    ('12', '12'),
+    ('13', '13'),
+    ('14', '14'),
+    ('15', '15'),
+    ('16', '16'),
+    ('17', '17'),
+    ('18', '18'),
+    ('19', '19'),
+    ('20', '20'),
+    ('21', '21'),
+    ('22', '22'),
+    ('23', '23'),
+    ('24', '24'),
+    ('25', '25'),
+    ('26', '26'),
+    ('27', '27'),
+    ('28', '28'),
+    ('29', '29'),
+    ('30', '30'),
+    ('31', '31')
+)
 
 minutes = (
     ('0', '00'),
@@ -60,32 +92,7 @@ class CreateUserForm(forms.Form):
 
 
 class TimeEntryForm(forms.Form):
-    project = forms.ModelChoiceField(Project.objects.all(), required=False,
-                                     label="Select a project if applicable (not required)",
-                                     widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
-
-    hours = forms.ChoiceField(required=True, choices=hours,
-                              widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
-    minutes = forms.ChoiceField(required=True, choices=minutes,
-                                widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
-
-
-class PastTimeEntryForm(forms.Form):
-
-    def __init__(self, *args, **kwargs):
-        self.month = kwargs.pop('month')
-        self.year = kwargs.pop('year')
-        super(PastTimeEntryForm, self).__init__(*args, **kwargs)
-
-    def number_of_days(self):
-        days = []
-        r = monthrange(self.year, self.month)
-        print(r)
-        return days
-
-    number_of_days
-
-    day_of_month = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
+    day_of_month = forms.ChoiceField(choices=days, widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))
     project = forms.ModelChoiceField(Project.objects.all(), required=False,
                                      label="Select a project if applicable (not required)",
                                      widget=forms.Select(attrs={'class': 'form-control form-control-lg'}))

+ 1 - 1
app/templates/edit.html

@@ -7,7 +7,7 @@
             <h2>{{ user.first_name | title }} {{ user.last_name | title }}</h2>
         </div>
         <div class="col-sm-12 col-md-12 col-lg-6 text-sm-center text-lg-end">
-            <a href="#!" class="btn btn-lg btn-secondary"><span class="oi" data-glyph="arrow-thick-left"></span> Back</a>
+            <a href="{% url 'timesheet' year=entry.date.year month=entry.date.month %}" class="btn btn-lg btn-secondary"><span class="oi" data-glyph="arrow-thick-left"></span> Back</a>
         </div>
     </div>
 

+ 23 - 4
app/templates/forms/timesheet_entry.html

@@ -1,14 +1,33 @@
 {% load crispy_forms_tags %}
-
+{{ form.errors }}
 <form action="{% url 'timesheet' year=current_month.year month=current_month.month %}" method="post">
     <div class="row">
-        <div class="col-sm-12 col-md-4">
+        <div class="col-sm-12 col-md-3">
+
+            <label class="form-label" for="day_of_month">Day of month*</label>
+            <select id="day_of_month" name="day_of_month" class="form-control form-control-lg">
+                <option value="-">---</option>
+                {% for day in days_of_month %}
+                    {% if selected_day == day.day %}
+                        <option value="{{ day.day }}" selected>{{ day.label }}</option>
+                    {% else %}
+                        <option value="{{ day.day }}">{{ day.label }}</option>
+                    {% endif %}
+                {% endfor %}
+            </select>
+            {% if form.day_of_month.errors %}
+                {% for error in form.day_of_month.errors %}
+                    <div class="text-danger"><b>{{ error }}</b></div>
+                {% endfor %}
+            {% endif %}
+        </div>
+        <div class="col-sm-12 col-md-3">
             {{ form.hours | as_crispy_field }}
         </div>
-        <div class="col-sm-12 col-md-4">
+        <div class="col-sm-12 col-md-3">
             {{ form.minutes | as_crispy_field }}
         </div>
-        <div class="col-sm-12 col-md-4">
+        <div class="col-sm-12 col-md-3">
             <label class="form-label">&nbsp;</label>
             <input type="submit" value="Submit" class="form-control form-control-lg btn btn-lg btn-dark">
         </div>

+ 0 - 20
app/templates/forms/timesheet_entry_past.html

@@ -1,20 +0,0 @@
-{% load crispy_forms_tags %}
-
-<form action="{% url 'timesheet' year=current_month.year month=current_month.month %}" method="post">
-    <div class="row">
-        <div class="col-sm-12 col-md-3">
-            {{ form.day_of_month | as_crispy_field }}
-        </div>
-        <div class="col-sm-12 col-md-3">
-            {{ form.hours | as_crispy_field }}
-        </div>
-        <div class="col-sm-12 col-md-3">
-            {{ form.minutes | as_crispy_field }}
-        </div>
-        <div class="col-sm-12 col-md-3">
-            <label class="form-label">&nbsp;</label>
-            <input type="submit" value="Submit" class="form-control form-control-lg btn btn-lg btn-dark">
-        </div>
-        {% csrf_token %}
-    </div>
-</form>

+ 20 - 3
app/templates/forms/timesheet_entry_projects.html

@@ -1,11 +1,28 @@
 {% load crispy_forms_tags %}
-
 <form action="{% url 'timesheet' year=current_month.year month=current_month.month %}" method="post">
     <div class="row">
-        <div class="col-sm-12 col-md-6">
+        <div class="col-sm-12 col-md-4">
+            <label class="form-label" for="day_of_month">Day of month*</label>
+            <select id="day_of_month" name="day_of_month" class="form-control form-control-lg {% if form.day_of_month.errors %}is-invalid{% endif %}">
+                <option value="Blank">---</option>
+                {% for day in days_of_month %}
+                    {% if selected_day == day.day %}
+                        <option value="{{ day.day }}" selected>{{ day.label }}</option>
+                    {% else %}
+                        <option value="{{ day.day }}">{{ day.label }}</option>
+                    {% endif %}
+                {% endfor %}
+            </select>
+            {% if form.day_of_month.errors %}
+                {% for error in form.day_of_month.errors %}
+                    <p class="invalid-feedback"><strong>{{ error }}</strong></p>
+                {% endfor %}
+            {% endif %}
+        </div>
+        <div class="col-sm-12 col-md-4">
             {{ form.hours | as_crispy_field }}
         </div>
-        <div class="col-sm-12 col-md-6">
+        <div class="col-sm-12 col-md-4">
             {{ form.minutes | as_crispy_field }}
         </div>
         <div class="col-sm-12 col-md-6">

+ 0 - 25
app/templates/forms/timesheet_entry_projects_past.html

@@ -1,25 +0,0 @@
-{% load crispy_forms_tags %}
-
-<form action="{% url 'timesheet' year=current_month.year month=current_month.month %}" method="post">
-    <div class="row">
-        <div class="col-sm-12 col-md-4">
-            {{ form.day_of_month | as_crispy_field }}
-        </div>
-        <div class="col-sm-12 col-md-4">
-            {{ form.hours | as_crispy_field }}
-        </div>
-        <div class="col-sm-12 col-md-4">
-            {{ form.minutes | as_crispy_field }}
-        </div>
-        <div class="col-sm-12 col-md-6">
-            {{ form.project | as_crispy_field }}
-        </div>
-        <div class="col-sm-12 col-md-6">
-            <label class="form-label">&nbsp;</label>
-            <input type="submit" value="Submit" class="form-control form-control-lg btn btn-lg btn-dark">
-        </div>
-        {% csrf_token %}
-    </div>
-</form>
-
-

+ 20 - 36
app/templates/timesheet.html

@@ -16,13 +16,13 @@
     </div>
 
     <div class="row">
-        <div class="col-4">
+        <div class="col-2">
             <a class="btn btn-lg btn-light border" href="{% url 'timesheet' year=previous_month.year month=previous_month.month %}"><span class="oi" data-glyph="chevron-left"></span></a>
         </div>
-        <div class="col-4 text-center">
+        <div class="col-8 text-center">
             <h1>{{ current_month_name }} {{ current_month.year }}</h1>
         </div>
-        <div class="col-4 text-end">
+        <div class="col-2 text-end">
             {% if show_next %}
                 <a class="btn btn-lg btn-light border" href="{% url 'timesheet' year=next_month.year month=next_month.month %}"><span class="oi" data-glyph="chevron-right"></span></a>
             {% else %}
@@ -31,46 +31,30 @@
         </div>
     </div>
     {% if show_form %}
-        {% if today_is_today %}
-            {% if max_daily_entries_quota == False %}
-            <div class="row mb-3">
-                <div class="col-12">
-                    <div class="card">
-                        <div class="card-body">
-                            {% if projects == "True" %}
-                                {% include 'forms/timesheet_entry_projects.html' %}
-                            {% else %}
-                                {% include 'forms/timesheet_entry.html' %}
-                            {% endif %}
-                        </div>
-                    </div>
-                </div>
-            </div>
-            {% else %}
-            <div class="row mb-3">
-                <div class="col-12">
-                    <div class="card">
-                        <div class="card-body text-center">
-                            <p class="lead pt-1"><span class="oi" data-glyph="warning"></span> Maximum daily entries met.</p>
-                        </div>
+        {% if max_daily_entries_quota == False %}
+        <div class="row mb-3">
+            <div class="col-12">
+                <div class="card">
+                    <div class="card-body">
+                        {% if projects == "True" %}
+                            {% include 'forms/timesheet_entry_projects.html' %}
+                        {% else %}
+                            {% include 'forms/timesheet_entry.html' %}
+                        {% endif %}
                     </div>
                 </div>
             </div>
-            {% endif %}
+        </div>
         {% else %}
-            <div class="row mb-3">
-                <div class="col-12">
-                    <div class="card">
-                        <div class="card-body">
-                            {% if projects == "True" %}
-                                {% include 'forms/timesheet_entry_projects_past.html' %}
-                            {% else %}
-                                {% include 'forms/timesheet_entry_past.html' %}
-                            {% endif %}
-                        </div>
+        <div class="row mb-3">
+            <div class="col-12">
+                <div class="card">
+                    <div class="card-body text-center">
+                        <p class="lead pt-1"><span class="oi" data-glyph="warning"></span> Maximum daily entries met.</p>
                     </div>
                 </div>
             </div>
+        </div>
         {% endif %}
     {% endif %}
 

+ 28 - 24
app/views.py

@@ -1,10 +1,10 @@
 from django.shortcuts import render, redirect
-from . forms import LoginForm, CreateUserForm, TimeEntryForm, SettingsForm, PastTimeEntryForm
+from . forms import LoginForm, CreateUserForm, TimeEntryForm, SettingsForm
 from . models import User, Setting, Entry
 from hashlib import sha256
 import datetime
-from django.template.defaultfilters import date
 from dateutil.relativedelta import relativedelta
+from calendar import monthrange
 
 
 def hash_pin(pin):
@@ -24,6 +24,15 @@ def check_setup():
         return False
 
 
+def days_of_month(year, month):
+    r = monthrange(year, month)
+    days = []
+    for day in range(1, r[1]+1):
+        d = datetime.date(year, month, day)
+        days.append({'day': str(day), 'label': d.strftime('%A, %b. %d')})
+    return days
+
+
 def logout_user(request):
     request.session['authenticated'] = False
     return redirect('home')
@@ -163,6 +172,7 @@ def timesheet(request, year=None, month=None, day=None):
     current_month = datetime.date(year, month, 1)
     next_month = current_month + relativedelta(months=+1)
     previous_month = current_month + relativedelta(months=-1)
+    current_month = datetime.date(year, month, day)
 
     if requires_auth(request) is False:
         request.session['authenticated'] = False
@@ -173,44 +183,35 @@ def timesheet(request, year=None, month=None, day=None):
 
     projects = Setting.objects.get(setting='Projects')
 
-    if today_is_today:
-        form = TimeEntryForm()
-    else:
-        form = PastTimeEntryForm(month=current_month.month, year=current_month.year)
+    form = TimeEntryForm()
+    selected_day = None
 
     if request.method == "POST":
-        # form = TimeEntryForm(request.POST)
-
-        if today_is_today:
-            form = TimeEntryForm(request.POST)
-        else:
-            form = PastTimeEntryForm(request.POST, month=current_month.month, year=current_month.year)
+        form = TimeEntryForm(request.POST)
 
         if form.is_valid():
             data = form.cleaned_data
             if data['hours'] == '0' and data['minutes'] == '0':
-                form.add_error('hours', 'May not be 0 if minutes is 0')
-                form.add_error('minutes', 'May not be 0 if hours is 0')
+                selected_day = data['day_of_month']
+                form.add_error('hours', 'No time worked provided')
+                form.add_error('minutes', 'No time worked provided')
             else:
                 entry = Entry()
                 entry.user = user
                 entry.project = data['project']
-                entry.date = datetime.date(year=current_month.year, month=current_month.month, day=current_month.day)
+                entry.date = datetime.date(year=current_month.year, month=current_month.month,
+                                           day=int(data['day_of_month']))
                 entry.hours = data['hours']
                 entry.minutes = data['minutes']
                 entry.save()
+                form = TimeEntryForm()
 
-                # form = TimeEntryForm()
-
-                if today_is_today:
-                    form = TimeEntryForm()
-                else:
-                    form = PastTimeEntryForm(month=current_month.month, year=current_month.year)
-
-    entries = Entry.objects.filter(user=user, date__year=current_month.year, date__month=current_month.month)
+    entries = Entry.objects.filter(user=user, date__year=current_month.year, date__month=current_month.month)\
+        .order_by('date')
 
     max_daily_entries = Setting.objects.get(setting='Max Daily Entries')
-    todays_entries = Entry.objects.filter(user=user, date__year=current_month.year, date__month=current_month.month).count()
+    todays_entries = Entry.objects.filter(user=user, date__year=current_month.year, date__month=current_month.month)\
+        .count()
 
     max_daily_entries_quota = False
 
@@ -246,9 +247,12 @@ def timesheet(request, year=None, month=None, day=None):
         'projects': projects.value,
         'session_timeout': auth_timeout,
         'current_month': current_month,
+        'current_month_day': str(current_month.day),
         'current_month_name': current_month.strftime('%B'),
         'next_month': next_month,
         'previous_month': previous_month,
+        'days_of_month': days_of_month(current_month.year, current_month.month),
+        'selected_day': selected_day,
     }
 
     return render(request, 'timesheet.html', context=context)