1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import datetime
from django.contrib.gis.db import models
from django.template.defaultfilters import slugify
from django import forms
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from locations.models import Location
from twython import Twython
# http://freewisdom.org/projects/python-markdown/
import markdown
class Note(models.Model):
slug = models.SlugField(unique_for_date='date_created', blank=True)
date_created = models.DateTimeField('Date', blank=True)
date_last_updated = models.DateTimeField('Date', blank=True)
point = models.PointField()
location = models.ForeignKey(Location, null=True, blank=True)
body_html = models.TextField(blank=True)
body_markdown = models.CharField('Note', max_length=450)
twitter_id = models.CharField('twitter_id', max_length=450)
def __str__(self):
return self.slug
def get_absolute_url(self):
return '/notes/%s/%s' % (self.date_created.strftime("%Y/%b").lower(), self.slug)
@property
def state(self):
return self.location.state
@property
def country(self):
return self.location.state.country
@property
def region(self):
return self.location.state.country.lux_region
@property
def longitude(self):
'''Get the site's longitude.'''
return self.point.x
@property
def latitude(self):
'''Get the site's latitude.'''
return self.point.y
def save(self):
self.body_html = markdown.markdown(self.body_markdown, extensions=['extra'], safe_mode=False)
self.date_last_updated = datetime.datetime.now()
if not self.date_created:
self.date_created = datetime.datetime.now()
if not self.slug:
self.slug = slugify(self.body_markdown)[:20]
try:
self.location = Location.objects.filter(geometry__contains=self.point).get()
except Location.DoesNotExist:
raise forms.ValidationError("There is no location associated with that point, add it: %sadmin/locations/location/add/" % (settings.BASE_URL))
super(Note, self).save()
@receiver(post_save, sender=Note)
def post_save_events(sender, instance, **kwargs):
t = Twython(settings.TWITTER_API_KEY, settings.TWITTER_API_SECRET, settings.TWITTER_ACCESS_TOKEN, settings.TWITTER_ACCESS_SECRET)
geo = t.reverse_geocode(lat=instance.latitude, lon=instance.longitude, accuracy=1500, granularity="city")
geo_id = geo['result']['places'][0]['id']
status = t.update_status(status='Look, Twitter', place_id=geo_id)
instance.twitter_id = status['id']
print(instance.twitter_id)
|