Commits

Caleb Smith  committed f8e5fd9

fixed #58 Active entries have no end time field. Tests adjusted. Testing
editing an active entry split into two tests, one for new values, one for
maintaining the same values. Manual model clean call removed.

  • Participants
  • Parent commits 0b405c1

Comments (0)

Files changed (2)

File timepiece/forms.py

 
     def __init__(self, *args, **kwargs):
         self.user = kwargs.pop('user')
-        if not kwargs.get('instance', None):
-            kwargs['instance'] = timepiece.Entry(user=self.user)
         super(AddUpdateEntryForm, self).__init__(*args, **kwargs)
         self.fields['project'].queryset = timepiece.Project.objects.filter(
             users=self.user,
         )
-
+        #if editing a current entry, remove the end time field
+        if self.instance.start_time and not self.instance.end_time:
+            self.fields.pop('end_time')
+        #Use default activity if possible
         try:
             profile = self.user.profile
         except timepiece.UserProfile.DoesNotExist:
         else:
             if profile.default_activity:
                 self.fields['activity'].initial = profile.default_activity
+        self.instance.user = self.user
 
     def clean(self):
         """
         cleaned_data = self.cleaned_data
         start = cleaned_data.get('start_time', None)
         end = cleaned_data.get('end_time', None)
-        if not start or not end:
+        if not start:
             raise forms.ValidationError(
-                'Please enter a valid start and end date/time.')
-        if start >= datetime.now() or end > datetime.now():
+                'Please enter a valid date/time.')
+        if start >= datetime.now() or end and end > datetime.now():
             raise forms.ValidationError(
                 'Entries may not be added in the future.')
         #Obtain all current entries, except the one being edited
+        times = [start, end] if end else [start]
+        query = reduce(lambda q, time: q | Q(start_time__lte=time), times, Q())
         entries = self.user.timepiece_entries.filter(
-            Q(start_time__lte=end, end_time__isnull=True) | \
-            Q(start_time__lte=start, end_time__isnull=True)
+            query, end_time__isnull=True
             ).exclude(id=self.instance.id)
         for entry in entries:
             output = 'The times below conflict with the current entry: ' + \
     def save(self, commit=True):
         entry = super(AddUpdateEntryForm, self).save(commit=False)
         entry.user = self.user
-        self.instance.clean()
         if commit:
             entry.save()
         return entry

File timepiece/tests/timesheet.py

             'The entry has been updated successfully', count=1)
         self.assertEquals(len(response.context['this_weeks_entries']), 1)
 
-    def testEditCurrent(self):
+    def testEditCurrentSameTime(self):
         """
-        Test the ability to edit a current entry, using valid values
+        Test the ability to edit a current entry, not changing the values
         """
         data = self.default_data
         data.update({
                 '%m/%d/%Y'),
             'start_time_1': self.current_entry_data['start_time'].strftime(
                 '%H:%M:%S'),
-            'end_time_0': (self.current_entry_data['start_time'] +
-                datetime.timedelta(minutes=1)).strftime('%m/%d/%Y'),
-            'end_time_1': (self.current_entry_data['start_time'] +
-                datetime.timedelta(minutes=1)).strftime('%H:%M:%S'),
         })
         response = self.client.post(self.edit_current_url, data, follow=True)
         #This post should redirect to the dashboard, with the correct message
-        #and 1 entry for this week, because we updated the entry in setUp
-
-        #(Make sure test fails the right way)
-#        self.assertFormError(response, 'form', None,
-#            'The times below conflict with the current entry: ' +
-#            '%(project)s - %(activity)s starting at %(start_time_str)s' %
-#            self.current_entry_data)
-
-        #These should pass once fixed:
+        #and 1 active entry, because we updated the current entry from setUp
         self.assertRedirects(response, reverse('timepiece-entries'),
             status_code=302, target_status_code=200)
         self.assertContains(response,
             'The entry has been updated successfully', count=1)
-        #There is one closed entry already, now there should be 2
-        self.assertEquals(len(response.context['this_weeks_entries']), 2)
+        self.assertEquals(len(response.context['my_active_entries']), 1)
+
+    def testEditCurrentDiffTime(self):
+        """
+        Test the ability to edit a current entry, using valid new values
+        """
+        data = self.default_data
+        new_start = self.current_entry_data['start_time'] + \
+            datetime.timedelta(minutes=5)
+        data.update({
+            'start_time_0': new_start.strftime('%m/%d/%Y'),
+            'start_time_1': new_start.strftime('%H:%M:%S'),
+        })
+        response = self.client.post(self.edit_current_url, data, follow=True)
+        #This post should redirect to the dashboard, with the correct message
+        #and 1 active entry, because we updated the current entry from setUp
+        self.assertRedirects(response, reverse('timepiece-entries'),
+            status_code=302, target_status_code=200)
+        self.assertContains(response,
+            'The entry has been updated successfully', count=1)
+        self.assertEquals(len(response.context['my_active_entries']), 1)
 
     def testCreateBlockByClosed(self):
         """