src/hdalab/fields.py
author ymh <ymh.work@gmail.com>
Fri, 14 Nov 2014 03:07:20 +0100
changeset 353 91c44b3fd11f
child 359 46ad324f6fe4
permissions -rw-r--r--
Correcterrors on missing dbpedia informations
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
'''
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
from django.db import models
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
from django.db.models import fields as django_fields
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
from django.core.exceptions import ObjectDoesNotExist
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
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
class OneToOneField(models.OneToOneField):    
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    def __init__(self, to, **kwargs):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        self.related_default = kwargs.pop('related_default', None)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        super(OneToOneField, self).__init__(to, **kwargs)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    def contribute_to_related_class(self, cls, related):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        setattr(cls, related.get_accessor_name(),
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
                SingleRelatedObjectDescriptor(related, self.related_default))
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
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
class SingleRelatedObjectDescriptor(django_fields.related.SingleRelatedObjectDescriptor):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
    def __init__(self, related, default):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        super(SingleRelatedObjectDescriptor, self).__init__(related)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        self.default = default
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
    def __get__(self, instance, instance_type=None):
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        try:
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            return super(SingleRelatedObjectDescriptor, self).__get__(instance,
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
                                                                      instance_type)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        except ObjectDoesNotExist:
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            if self.default is None:
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
                raise
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
            value = self.default(instance)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
            setattr(instance, self.cache_name, value)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
            if value is not None:
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
                setattr(value, self.related.field.get_cache_name(), instance)
91c44b3fd11f Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
            return value