| author | ymh <ymh.work@gmail.com> |
| Tue, 07 Jul 2015 15:59:31 +0200 | |
| changeset 640 | 939461cc322b |
| parent 545 | c752fdee555b |
| permissions | -rw-r--r-- |
|
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 |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
|
| 545 | 14 |
def isalambda(v): |
15 |
LAMBDA = lambda:0 |
|
16 |
return isinstance(v, type(LAMBDA)) and v.__name__ == LAMBDA.__name__ |
|
|
353
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
|
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
class OneToOneField(models.OneToOneField): |
| 545 | 19 |
def __init__(self, *args, **kwargs): |
|
353
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
self.related_default = kwargs.pop('related_default', None) |
| 545 | 21 |
#This is BAD. Did this to avoid more work in Django 1.7 migration |
22 |
#TODO: correct this |
|
23 |
if self.related_default == "lambda:": |
|
24 |
self.related_default = lambda instance: None |
|
25 |
super(OneToOneField, self).__init__(*args, **kwargs) |
|
|
353
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
|
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
def contribute_to_related_class(self, cls, related): |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
setattr(cls, related.get_accessor_name(), |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
SingleRelatedObjectDescriptor(related, self.related_default)) |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
|
| 545 | 31 |
def deconstruct(self): |
32 |
name, path, args, kwargs = super(OneToOneField, self).deconstruct() |
|
33 |
if self.related_default is not None: |
|
34 |
# this is VERY dirty and works only in our application. |
|
35 |
#TODO: correct this... |
|
36 |
if isalambda(self.related_default): |
|
37 |
kwargs['related_default'] = "lambda:" |
|
38 |
else: |
|
39 |
kwargs['related_default'] = self.related_default |
|
40 |
return name, path, args, kwargs |
|
|
353
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
|
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
class SingleRelatedObjectDescriptor(django_fields.related.SingleRelatedObjectDescriptor): |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
def __init__(self, related, default): |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
super(SingleRelatedObjectDescriptor, self).__init__(related) |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
self.default = default |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
|
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
def __get__(self, instance, instance_type=None): |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
try: |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
return super(SingleRelatedObjectDescriptor, self).__get__(instance, |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
instance_type) |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
except ObjectDoesNotExist: |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
if self.default is None: |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
raise |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
value = self.default(instance) |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
setattr(instance, self.cache_name, value) |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
if value is not None: |
|
91c44b3fd11f
Correcterrors on missing dbpedia informations
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
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
|
58 |
return value |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
353
diff
changeset
|
59 |