def calculate_duration(self): try: dur = Duration(self.duration_text) dur_td = dur.timedelta() self.duration = dur_td except InvalidDuration: pass
def post(self, request): iid = request.POST.get('iid', None) item = get_object_or_404(Item, iid=iid) email = request.POST.get('email', None) email = normalize_email(email) user = get_object_or_404(UserProfile, email=email) status = request.POST.get('status', '') resolve_time = request.POST.get('resolve_time', '') comment = request.POST.get('comment', '') if status == 'FIXED': item.status = 'RESOLVED' item.r_status = 'FIXED' item.add_event("RESOLVED", user, comment) item.save() item.update_email( "%s $%d %s updated\n%s\n" % (item.type, item.iid, item.title, comment), user) elif comment != "": item.add_comment(user, comment, comment) item.update_email( "comment added to %s $%d %s updated\n%s\n" % (item.type, item.iid, item.title, comment), user) if resolve_time != "": try: d = Duration(resolve_time) td = d.timedelta() item.add_resolve_time(user, td) except InvalidDuration: # eventually, this needs to get back to the user # via form validation, but for now # we just deal with it... pass item.save() data = ItemSerializer(item, context={'request': request}).data return Response(data)
def test_cmp(self): d1 = Duration("10 minutes") d2 = Duration("601 seconds") self.assertTrue(d1 < d2) self.assertTrue(d1 <= d2) self.assertFalse(d1 > d2) self.assertFalse(d1 >= d2)
def add_item(self, type='action item', title="", assigned_to=None, owner=None, milestone=None, priority=1, description="", estimated_time="1 hour", status='OPEN', r_status='', tags=None): try: d = Duration(estimated_time) except InvalidDuration: d = Duration("0 minutes") td = d.timedelta() item = Item.objects.create( milestone=milestone, type=type, owner=owner, assigned_to=assigned_to, title=title, priority=priority, status=status, r_status=r_status, estimated_time=td, target_date=milestone.target_date, last_mod=datetime.now(), description=description) item.add_event('OPEN', owner, "<b>%s added</b>" % type) if tags: item.tags.add(*tags) item.setup_default_notification() item.add_project_notification() item.update_email("%s added" % type, owner) milestone.update_milestone()
def test_eq(self): d1 = Duration("10 minutes") d2 = Duration("10 m") d3 = Duration("600 seconds") d4 = Duration("601 seconds") self.assertTrue(d1 == d2) self.assertTrue(d1 == d3) self.assertTrue(d3 == d1) self.assertTrue(d1 != d4)
def log_time(item, user, request): t = request.POST.get('time', False) if t: try: d = Duration(request.POST.get('time', "1 hour")) td = d.timedelta() item.add_resolve_time(user, td) except InvalidDuration: pass
def test_get_resolve_time_multiple_times(self): i = ItemFactory() u = UserProfileFactory() td = Duration('1 hour').timedelta() i.add_resolve_time(u, td) u = UserProfileFactory() td = Duration('1 hour').timedelta() i.add_resolve_time(u, td) resolve_time = i.get_resolve_time() self.assertEqual(resolve_time, timedelta(0, 7200))
def test_arithmetic(self): d1 = Duration("10 seconds") d2 = Duration("20 seconds") self.assertEqual(d1 + d1, d2) self.assertEqual(d2 - d1, d1) self.assertEqual(d1 * 2, d2) self.assertEqual(2 * d1, d2) d1 += d1 self.assertEqual(d1, d2) d2 -= d1 self.assertEqual(d2, 0.0)
def post(self, request, pk): item = get_object_or_404(Item, iid=pk) user = get_object_or_404(Claim, django_user=request.user).pmt_user try: d = Duration(request.POST.get('time', "1 hour")) except InvalidDuration: # eventually, this needs to get back to the user # via form validation, but for now # we just deal with it... d = Duration("0 minutes") td = d.timedelta() item.add_resolve_time(user, td) return HttpResponse("ok")
def test_get_resolve_zero(self): i = ItemFactory() u = UserProfileFactory() td = Duration('0h').timedelta() i.add_resolve_time(u, td) resolve_time = i.get_resolve_time() self.assertEqual(resolve_time, timedelta(0, 0))
def clean(self, value): try: d = Duration(value).timedelta() except InvalidDuration: d = timedelta(hours=1) return super(SimpleDurationField, self).clean(d)
def test_add_resolve_time(self): i = ItemFactory() u = UserProfileFactory() td = Duration('1 hour').timedelta() i.add_resolve_time(u, td) self.assertEqual(ActualTime.objects.count(), 1) actualtime = ActualTime.objects.first() self.assertEqual(actualtime.actual_time, timedelta(0, 3600))
def new_duration(timestr): try: d = Duration(timestr) except InvalidDuration: d = None # If the user's string is a number with no unit, simpleduration # throws an InvalidDuration exception. In this case, we'll assume # the user is talking about the number of hours. if d is None and re.match(r'\d+', timestr): try: d = Duration(timestr + 'h') except InvalidDuration: d = None if d is None: d = Duration('0 minutes') return d
def post(self, request): iid = request.POST.get('iid', None) item = get_object_or_404(Item, iid=iid) email = request.POST.get('email', None) email = normalize_email(email) user = get_object_or_404(UserProfile, email=email) status = request.POST.get('status', '') resolve_time = request.POST.get('resolve_time', '') comment = request.POST.get('comment', '') if status == 'FIXED': item.status = 'RESOLVED' item.r_status = 'FIXED' item.add_event("RESOLVED", user, comment) item.save() item.update_email( "%s $%d %s updated\n%s\n" % ( item.type, item.iid, item.title, comment), user) elif comment != "": item.add_comment(user, comment, comment) item.update_email( "comment added to %s $%d %s updated\n%s\n" % ( item.type, item.iid, item.title, comment), user) if resolve_time != "": try: d = Duration(resolve_time) td = d.timedelta() item.add_resolve_time(user, td) except InvalidDuration: # eventually, this needs to get back to the user # via form validation, but for now # we just deal with it... pass item.save() data = ItemSerializer(item, context={'request': request}).data return Response(data)
def post(self, request): pid = request.POST.get('pid', None) task = request.POST.get('task', None) try: d = Duration(request.POST.get('time', "1 hour")) except InvalidDuration: # eventually, this needs to get back to the user # via form validation, but for now # we just deal with it... d = Duration("0 minutes") client_uni = request.POST.get('client', '') td = d.timedelta() # two required fields if None in [pid, task]: return HttpResponse("bad request") project = get_object_or_404(Project, pid=pid) user = get_object_or_404(Claim, django_user=request.user).pmt_user milestone = project.upcoming_milestone() item = Item.objects.create(milestone=milestone, type='action item', owner=user, assigned_to=user, title=task, status='VERIFIED', priority=1, target_date=milestone.target_date, estimated_time=td) if client_uni != '': r = Client.objects.filter(email=client_uni + "@columbia.edu") if r.count() > 0: item.add_clients([r[0]]) else: pass item.add_resolve_time(user, td) return HttpResponse("ok")
def add_item(self, type='action item', title="", assigned_to=None, owner=None, milestone=None, priority=1, description="", estimated_time="1 hour", status='OPEN', r_status='', tags=None): try: d = Duration(estimated_time) except InvalidDuration: d = Duration("0 minutes") td = d.timedelta() item = Item.objects.create(milestone=milestone, type=type, owner=owner, assigned_to=assigned_to, title=title, priority=priority, status=status, r_status=r_status, estimated_time=td, target_date=milestone.target_date, last_mod=datetime.now(), description=description) item.add_event('OPEN', owner, "<b>%s added</b>" % type) if tags: item.tags.add(*tags) item.setup_default_notification() item.add_project_notification() item.update_email("%s added" % type, owner) milestone.update_milestone()
def post(self, request): pid = request.POST.get('pid', None) task = request.POST.get('task', None) try: d = Duration(request.POST.get('time', "1 hour")) except InvalidDuration: # eventually, this needs to get back to the user # via form validation, but for now # we just deal with it... d = Duration("0 minutes") client_uni = request.POST.get('client', '') td = d.timedelta() # two required fields if None in [pid, task]: return HttpResponse("bad request") project = get_object_or_404(Project, pid=pid) user = get_object_or_404(Claim, django_user=request.user).pmt_user milestone = project.upcoming_milestone() item = Item.objects.create( milestone=milestone, type='action item', owner=user, assigned_to=user, title=task, status='VERIFIED', priority=1, target_date=milestone.target_date, estimated_time=td) if client_uni != '': r = Client.objects.filter( email=client_uni + "@columbia.edu") if r.count() > 0: item.add_clients([r[0]]) else: pass item.add_resolve_time(user, td) return HttpResponse("ok")
def clean(self): cleaned_data = super().clean() start_data = cleaned_data.get('start_datetime') duration_text = cleaned_data.get('duration_text') duration_worked = False try: dur = Duration(duration_text) dur_td = dur.timedelta() duration = dur_td end_data = start_data + duration qs1 = Reservation.objects.filter(start_datetime__lte=end_data, end_datetime__gte=start_data) duration_worked = True except InvalidDuration: pass if duration_worked == False: raise forms.ValidationError('Esta duracion no es valida') elif start_data < timezone.now(): raise forms.ValidationError('No puedes agendar una reunion para el pasado') elif qs1.exists(): raise forms.ValidationError('Ya hay una reunion que empiza o termina en el momento seleccionado')
def test_single_cases(self): for s, expected in SINGLE_TEST_CASES: d = Duration(s) self.assertEqual(int(d.timedelta().total_seconds()), expected)
def test_combined_cases(self): for s, expected in COMBINED_TEST_CASES: d = Duration(s) self.assertEqual(int(d.timedelta().total_seconds()), expected)