<commit_before>import logging

from django.db.models import get_model, ObjectDoesNotExist
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.utils.safestring import mark_safe

from django.conf import settings

logger = logging.getLogger(__name__)

from galleries.settings import IMAGE_CROPPING_RATIO

ASPECT_RATIO = "/".join(IMAGE_CROPPING_RATIO.split("x"))

class GalleryWidget(object):
    """
    This widget is ment to be used with the GalleryField, this provides
    all the javascript and stylesheets that make the GalleryField work.
    """
    
    class Media:
        js = (
            getattr(settings, 'JQUERY_URL', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js'),
            getattr(settings, 'JQUERY_UI_URL', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'),
            "galleries/javascripts/lib/jquery.Jcrop.min.js",
            "galleries/javascripts/lib/json2.js",
            "galleries/javascripts/lib/underscore-min.js",
            "galleries/javascripts/lib/backbone-min.js",
            "galleries/javascripts/lib/jquery.upload.js",
            "galleries/javascripts/lib/jquery.drop.js",
            "galleries/javascripts/lib/fancybox/jquery.fancybox-1.3.4.pack.js",
            "galleries/javascripts/gallery.js",
        )
        css= {'all' : (
            "galleries/javascripts/lib/fancybox/jquery.fancybox-1.3.4.css",
            "galleries/stylesheets/lib/jquery.Jcrop.min.css",
            "galleries/stylesheets/gallery.css",
        )}


class GalleryForeignKeyWidget(ForeignKeyRawIdWidget, GalleryWidget):

    def __init__(self, *args, **kwargs):
        super(GalleryForeignKeyWidget, self).__init__(*args, **kwargs)

    def render(self, name, value, *args, **kwargs):
        
        output = [super(GalleryForeignKeyWidget, self).render(name, value, *args, **kwargs)]

        if value:
            output.append('<hr style="margin: 10px 0 10px 0;" />')
            output.append('<div data-aspect-ratio="%s" data-id="%s" id="gallery">' % (ASPECT_RATIO, value))
            output.append('<ul>')
            
            # Get model based on app name and model name
            app_name = self.rel.to._meta.app_label
            model_name = self.rel.to._meta.object_name.lower()
            try:
                gallery = get_model(app_name, model_name
                    ).objects.get(pk=value)
            except ObjectDoesNotExist:
                logger.error("Can't find object: %s.%s with primary key %s "
                    "for displaying gallery." % (app_name, model_name, value))
            
            # Add images
            for image in gallery.images.all():
                output.append('<li>')
                output.append('<a href="%s">' % image.croppable)
                output.append('<img data-id="%s" data-org-width="%s" data-org-height="%s" src="%s" />' % (image.id, image.image.width, image.image.height, image.thumbnail))
                output.append('</a>')<commit_msg>Included argument for admin site <commit_after>import logging

from django.db.models import get_model, ObjectDoesNotExist
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.utils.safestring import mark_safe

from django.conf import settings

logger = logging.getLogger(__name__)

from galleries.settings import IMAGE_CROPPING_RATIO

ASPECT_RATIO = "/".join(IMAGE_CROPPING_RATIO.split("x"))

class GalleryWidget(object):
    """
    This widget is ment to be used with the GalleryField, this provides
    all the javascript and stylesheets that make the GalleryField work.
    """
    
    class Media:
        js = (
            getattr(settings, 'JQUERY_URL', 'https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js'),
            getattr(settings, 'JQUERY_UI_URL', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js'),
            "galleries/javascripts/lib/jquery.Jcrop.min.js",
            "galleries/javascripts/lib/json2.js",
            "galleries/javascripts/lib/underscore-min.js",
            "galleries/javascripts/lib/backbone-min.js",
            "galleries/javascripts/lib/jquery.upload.js",
            "galleries/javascripts/lib/jquery.drop.js",
            "galleries/javascripts/lib/fancybox/jquery.fancybox-1.3.4.pack.js",
            "galleries/javascripts/gallery.js",
        )
        css= {'all' : (
            "galleries/javascripts/lib/fancybox/jquery.fancybox-1.3.4.css",
            "galleries/stylesheets/lib/jquery.Jcrop.min.css",
            "galleries/stylesheets/gallery.css",
        )}


class GalleryForeignKeyWidget(ForeignKeyRawIdWidget, GalleryWidget):

    def __init__(self, *args, **kwargs):
        from django.contrib.admin.sites import site
        args += (site,)
        super(GalleryForeignKeyWidget, self).__init__(*args, **kwargs)

    def render(self, name, value, *args, **kwargs):
        
        output = [super(GalleryForeignKeyWidget, self).render(name, value, *args, **kwargs)]

        if value:
            output.append('<hr style="margin: 10px 0 10px 0;" />')
            output.append('<div data-aspect-ratio="%s" data-id="%s" id="gallery">' % (ASPECT_RATIO, value))
            output.append('<ul>')
            
            # Get model based on app name and model name
            app_name = self.rel.to._meta.app_label
            model_name = self.rel.to._meta.object_name.lower()
            try:
                gallery = get_model(app_name, model_name
                    ).objects.get(pk=value)
            except ObjectDoesNotExist:
                logger.error("Can't find object: %s.%s with primary key %s "
                    "for displaying gallery." % (app_name, model_name, value))
            
            # Add images
            for image in gallery.images.all():
                output.append('<li>')
                output.append('<a href="%s">' % image.croppable)
                output.append('<img data-id="%s" data-org-width="%s" data-org-height="%s" src="%s" />' % (image.id, image.image.width, image.image.height, image.thumbnail))
                output.append('</a>')
{'fraction_non_alphanumeric': 0.133115823817292, 'fraction_numerical': 0.006199021207177814, 'mean_word_length': 2.4328107502799554, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 4, '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': '7108857', 'n_tokens_mistral': 1955, 'n_tokens_neox': 1848, 'n_words': 338}