<commit_before>from __future__ import unicode_literals
import base64
import datetime

from django.db import models
from django.utils import timezone
from django.utils.text import slugify
from django.core.exceptions import ValidationError
from django.utils.numberformat import format

from ckeditor.fields import RichTextField

from . import PAYMENT_FREQUENCIES, BOOL_CHOICES
from geography.models import Country
from products.models import Type, Category, ProhibitedItem


class LogisticsModel(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class Logo(models.Model):
    name = models.CharField(max_length=200)
    _encoded_data = models.TextField()

    def base64_logo(self):
        return self._encoded_data

    def __str__(self):
        return "{0}".format(self.name)


class SellerModel(models.Model):
    name = models.CharField(max_length=200, unique=True,)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class SupportChannel(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class UploadMethod(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class Currency(models.Model):
    name = models.CharField(max_length=200, unique=True)
    symbol = models.CharField(max_length=1, blank=True, null=True)

    def __str__(self):
        return "{0} {1}".format(self.name, self.symbol)

    class Meta:
        verbose_name_plural = "Currencies"
        ordering = ('name',)


class Brand(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class ApprovalModel(models.Model):

    class Meta:
        abstract = True

    published = models.BooleanField(default=False)
    approval_fields = []

    def clean(self):
        """
        If the published flag is True, then go over all of the fields that must be populated for publishing, and check
        that they have a value
        """

        if self.published:
            errors = {}

            for field in self.approval_fields:
                value = getattr(self, field, None)
                if value is None or value == '':
                    errors[field] = 'This field must be filled in for publishing'

            if errors:
                raise ValidationError(errors)<commit_msg>fixed validation to work on many-to-many fields<commit_after>from __future__ import unicode_literals
import base64
import datetime

from django.db import models
from django.utils import timezone
from django.utils.text import slugify
from django.core.exceptions import ValidationError
from django.utils.numberformat import format

from ckeditor.fields import RichTextField

from . import PAYMENT_FREQUENCIES, BOOL_CHOICES
from geography.models import Country
from products.models import Type, Category, ProhibitedItem


class LogisticsModel(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class Logo(models.Model):
    name = models.CharField(max_length=200)
    _encoded_data = models.TextField()

    def base64_logo(self):
        return self._encoded_data

    def __str__(self):
        return "{0}".format(self.name)


class SellerModel(models.Model):
    name = models.CharField(max_length=200, unique=True,)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class SupportChannel(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class UploadMethod(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class Currency(models.Model):
    name = models.CharField(max_length=200, unique=True)
    symbol = models.CharField(max_length=1, blank=True, null=True)

    def __str__(self):
        return "{0} {1}".format(self.name, self.symbol)

    class Meta:
        verbose_name_plural = "Currencies"
        ordering = ('name',)


class Brand(models.Model):
    name = models.CharField(max_length=200, unique=True)

    def __str__(self):
        return "{0}".format(self.name)

    class Meta:
        ordering = ('name',)


class ApprovalModel(models.Model):

    class Meta:
        abstract = True

    published = models.BooleanField(default=False)
    approval_fields = []

    def clean(self):
        """
        If the published flag is True, then go over all of the fields that must be populated for publishing, and check
        that they have a value
        """

        if self.published:
            errors = {}

            for field_name in self.approval_fields:
                field = getattr(self, field_name, None)
                if getattr(field, 'all', False):
                    value = field.all()
                    if len(value) == 0:
                        errors[field_name] = 'This field must be filled in for publishing'
                else:
                    value = field
                    if value is None or value == '':
                        errors[field_name] = 'This field must be filled in for publishing'

            if errors:
                raise ValidationError(errors)
{'fraction_non_alphanumeric': 0.10258605925672204, 'fraction_numerical': 0.011817091967802706, 'mean_word_length': 2.707936507936508, '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': '303213', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1646, 'n_words': 452}