<commit_before>import django.forms as forms
from src.photos.models import Photo
from src.photos.cs import palette
from src.settings import IMAGE_SIZE_BOUNDS, MEDIA_ROOT
from custom_widgets import *
from PIL import Image

class PhotoAddForm( forms.ModelForm ):

    #slug = forms.CharField(  label = 'SLUG',  widget = AdminSlugWidget )

    def save(self, force_insert=False, force_update=False, commit=True):

        m = super(PhotoAddForm, self).save(commit=False)

        # get the image's palette
        img = Image.open( m.image )
        p = palette( img, 8 )

        # save each color in "rgb(R,G,B)" format
        m.palette0 = "rgb%s" % str( p[0] )
        m.palette1 = "rgb%s" % str( p[1] )
        m.palette2 = "rgb%s" % str( p[2] )
        m.palette3 = "rgb%s" % str( p[3] )
        m.palette4 = "rgb%s" % str( p[4] )
        m.palette5 = "rgb%s" % str( p[5] )
        m.palette6 = "rgb%s" % str( p[6] )
        m.palette7 = "rgb%s" % str( p[7] )

        # resize the image
        img_path = "%s%s" % ( MEDIA_ROOT, m.image )
        img.thumbnail( IMAGE_SIZE_BOUNDS )
        img.save( img_path )

        if commit:
            m.save()

        return m

    class Meta:
        model  = Photo
        fields = ("title","image","slug","shot_date",)



class PhotoChangeForm( forms.ModelForm ):

    #slug          = forms.CharField(  label = 'SLUG',  widget = AdminSlugWidget )
    #text          = forms.CharField(  label = 'TEXT' )
    #caption       = forms.CharField(  label = 'CAPT' )

    stroke_color  = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    border_color  = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    title_color   = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    nav_color     = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    caption_color = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    post_color    = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )

    palette0      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette1      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette2      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette3      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette4      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette5      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette6      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette7      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )

    image         = forms.ImageField( label = 'PHOTO', widget = AdminImageWidget )
#    title         = forms.CharField(  label = 'TITLE', widget = AdminTitleWidget )
#    slug          = forms.CharField(  label = 'SLUG',  widget = AdminSlugWidget )
#    text          = forms.CharField(  label = 'TEXT',  widget = AdminTextWidget )
#    caption       = forms.CharField(  label = 'CAPT',  widget = AdminCaptionWidget )
#
#    stroke_color  = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    border_color  = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    title_color   = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    nav_color     = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    caption_color = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    post_color    = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#
#    #shot_date     = forms.CharField(  label = 'SHOT',  widget = AdminShotDateWidget )
#    palette0      = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    palette1      = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    palette2      = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    palette3      = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    palette4      = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    palette5      = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    palette6      = forms.CharField(  label = '',      widget = AdminSwatchWidget )
#    palette7      = forms.CharField(  label = '',      widget = AdminSwatchWidget )

    def save(self, force_insert=False, force_update=False, commit=True):

        m = super(PhotoChangeForm, self).save(commit=False)

        # get the image's palette
        img = Image.open( m.image )
        p = palette( img, 8 )

        # save each color in "rgb(R,G,B)" format
        m.palette0 = "rgb%s" % str( p[0] )
        m.palette1 = "rgb%s" % str( p[1] )
        m.palette2 = "rgb%s" % str( p[2] )
        m.palette3 = "rgb%s" % str( p[3] )
        m.palette4 = "rgb%s" % str( p[4] )
        m.palette5 = "rgb%s" % str( p[5] )
        m.palette6 = "rgb%s" % str( p[6] )
        m.palette7 = "rgb%s" % str( p[7] )

        # resize the image
        img_path = "%s%s" % ( MEDIA_ROOT, m.image )
        img.thumbnail( IMAGE_SIZE_BOUNDS )
        img.save( img_path )

        if commit:
            m.save()

        return m


    class Meta:
        model  = Photo
        fields = (
            "title",
            "image",
            "stroke_color",
            "border_color",
            "title_color",
            "nav_color",
            "caption_color",
            "post_color",
            "palette0",
            "palette1",
            "palette2",
            "palette3",
            "palette4",
            "palette5",
            "palette6",
            "palette7",
            "slug",
            "shot_date",
            )

