# Copyright (c) Ecometrica. All rights reserved.
# Distributed under the BSD license. See LICENSE for details.

import re

from django.db.models.signals import pre_save, post_save
from django.utils.translation import ugettext, ugettext_lazy

class VinaigretteError(Exception):
    pass
    
_registry = {}

DOUBLE_PERCENTAGE_RE = re.compile(u'%(?!\()')

def _vinaigrette_pre_save(sender, instance, **kwargs):
    setattr(instance, '_vinaigrette_saving', True)
    
def _vinaigrette_post_save(sender, instance, **kwargs):
    delattr(instance, '_vinaigrette_saving')

def register(model, fields, restrict_to=None, manager=None):
    """Tell vinaigrette which fields on a Django model should be translated.
    
    Arguments:
    model -- The relevant model class
    fields -- A list or tuple of field names. e.g. ['name', 'nickname']
    restrict_to -- Optional. A django.db.models.Q object representing the subset
        of objects to collect translation strings from.
    manager -- Optional. A reference to a manager -- e.g. Person.objects -- to use
        when collecting translation strings.
        
    Note that both restrict_to and manager are only used when collecting translation
    strings. Gettext lookups will always be performed on relevant fields for all
    objects on registered models.
    """
    
    global _registry
    _registry[model] = {
        'fields': fields,
        'restrict_to': restrict_to,
        'manager': manager
    }
    for field in fields:
        setattr(model, field, VinaigretteDescriptor(field))
    model.untranslated = lambda self, fieldname: self.__dict__[fieldname]
    
    pre_save.connect(_vinaigrette_pre_save, sender=model)
    post_save.connect(_vinaigrette_post_save, sender=model)

class VinaigretteDescriptor(object):
    
    def __init__(self, name):
        self.name = name

    def __get__(self, obj, type=None):
        if obj:
            key = obj.__dict__[self.name]
        else:
            return object.__getattribute__(type, self.name)
        if not key:
            return key
        if getattr(obj, '_vinaigrette_saving', False):
            return key

        # We double over all the keys to mimic how {% trans %} works
        key = DOUBLE_PERCENTAGE_RE.sub(u'%%', key)
        return ugettext(key).replace('%%', '%')

    def __set__(self, obj, value):
        obj.__dict__[self.name] = value

@@ -173,16 +173,94 @@
 st_save%0A
+from django.conf import settings%0Afrom django.core.urlresolvers import reverse%0A
 from dja
@@ -311,16 +311,40 @@
 ext_lazy
+, activate, get_language
 %0A%0Aclass 
@@ -2476,8 +2476,831 @@
 = value%0A
+%0A%0Aclass VinaigrettteAdminLanguageMiddleware(object):%0A    %22%22%22%0A    Use this middleware to ensure the admin is always running under the%0A    default language, to prevent vinaigrette from clobbering the registered%0A    fields with the user's picked language in the change views. Aslo make%0A    sure that this is after any LocaleMiddleware like classes.%0A    %22%22%22%0A%0A    def is_admin_request(self, request):%0A        %22%22%22%0A        Returns True if this request is for the admin views.%0A        %22%22%22%0A        return request.path.startswith(reverse('admin:index'))%0A%0A    def process_request(self, request):%0A        print(%22***** RORY: path: %25r admin: %25r%22 %25 ( request.path, reverse('admin:index')))%0A        if not self.is_admin_request(request):%0A            return None%0A%0A        # We are in the admin site%0A        activate(settings.LANGUAGE_CODE)%0A

Add VinaigrettteAdminLanguageMiddleware to avoid using vinaigrette in the admin.