Skip to content

Commit

Permalink
Improve library structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Silva committed May 11, 2022
1 parent 81ddecf commit 0e69964
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
28 changes: 6 additions & 22 deletions colorfield/fields.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
# -*- coding: utf-8 -*-

from django import VERSION as DJANGO_VERSION

from colorfield.utils import get_image_file_background_color
from colorfield.validators import color_hex_validator, color_hexa_validator
from colorfield.widgets import ColorWidget

import django

if django.VERSION >= (1, 8):
if DJANGO_VERSION >= (1, 8):
from django.core.exceptions import FieldDoesNotExist
else:
FieldDoesNotExist = Exception
from django.core.exceptions import ImproperlyConfigured
from django.core.validators import RegexValidator

from django.db.models import CharField, signals
from django.db.models.fields.files import ImageField

if django.VERSION >= (2, 0):
from django.utils.translation import gettext_lazy as _
else:
from django.utils.translation import ugettext_lazy as _

import re


COLOR_HEX_RE = re.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")
color_hex_validator = RegexValidator(
COLOR_HEX_RE, _("Enter a valid hex color, eg. #000000"), "invalid"
)

COLOR_HEXA_RE = re.compile("#([A-Fa-f0-9]{8}|[A-Fa-f0-9]{4})$")
color_hexa_validator = RegexValidator(
COLOR_HEXA_RE, _("Enter a valid hexa color, eg. #00000000"), "invalid"
)

VALIDATORS_PER_FORMAT = {"hex": color_hex_validator, "hexa": color_hexa_validator}

Expand Down Expand Up @@ -105,7 +89,7 @@ def _get_image_field_color(self, instance):
image_file = getattr(instance, self.image_field)
if image_file:
alpha = self.format == "hexa"
if django.VERSION >= (2, 0):
if DJANGO_VERSION >= (2, 0):
# https://stackoverflow.com/a/3033986/2096218
with image_file.open() as _:
color = get_image_file_background_color(image_file, alpha)
Expand Down
21 changes: 21 additions & 0 deletions colorfield/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from re import compile as re_compile

from django import VERSION as DJANGO_VERSION
from django.core.validators import RegexValidator

if DJANGO_VERSION >= (2, 0):
from django.utils.translation import gettext_lazy as _
else:
from django.utils.translation import ugettext_lazy as _


COLOR_HEX_RE = re_compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")
color_hex_validator = RegexValidator(
COLOR_HEX_RE, _("Enter a valid hex color, eg. #000000"), "invalid"
)


COLOR_HEXA_RE = re_compile("#([A-Fa-f0-9]{8}|[A-Fa-f0-9]{4})$")
color_hexa_validator = RegexValidator(
COLOR_HEXA_RE, _("Enter a valid hexa color, eg. #00000000"), "invalid"
)
4 changes: 2 additions & 2 deletions colorfield/widgets.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-

from django import forms
from django.conf import settings
from django.forms import TextInput
from django.template.loader import render_to_string


class ColorWidget(forms.TextInput):
class ColorWidget(TextInput):

template_name = "colorfield/color.html"

Expand Down

0 comments on commit 0e69964

Please sign in to comment.