<commit_msg>colors now set to the generated palette by default.<commit_after>import django.forms as forms
from src.photos.models import Photo
from src.photos.cs import palette
from src.settings import IMAGE_SIZE_BOUNDS, MEDIA_ROOT
from custom_widgets import *
from PIL import Image

class PhotoAddForm( forms.ModelForm ):

    def save(self, force_insert=False, force_update=False, commit=True):

        m = super(PhotoAddForm, self).save(commit=False)

        # get the image's palette
        img = Image.open( m.image )
        p = palette( img, 8 )

        # save each color in "rgb(R,G,B)" format
        m.palette0 = "rgb%s" % str( p[0] )
        m.palette1 = "rgb%s" % str( p[1] )
        m.palette2 = "rgb%s" % str( p[2] )
        m.palette3 = "rgb%s" % str( p[3] )
        m.palette4 = "rgb%s" % str( p[4] )
        m.palette5 = "rgb%s" % str( p[5] )
        m.palette6 = "rgb%s" % str( p[6] )
        m.palette7 = "rgb%s" % str( p[7] )
        m.stroke_color = m.palette0
        m.border_color = m.palette1
        m.title_color = m.palette2
        m.nav_color = m.palette3
        m.caption_color = m.palette4
        m.post_color = m.palette5

        # resize the image
        img_path = "%s%s" % ( MEDIA_ROOT, m.image )
        img.thumbnail( IMAGE_SIZE_BOUNDS )
        img.save( img_path )

        if commit:
            m.save()

        return m

    class Meta:
        model  = Photo
        fields = ("title","image","slug","shot_date",)



class PhotoChangeForm( PhotoAddForm ):

    image = forms.ImageField( label = 'PHOTO', widget = AdminImageWidget )

    stroke_color  = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    border_color  = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    title_color   = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    nav_color     = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    caption_color = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )
    post_color    = forms.CharField(  label = '',      widget = AdminEditableSwatchWidget )

    palette0      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette1      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette2      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette3      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette4      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette5      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette6      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )
    palette7      = forms.CharField(  label = '',      widget = AdminGeneratedSwatchWidget )

    def save(self, force_insert=False, force_update=False, commit=True):

        m = super(PhotoAddForm, self).save(commit=False)

        # get the image's palette
        img = Image.open( m.image )
        p = palette( img, 8 )

        # save each color in "rgb(R,G,B)" format
        m.palette0 = "rgb%s" % str( p[0] )
        m.palette1 = "rgb%s" % str( p[1] )
        m.palette2 = "rgb%s" % str( p[2] )
        m.palette3 = "rgb%s" % str( p[3] )
        m.palette4 = "rgb%s" % str( p[4] )
        m.palette5 = "rgb%s" % str( p[5] )
        m.palette6 = "rgb%s" % str( p[6] )
        m.palette7 = "rgb%s" % str( p[7] )

        # resize the image
        img_path = "%s%s" % ( MEDIA_ROOT, m.image )
        img.thumbnail( IMAGE_SIZE_BOUNDS )
        img.save( img_path )


        if commit:
            m.save()

        return m


    class Meta:
        model  = Photo
        fields = (
            "title",
            "slug",
            "shot_date",
            "image",
            "stroke_color",
            "border_color",
            "title_color",
            "nav_color",
            "caption_color",
            "post_color",
            "palette0",
            "palette1",
            "palette2",
            "palette3",
            "palette4",
            "palette5",
            "palette6",
            "palette7",
            )


{'fraction_non_alphanumeric': 0.12380009896091045, 'fraction_numerical': 0.01128154379020287, 'mean_word_length': 2.074536051110435, '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': '5244845', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3227, 'n_words': 702}