<commit_before>from django.conf import settings
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.shortcuts import render
from django.utils.translation import ugettext_lazy as _

import json
import re

from wagtail.wagtailcore.models import PageBase, Page, Orderable
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
from wagtail.wagtailforms.backends.email import EmailFormProcessor

from modelcluster.fields import ParentalKey
from .forms import FormBuilder


FORM_FIELD_CHOICES = (
    ('singleline',   _('Single line text')),
    ('multiline',    _('Multi-line text')),
    ('email',        _('Email')),
    ('number',       _('Number')),
    ('url',          _('URL')),
    ('checkbox',     _('Checkbox')),
    ('checkboxes',   _('Checkboxes')),
    ('dropdown',     _('Drop down')),
    ('radio',        _('Radio buttons')),
    ('date',         _('Date')),
    ('datetime',     _('Date/time')),
)


HTML_EXTENSION_RE = re.compile(r"(.*)\.html")


class FormSubmission(models.Model):
    """Data for a Form submission."""

    form_data = models.TextField()
    page = models.ForeignKey(Page)

    submit_time = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)

    def get_data(self):
        return json.loads(self.form_data)

    def __unicode__(self):
        return self.form_data


class AbstractFormField(Orderable):
    """Database Fields required for building a Django Form field."""

    label = models.CharField(
        max_length=255,
        help_text=_('The label of the form field')
    )
    field_type = models.CharField(max_length=16, choices=FORM_FIELD_CHOICES)
    required = models.BooleanField(default=True)
    choices = models.CharField(
        max_length=512,
        blank=True,
        help_text=_('Comma seperated list of choices. Only applicable in checkboxes, radio and dropdown.')
    )
    default_value = models.CharField(
        max_length=255,
        blank=True,
        help_text=_('Default value. Comma seperated values supported for checkboxes.')
    )
    help_text = models.CharField(max_length=255, blank=True)

    panels = [
        FieldPanel('label'),
        FieldPanel('field_type'),
        FieldPanel('required'),
        FieldPanel('choices'),
        FieldPanel('default_value'),
        FieldPanel('help_text'),
    ]

    class Meta:
        abstract = True


FORM_MODEL_CLASSES = []
_FORM_CONTENT_TYPES = []


def get_form_types():
    global _FORM_CONTENT_TYPES
    if len(_FORM_CONTENT_TYPES) != len(FORM_MODEL_CLASSES):
        _FORM_CONTENT_TYPES = [
            ContentType.objects.get_for_model(cls) for cls in FORM_MODEL_CLASSES
        ]
    return _FORM_CONTENT_TYPES


class FormBase(PageBase):
    """Metaclass for Forms"""
    def __init__(cls, name, bases, dct):
        super(FormBase, cls).__init__(name, bases, dct)

        if not cls.is_abstract:
            # register this type in the list of page content types
            FORM_MODEL_CLASSES.append(cls)
            # Check if form_processing_backend is ok
            if hasattr(cls, 'form_processing_backend'):
                cls.form_processing_backend.validate_usage(cls)

<commit_msg>Fix AbstractFormField to not lose the sort_order ordering<commit_after>from django.conf import settings
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.shortcuts import render
from django.utils.translation import ugettext_lazy as _

import json
import re

from wagtail.wagtailcore.models import PageBase, Page, Orderable
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
from wagtail.wagtailforms.backends.email import EmailFormProcessor

from modelcluster.fields import ParentalKey
from .forms import FormBuilder


FORM_FIELD_CHOICES = (
    ('singleline',   _('Single line text')),
    ('multiline',    _('Multi-line text')),
    ('email',        _('Email')),
    ('number',       _('Number')),
    ('url',          _('URL')),
    ('checkbox',     _('Checkbox')),
    ('checkboxes',   _('Checkboxes')),
    ('dropdown',     _('Drop down')),
    ('radio',        _('Radio buttons')),
    ('date',         _('Date')),
    ('datetime',     _('Date/time')),
)


HTML_EXTENSION_RE = re.compile(r"(.*)\.html")


class FormSubmission(models.Model):
    """Data for a Form submission."""

    form_data = models.TextField()
    page = models.ForeignKey(Page)

    submit_time = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)

    def get_data(self):
        return json.loads(self.form_data)

    def __unicode__(self):
        return self.form_data


class AbstractFormField(Orderable):
    """Database Fields required for building a Django Form field."""

    label = models.CharField(
        max_length=255,
        help_text=_('The label of the form field')
    )
    field_type = models.CharField(max_length=16, choices=FORM_FIELD_CHOICES)
    required = models.BooleanField(default=True)
    choices = models.CharField(
        max_length=512,
        blank=True,
        help_text=_('Comma seperated list of choices. Only applicable in checkboxes, radio and dropdown.')
    )
    default_value = models.CharField(
        max_length=255,
        blank=True,
        help_text=_('Default value. Comma seperated values supported for checkboxes.')
    )
    help_text = models.CharField(max_length=255, blank=True)

    panels = [
        FieldPanel('label'),
        FieldPanel('field_type'),
        FieldPanel('required'),
        FieldPanel('choices'),
        FieldPanel('default_value'),
        FieldPanel('help_text'),
    ]

    class Meta:
        abstract = True
        ordering = ['sort_order']


FORM_MODEL_CLASSES = []
_FORM_CONTENT_TYPES = []


def get_form_types():
    global _FORM_CONTENT_TYPES
    if len(_FORM_CONTENT_TYPES) != len(FORM_MODEL_CLASSES):
        _FORM_CONTENT_TYPES = [
            ContentType.objects.get_for_model(cls) for cls in FORM_MODEL_CLASSES
        ]
    return _FORM_CONTENT_TYPES


class FormBase(PageBase):
    """Metaclass for Forms"""
    def __init__(cls, name, bases, dct):
        super(FormBase, cls).__init__(name, bases, dct)

        if not cls.is_abstract:
            # register this type in the list of page content types
            FORM_MODEL_CLASSES.append(cls)
            # Check if form_processing_backend is ok
            if hasattr(cls, 'form_processing_backend'):
                cls.form_processing_backend.validate_usage(cls)


{'fraction_non_alphanumeric': 0.10963603818615751, 'fraction_numerical': 0.004176610978520286, 'mean_word_length': 3.230283911671924, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7296179', 'n_tokens_mistral': 2083, 'n_tokens_neox': 2019, 'n_words': 465}