src/hdalab/fields.py
author ymh <ymh.work@gmail.com>
Thu, 26 Feb 2015 10:33:10 +0100
changeset 458 604b887e70c3
parent 359 46ad324f6fe4
child 545 c752fdee555b
permissions -rw-r--r--
add state history to renkan, correct get into post, generally prepare ground for mail management
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
353
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Nov 14, 2014
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
from https://gist.github.com/gsakkis/601977
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
to correct https://code.djangoproject.com/ticket/10227
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
@author: ymh
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
'''
359
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    10
from django.core.exceptions import ObjectDoesNotExist
353
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from django.db import models
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from django.db.models import fields as django_fields
359
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    13
from south.modelsinspector import add_introspection_rules
353
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
class OneToOneField(models.OneToOneField):    
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    def __init__(self, to, **kwargs):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        self.related_default = kwargs.pop('related_default', None)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        super(OneToOneField, self).__init__(to, **kwargs)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    def contribute_to_related_class(self, cls, related):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        setattr(cls, related.get_accessor_name(),
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
                SingleRelatedObjectDescriptor(related, self.related_default))
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
class SingleRelatedObjectDescriptor(django_fields.related.SingleRelatedObjectDescriptor):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    def __init__(self, related, default):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        super(SingleRelatedObjectDescriptor, self).__init__(related)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        self.default = default
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    def __get__(self, instance, instance_type=None):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        try:
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            return super(SingleRelatedObjectDescriptor, self).__get__(instance,
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
                                                                      instance_type)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        except ObjectDoesNotExist:
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
            if self.default is None:
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
                raise
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
            value = self.default(instance)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
            setattr(instance, self.cache_name, value)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
            if value is not None:
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
                setattr(value, self.related.field.get_cache_name(), instance)
359
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    42
            return value
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    43
        
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    44
add_introspection_rules([
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    45
    (
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    46
        [OneToOneField],
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    47
        [],         # Positional arguments (not used)
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    48
        {           # Keyword argument
458
604b887e70c3 add state history to renkan, correct get into post, generally prepare ground for mail management
ymh <ymh.work@gmail.com>
parents: 359
diff changeset
    49
            "related_default": ["related_default", {"default": None, "is_django_function": True}],
359
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    50
        },
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    51
    ),
46ad324f6fe4 Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents: 353
diff changeset
    52
], ["^hdalab\.fields\.OneToOneField"])