| 1 | from Globals import InitializeClass |
|---|
| 2 | from AccessControl import ClassSecurityInfo |
|---|
| 3 | from Products.Archetypes.Registry import registerField |
|---|
| 4 | from Products.Archetypes.Field import ObjectField, Field |
|---|
| 5 | from Products.ATGoogleMaps.widget import LatLngWidget |
|---|
| 6 | |
|---|
| 7 | import validator |
|---|
| 8 | |
|---|
| 9 | class LatLngField(ObjectField): |
|---|
| 10 | """A field that store latitude and longitude value""" |
|---|
| 11 | __implements__ = ObjectField.__implements__ |
|---|
| 12 | |
|---|
| 13 | _properties = Field._properties.copy() |
|---|
| 14 | _properties.update({ |
|---|
| 15 | 'type': 'latlng', |
|---|
| 16 | 'default': {}, |
|---|
| 17 | 'size': 12, |
|---|
| 18 | 'default': None, |
|---|
| 19 | 'widget': LatLngWidget, |
|---|
| 20 | 'validators': ('LatLngValidator'), |
|---|
| 21 | }) |
|---|
| 22 | |
|---|
| 23 | security = ClassSecurityInfo() |
|---|
| 24 | |
|---|
| 25 | security.declarePrivate('validate_required') |
|---|
| 26 | def validate_required(self, instance, value, errors): |
|---|
| 27 | try: |
|---|
| 28 | float(value.latitude) |
|---|
| 29 | float(value.longitude) |
|---|
| 30 | except (ValueError, TypeError): |
|---|
| 31 | result = False |
|---|
| 32 | else: |
|---|
| 33 | result = True |
|---|
| 34 | return ObjectField.validate_required(self, instance, result, errors) |
|---|
| 35 | |
|---|
| 36 | security.declarePrivate('get') |
|---|
| 37 | def get(self, instance, **kwargs): |
|---|
| 38 | return ObjectField.get(self, instance, **kwargs) |
|---|
| 39 | |
|---|
| 40 | security.declarePrivate('set') |
|---|
| 41 | def set(self, instance, value, **kwargs): |
|---|
| 42 | if type(value) != type({}) and hasattr(value, 'keys'): |
|---|
| 43 | new_value = {} |
|---|
| 44 | new_value.update(value) |
|---|
| 45 | value = new_value |
|---|
| 46 | |
|---|
| 47 | ObjectField.set(self, instance, value, **kwargs) |
|---|
| 48 | |
|---|
| 49 | InitializeClass(LatLngField) |
|---|
| 50 | |
|---|
| 51 | registerField(LatLngField, |
|---|
| 52 | title="LatLng", |
|---|
| 53 | description="Used to store longitude and longitude.", |
|---|
| 54 | ) |
|---|
| 55 | |
|---|