| author | ymh <ymh.work@gmail.com> |
| Fri, 22 Jan 2010 18:23:34 +0100 | |
| changeset 11 | f236caaceb43 |
| parent 9 | 60555e0d17f4 |
| child 25 | 474430a0a43d |
| permissions | -rw-r--r-- |
| 5 | 1 |
import os |
2 |
||
3 |
from django.db import models |
|
4 |
from django.contrib import admin |
|
5 |
import uuid |
|
6 |
import photologue.models |
|
7 |
from django.conf import settings |
|
|
9
60555e0d17f4
commit patch from issue : http://code.google.com/p/django-modeltranslation/issues/detail?id=1
ymh <ymh.work@gmail.com>
parents:
7
diff
changeset
|
8 |
from modeltranslation.utils import build_localized_fieldname |
| 11 | 9 |
from modeltranslation.translator import translator |
10 |
import tagging |
|
11 |
||
| 5 | 12 |
|
13 |
def get_sid(): |
|
14 |
return unicode(uuid.uuid1()) |
|
15 |
||
| 11 | 16 |
class Ioi(models.Model): |
17 |
||
18 |
class Meta: |
|
19 |
abstract = True |
|
| 5 | 20 |
|
21 |
sid = models.CharField(max_length=36, unique=True, blank=False, null=False, db_index=True, editable=False, default=get_sid) |
|
| 11 | 22 |
loc = models.CharField(max_length=512, unique=False, blank=True, null=True) |
23 |
loc_radius = models.FloatField(null=True, blank=True) |
|
| 5 | 24 |
title = models.CharField(max_length=1024, unique=True, blank=False, null=False) |
25 |
short_title = models.CharField(max_length=512, unique=True, blank=False, null=False) |
|
26 |
description = models.TextField(blank=True, null=True) |
|
| 11 | 27 |
|
| 5 | 28 |
photos = models.ForeignKey(photologue.models.Gallery, null=True) |
29 |
||
30 |
def serialize_to_dict(self): |
|
31 |
res = { |
|
32 |
"sid": self.sid, |
|
33 |
} |
|
34 |
||
| 11 | 35 |
# serialize translated fields |
36 |
trans_options = None |
|
37 |
try: |
|
38 |
trans_options = translator.get_options_for_model(self.__class__) |
|
39 |
except: |
|
40 |
trans_options = None |
|
41 |
if trans_options: |
|
42 |
for fieldname in trans_options.localized_fieldnames: |
|
43 |
lang_dict = {} |
|
44 |
for l in settings.LANGUAGES: |
|
45 |
lang = l[0] |
|
46 |
lfieldname = build_localized_fieldname(fieldname, lang) |
|
47 |
lang_dict[lang] = self.__dict__[lfieldname] |
|
48 |
res[fieldname] = lang_dict |
|
49 |
||
| 5 | 50 |
res["loc"] = {} |
51 |
if self.loc: |
|
52 |
loc_array = self.loc.split(",") |
|
53 |
res["loc"] = { "type" : "Point", "coordinates": [float(loc_array[0]),float(loc_array[1])] } |
|
54 |
||
55 |
res["loc_radius"] = self.loc_radius |
|
56 |
||
57 |
res["photos"] = [] |
|
58 |
||
59 |
if self.photos: |
|
60 |
for photo in self.photos.photos.all(): |
|
61 |
res["photos"].append({"title":photo.title,"url":settings.WEB_URL.strip("/")+photo.image.url}) |
|
62 |
||
63 |
return res |
|
64 |
||
| 11 | 65 |
|
66 |
||
67 |
class Roi(Ioi): |
|
68 |
||
69 |
def __unicode__(self): |
|
70 |
return "%s : %s" % (unicode(self.pk),unicode(self.title)) |
|
71 |
||
72 |
||
73 |
class Poi(Ioi): |
|
74 |
||
75 |
roi = models.ForeignKey(Roi, null=False, related_name="region") |
|
76 |
base_tag = models.CharField(max_length=512, unique=True, blank=False, null=False, db_index=True) |
|
77 |
tags = tagging.fields.TagField(max_length=2048, blank=True, null=True) |
|
78 |
||
79 |
def serialize_to_dict(self): |
|
80 |
res = super(Poi, self).serialize_to_dict() |
|
81 |
res["roi"] = self.roi.sid |
|
82 |
res["base_tag"] = self.base_tag |
|
83 |
res["tags"] = self.tags |
|
84 |
return res |
|
85 |
||
86 |
def __unicode__(self): |
|
87 |
return "%s : %s" % (unicode(self.pk),unicode(self.title)) |
|
88 |
||
89 |
||
90 |
||
91 |
admin.site.register(Roi) |
|
92 |
admin.site.register(Poi) |
|
93 |
#tagging.register(Poi) |
|
94 |