From 2667ab499fdb86cfd32f3f033094f09ad38660d5 Mon Sep 17 00:00:00 2001 From: "Mr. Senko" Date: Tue, 13 Mar 2018 23:33:03 +0200 Subject: [PATCH] Remove custom DurationField, replace with IntegerField the custom field derives from models.IntegerField so replace with that. Next we're going to make use of Django's own models.DurationField. --- tcms/core/forms/fields.py | 27 -------------- tcms/core/models/fields.py | 37 ------------------- tcms/core/utils/timedelta2int.py | 25 ------------- tcms/testcases/forms.py | 6 +-- tcms/testcases/migrations/0001_initial.py | 3 +- tcms/testcases/models.py | 3 +- tcms/testruns/forms.py | 4 +- tcms/testruns/migrations/0001_initial.py | 3 +- ...estrun_estimated_time_remove_max_length.py | 15 +++----- tcms/testruns/models.py | 3 +- tcms/xmlrpc/api/testcase.py | 5 +-- 11 files changed, 16 insertions(+), 115 deletions(-) delete mode 100644 tcms/core/utils/timedelta2int.py diff --git a/tcms/core/forms/fields.py b/tcms/core/forms/fields.py index 6b263d227d..ca81bf1393 100644 --- a/tcms/core/forms/fields.py +++ b/tcms/core/forms/fields.py @@ -4,10 +4,8 @@ from django.db.models import Q from django.core.exceptions import ValidationError from django.contrib.auth.models import User -from django.utils.translation import ugettext_lazy as _ from tcms.core.utils import string_to_list -from tcms.core.utils.timedelta2int import timedelta2int class UserField(forms.CharField): @@ -46,31 +44,6 @@ def clean(self, value): raise ValidationError('Unknown user: "%s"' % value) -class DurationField(forms.CharField): - """ - Customizing forms CharFiled validation. - estimated_time using integer mix with d(ay), h(our), m(inute) - """ - default_error_messages = { - 'invalid': _(u'Enter a valid estimated time. e.g. 12h45m'), - } - - def __init__(self, *args, **kwargs): - super(DurationField, self).__init__(*args, **kwargs) - - def validate(self, value): - super(DurationField, self).validate(value) - try: - from tcms.xmlrpc.utils import pre_process_estimated_time - pre_process_estimated_time(value) - except ValueError: - raise forms.ValidationError(self.error_messages['invalid']) - - def clean(self, value): - value = super(DurationField, self).clean(value) - return timedelta2int(value) - - class MultipleEmailField(forms.EmailField): def clean(self, value): """ diff --git a/tcms/core/models/fields.py b/tcms/core/models/fields.py index 73b3a6ad67..3c8eec55b4 100644 --- a/tcms/core/models/fields.py +++ b/tcms/core/models/fields.py @@ -1,6 +1,4 @@ # -*- coding: utf-8 -*- -import datetime - try: # may not be running under MySQL from MySQLdb.constants import FIELD_TYPE @@ -8,38 +6,3 @@ django_conversions.update({FIELD_TYPE.TIME: None}) except ImportError: pass - -from django.db.models.fields import IntegerField - -from tcms.core.forms.fields import DurationField as DurationFormField - - -class DurationField(IntegerField): - def from_db_value(self, value, expression, connection): - return self.to_python(value) - - def to_python(self, value): - if isinstance(value, int): - return datetime.timedelta(seconds=value) - elif isinstance(value, datetime.timedelta): - return value - else: - raise TypeError('Unable to convert %s to timedelta.' % value) - - def get_db_prep_value(self, value, connection, prepared): - """convert datetime.timedelta to seconds. - - 1 day equal to 86400 seconds - """ - if isinstance(value, datetime.timedelta): - return value.seconds + (86400 * value.days) - else: - value = super(DurationField, self).get_db_prep_value(value, - connection, - prepared) - return value - - def formfield(self, form_class=DurationFormField, **kwargs): - defaults = {'help_text': 'Enter duration in the format: DDHHMM'} - defaults.update(kwargs) - return form_class(**defaults) diff --git a/tcms/core/utils/timedelta2int.py b/tcms/core/utils/timedelta2int.py deleted file mode 100644 index a280db8f79..0000000000 --- a/tcms/core/utils/timedelta2int.py +++ /dev/null @@ -1,25 +0,0 @@ -# -*- coding: utf-8 -*- -import re - - -def timedelta2int(value): - '''convert string nh(ay):nh(our):nm(inute) to integer seconds.''' - value = value.replace(' ', '') - second_regex = re.compile(r'\d+s') - minute_regex = re.compile(r'\d+m') - hour_regex = re.compile(r'\d+h') - day_regex = re.compile(r'\d+d') - - second = second_regex.findall(value) - seconds = int(second[0][:-1]) if second else 0 - - minute = minute_regex.findall(value) - minutes = int(minute[0][:-1]) if minute else 0 - - hour = hour_regex.findall(value) - hours = int(hour[0][:-1]) if hour else 0 - - day = day_regex.findall(value) - days = int(day[0][:-1]) if day else 0 - - return seconds + minutes * 60 + hours * 3600 + days * 86400 diff --git a/tcms/testcases/forms.py b/tcms/testcases/forms.py index a6cef97fa2..668991a20d 100644 --- a/tcms/testcases/forms.py +++ b/tcms/testcases/forms.py @@ -3,7 +3,7 @@ from tinymce.widgets import TinyMCE -from tcms.core.forms.fields import UserField, DurationField, StripURLField +from tcms.core.forms.fields import UserField, StripURLField from tcms.core.utils import string_to_list from tcms.core.utils.validations import validate_bug_id from tcms.testplans.models import TestPlan @@ -105,7 +105,7 @@ class BaseCaseForm(forms.Form): widget=forms.Textarea, required=False ) - estimated_time = DurationField(label='Estimated Time', initial='0m', required=False) + estimated_time = forms.DurationField(label='Estimated Time', initial='0', required=False) setup = forms.CharField(label="Setup", widget=TinyMCE(), required=False) action = forms.CharField(label="Actions", widget=TinyMCE(), required=False) effect = forms.CharField(label="Expect results", widget=TinyMCE(), required=False) @@ -219,7 +219,7 @@ class CaseNotifyForm(forms.Form): class XMLRPCBaseCaseForm(BaseCaseForm): - estimated_time = DurationField(required=False) + estimated_time = forms.DurationField(required=False) is_automated = forms.ChoiceField( choices=FULL_AUTOMATED_CHOICES, widget=forms.CheckboxSelectMultiple(), diff --git a/tcms/testcases/migrations/0001_initial.py b/tcms/testcases/migrations/0001_initial.py index efb919196b..f0c20a6752 100644 --- a/tcms/testcases/migrations/0001_initial.py +++ b/tcms/testcases/migrations/0001_initial.py @@ -3,7 +3,6 @@ from django.db import migrations, models import tcms.core.models.base -import tcms.core.models.fields class Migration(migrations.Migration): @@ -46,7 +45,7 @@ class Migration(migrations.Migration): ('requirement', models.CharField(max_length=255, blank=True)), ('alias', models.CharField(max_length=255, blank=True)), ('estimated_time', - tcms.core.models.fields.DurationField(default=0, db_column='estimated_time')), + models.IntegerField(default=0, db_column='estimated_time')), ('notes', models.TextField(blank=True)), ], options={ diff --git a/tcms/testcases/models.py b/tcms/testcases/models.py index ccf332f8b3..fd542f87dc 100644 --- a/tcms/testcases/models.py +++ b/tcms/testcases/models.py @@ -13,7 +13,6 @@ from tcms.core.models import TCMSActionModel from tcms.core.models.base import TCMSContentTypeBaseModel -from tcms.core.models.fields import DurationField from tcms.core.utils.checksum import checksum from tcms.core.utils.timedeltaformat import format_timedelta from tcms.issuetracker.types import IssueTrackerType @@ -128,7 +127,7 @@ class TestCase(TCMSActionModel): summary = models.CharField(max_length=255) requirement = models.CharField(max_length=255, blank=True, null=True) alias = models.CharField(max_length=255, blank=True) - estimated_time = DurationField(db_column='estimated_time', default=0) + estimated_time = models.IntegerField(db_column='estimated_time', default=0) # DurationField notes = models.TextField(blank=True, null=True) case_status = models.ForeignKey(TestCaseStatus, on_delete=models.CASCADE) diff --git a/tcms/testruns/forms.py b/tcms/testruns/forms.py index 30a5f2e1ae..adf711fd2f 100644 --- a/tcms/testruns/forms.py +++ b/tcms/testruns/forms.py @@ -3,7 +3,7 @@ from django.contrib.auth.models import User from tcms.core.utils import string_to_list -from tcms.core.forms.fields import UserField, DurationField +from tcms.core.forms.fields import UserField from tcms.management.models import Product, Version, Build, EnvGroup from tcms.testplans.models import TestPlan from tcms.testcases.models import TestCase @@ -42,7 +42,7 @@ class BaseRunForm(forms.Form): queryset=Product.objects.all(), empty_label=None, ) - estimated_time = DurationField(required=False) + estimated_time = forms.DurationField(required=False) product_version = forms.ModelChoiceField( label='Product Version', queryset=Version.objects.none(), diff --git a/tcms/testruns/migrations/0001_initial.py b/tcms/testruns/migrations/0001_initial.py index d3ba90a074..cc0c5a66f0 100644 --- a/tcms/testruns/migrations/0001_initial.py +++ b/tcms/testruns/migrations/0001_initial.py @@ -2,7 +2,6 @@ from django.db import migrations, models import tcms.core.models.base from django.conf import settings -import tcms.core.models.fields class Migration(migrations.Migration): @@ -72,7 +71,7 @@ class Migration(migrations.Migration): ('stop_date', models.DateTimeField(db_index=True, null=True, blank=True)), ('summary', models.TextField()), ('notes', models.TextField(blank=True)), - ('estimated_time', tcms.core.models.fields.DurationField(default=0, max_length=11)), + ('estimated_time', models.IntegerField(default=0)), ('environment_id', models.IntegerField(default=0)), ('auto_update_run_status', models.BooleanField(default=False)), ('build', models.ForeignKey(related_name='build_run', to='management.TestBuild', diff --git a/tcms/testruns/migrations/0003_testrun_estimated_time_remove_max_length.py b/tcms/testruns/migrations/0003_testrun_estimated_time_remove_max_length.py index eab2b37d13..1d6470fd71 100644 --- a/tcms/testruns/migrations/0003_testrun_estimated_time_remove_max_length.py +++ b/tcms/testruns/migrations/0003_testrun_estimated_time_remove_max_length.py @@ -1,18 +1,15 @@ # -*- coding: utf-8 -*- from django.db import migrations -import tcms.core.models.fields class Migration(migrations.Migration): - + """ + As part of migrating to Django's own DurationField + we don't need this anymore. + """ dependencies = [ ('testruns', '0002_add_initial_data'), ] - operations = [ - migrations.AlterField( - model_name='testrun', - name='estimated_time', - field=tcms.core.models.fields.DurationField(default=0), - ), - ] + # deliberately left empty + operations = [] diff --git a/tcms/testruns/models.py b/tcms/testruns/models.py index e16a2ff6f7..da4f6995c1 100755 --- a/tcms/testruns/models.py +++ b/tcms/testruns/models.py @@ -11,7 +11,6 @@ import vinaigrette from tcms.core.contrib.linkreference.models import LinkReference -from tcms.core.models.fields import DurationField from tcms.core.models import TCMSActionModel from tcms.core.utils import is_int from tcms.core.utils.timedeltaformat import format_timedelta @@ -36,7 +35,7 @@ class TestRun(TCMSActionModel): stop_date = models.DateTimeField(null=True, blank=True, db_index=True) summary = models.TextField() notes = models.TextField(blank=True) - estimated_time = DurationField(default=0) + estimated_time = models.IntegerField(default=0) # todo: DurationField plan = models.ForeignKey('testplans.TestPlan', related_name='run', on_delete=models.CASCADE) diff --git a/tcms/xmlrpc/api/testcase.py b/tcms/xmlrpc/api/testcase.py index ccbc88c3b4..e50b8c8816 100644 --- a/tcms/xmlrpc/api/testcase.py +++ b/tcms/xmlrpc/api/testcase.py @@ -5,7 +5,6 @@ from modernrpc.core import rpc_method, REQUEST_KEY from tcms.core.utils import string_to_list, form_errors_to_list -from tcms.core.utils.timedelta2int import timedelta2int from tcms.management.models import Tag from tcms.management.models import Component from tcms.testcases.models import TestCase @@ -315,9 +314,7 @@ def filter(query): :rtype: list(dict) """ if query.get('estimated_time'): - query['estimated_time'] = timedelta2int( - pre_process_estimated_time(query.get('estimated_time')) - ) + query['estimated_time'] = pre_process_estimated_time(query.get('estimated_time')) results = [] for case in TestCase.objects.filter(**query):