<commit_before>from django import forms
from app.models import FermentationProfile, FermentationProfilePoint
from django.core import validators
import fermentrack_django.settings as settings

from django.forms import ModelForm

import re
import datetime
import pytz


# For the actual fermentation profile points, we're going to do something more complex. For the overriding
# FermentationProfile object, however, let's just use a model form.
class FermentationProfileForm(ModelForm):
    class Meta:
        model = FermentationProfile
        fields = ['name']

    def __init__(self, *args, **kwargs):
        super(FermentationProfileForm, self).__init__(*args, **kwargs)
        for this_field in self.fields:
            self.fields[this_field].widget.attrs['class'] = "form-control"


class FermentationProfilePointForm(forms.Form):
    ttl = forms.CharField(help_text="Time from the start of fermentation at which the temperature will be reached. " +
                                    "(Example: 7d 3h 15m 4s)",
                          widget=forms.TextInput(attrs={'placeholder': 'Ex: 7d 3h 15m 4s'}),
                          validators=[validators.RegexValidator(regex="([0-9]+[ ]*[ywdhms]{1}[ ]*)+",
                                                                message="Invalid TTL format")])
    temperature_setting = forms.DecimalField(min_value=-20, max_value=212, max_digits=5, decimal_places=2,
                                             widget=forms.TextInput(
                                                 attrs={'placeholder': 'Ex: 35'}),
                                             help_text="Temperature goal when TTL is reached")

    # Check that the ttl format is valid, and if it is, replace it with a datetime delta object
    def clean_ttl(self):
        # TODO - Convert this to instead use FermentationProfilePoint.string_to_ttl
        # tz = pytz.timezone(getattr(settings, 'TIME_ZONE', False))
        ttl_text = self.cleaned_data['ttl']

        # Split out the d/h/m/s of the timer
        try:
            timer_pattern = r"(?P<time_amt>[0-9]+)[ ]*(?P<ywdhms>[ywdhms]{1})"
            timer_regex = re.compile(timer_pattern)
            timer_matches = timer_regex.finditer(ttl_text)
        except:
            raise forms.ValidationError("TTL format is invalid")


        # timer_time is equal to now + the time delta
        time_delta = datetime.timedelta(seconds=0)
        for this_match in timer_matches:
            dhms = this_match.group('ywdhms')
            delta_amt = int(this_match.group('time_amt'))
            if dhms == 'y':  # This doesn't account for leap years, but whatever.
                time_delta = time_delta + datetime.timedelta(days=(365*delta_amt))
            elif dhms == 'w':
                time_delta = time_delta + datetime.timedelta(weeks=delta_amt)
            elif dhms == 'd':
                time_delta = time_delta + datetime.timedelta(days=delta_amt)
            elif dhms == 'h':
                time_delta = time_delta + datetime.timedelta(hours=delta_amt)
            elif dhms == 'm':
                time_delta = time_delta + datetime.timedelta(minutes=delta_amt)
            elif dhms == 's':
                time_delta = time_delta + datetime.timedelta(seconds=delta_amt)

        # return self.cleaned_data['ttl']
        return time_delta


class FermentationProfileImportForm(forms.Form):
    # TODO - Add a placeholder here
    import_text = forms.CharField(widget=forms.Textarea(attrs={'style': "font-family:monospace; font-size:11pt;", 'wrap': 'off'}),
                                  help_text="The text of the exported profile")


class FermentationProfileCopyForm(forms.Form):
    new_profile_name = forms.CharField(help_text="The text of the exported profile", max_length=128)


class FermentationProfileRenameForm(forms.Form):
    profile_name = forms.CharField(help_text="The new name for the profile", max_length=128)<commit_msg>Convert clean_ttl to use FermentationProfilePoint.string_to_ttl<commit_after>from django import forms
from app.models import FermentationProfile, FermentationProfilePoint
from django.core import validators
import fermentrack_django.settings as settings

from django.forms import ModelForm

import re
import datetime
import pytz


# For the actual fermentation profile points, we're going to do something more complex. For the overriding
# FermentationProfile object, however, let's just use a model form.
class FermentationProfileForm(ModelForm):
    class Meta:
        model = FermentationProfile
        fields = ['name']

    def __init__(self, *args, **kwargs):
        super(FermentationProfileForm, self).__init__(*args, **kwargs)
        for this_field in self.fields:
            self.fields[this_field].widget.attrs['class'] = "form-control"


class FermentationProfilePointForm(forms.Form):
    ttl = forms.CharField(help_text="Time from the start of fermentation at which the temperature will be reached. " +
                                    "(Example: 7d 3h 15m 4s)",
                          widget=forms.TextInput(attrs={'placeholder': 'Ex: 7d 3h 15m 4s'}),
                          validators=[validators.RegexValidator(regex="([0-9]+[ ]*[ywdhms]{1}[ ]*)+",
                                                                message="Invalid TTL format")])
    temperature_setting = forms.DecimalField(min_value=-20, max_value=212, max_digits=5, decimal_places=2,
                                             widget=forms.TextInput(
                                                 attrs={'placeholder': 'Ex: 35'}),
                                             help_text="Temperature goal when TTL is reached")

    # Check that the ttl format is valid, and if it is, replace it with a datetime delta object
    def clean_ttl(self):
        if 'ttl' in self.cleaned_data:
            ttl_text = self.cleaned_data['ttl']
        else:
            return None

        if len(ttl_text) <= 1:
            return None

        return FermentationProfilePoint.string_to_ttl(ttl_text)


class FermentationProfileImportForm(forms.Form):
    # TODO - Add a placeholder here
    import_text = forms.CharField(widget=forms.Textarea(attrs={'style': "font-family:monospace; font-size:11pt;", 'wrap': 'off'}),
                                  help_text="The text of the exported profile")


class FermentationProfileCopyForm(forms.Form):
    new_profile_name = forms.CharField(help_text="The text of the exported profile", max_length=128)


class FermentationProfileRenameForm(forms.Form):
    profile_name = forms.CharField(help_text="The new name for the profile", max_length=128)
{'fraction_non_alphanumeric': 0.09495101733232857, 'fraction_numerical': 0.010248681235870385, 'mean_word_length': 2.5260361317747075, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4398748', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1771, 'n_words': 477}