214 lines
6 KiB
Python
214 lines
6 KiB
Python
from django.contrib.gis.db import models
|
|
import oxlib
|
|
import flickr
|
|
import settings
|
|
try:
|
|
import json
|
|
except:
|
|
import simplejson as json
|
|
from oxweb import youtube
|
|
|
|
flickr.API_KEY = settings.FLICKR_API_KEY
|
|
flickr.SECRET = settings.FLICKR_SECRET
|
|
|
|
class Location(models.Model):
|
|
point = models.PointField()
|
|
name = models.CharField(max_length=255)
|
|
txt = models.TextField(blank=True, null=True)
|
|
objects = models.GeoManager()
|
|
|
|
def __unicode__(self):
|
|
return self.name
|
|
|
|
def geojson_as_dict(self):
|
|
return {
|
|
'type': 'Feature',
|
|
'geometry': {'type': 'Point', 'coordinates': [self.point.x, self.point.y]},
|
|
'properties': {'id': self.id}
|
|
}
|
|
|
|
def get_media(self):
|
|
d = {
|
|
'images': [],
|
|
'yvideos': [],
|
|
'pvideos': []
|
|
}
|
|
images = Image.objects.filter(location=self)
|
|
yvideos = YoutubeVideo.objects.filter(location=self)
|
|
pvideos = PadmaVideo.objects.filter(location=self)
|
|
for i in images:
|
|
d['images'].append(i.get_dict())
|
|
for y in yvideos:
|
|
d['yvideos'].append(y.get_dict())
|
|
for p in pvideos:
|
|
d['pvideos'].append(p.get_dict())
|
|
return d
|
|
|
|
class Image(models.Model):
|
|
url = models.URLField("FlickR URL")
|
|
caption = models.CharField(max_length=255, blank=True, null=True)
|
|
# category = models.ForeignKey("ImageCategory")
|
|
event = models.ForeignKey("Event")
|
|
location = models.ForeignKey(Location)
|
|
flickrData = models.TextField(blank=True, editable=False)
|
|
|
|
|
|
#this function is fucked. either just get user to enter flickr id, or write a recursive function with an array of regexps and pray. but this is just crap.
|
|
def get_flickr_id(self):
|
|
regex = r'http\:\/\/.*?\/\d{4}/(.*?)_.*'
|
|
id = oxlib.findRe(self.url, regex)
|
|
if id != '':
|
|
return id
|
|
else:
|
|
regex = r'http://.*?/.*?/.*?/(.*?)/in/.*'
|
|
return oxlib.findRe(self.url, regex)
|
|
|
|
def __unicode__(self):
|
|
return self.url + " - " + self.caption
|
|
|
|
def get_dict(self):
|
|
if self.flickrData != '':
|
|
return json.loads(self.flickrData)
|
|
else:
|
|
flickrPhoto = flickr.Photo(id=self.get_flickr_id())
|
|
sizes = flickrPhoto.getSizes()
|
|
# photoInfo = flickrPhoto.getInfo()
|
|
ret = {
|
|
'url': self.url,
|
|
'sizes': sizes,
|
|
'photoInfo': {
|
|
'title': flickrPhoto._Photo__title,
|
|
'description': flickrPhoto._Photo__description,
|
|
'owner': flickrPhoto._Photo__owner,
|
|
'tags': flickrPhoto._Photo__tags
|
|
},
|
|
'caption': self.caption,
|
|
'location': {
|
|
'id': self.location.id,
|
|
'name': self.location.name
|
|
},
|
|
'event': self.event.get_dict()
|
|
}
|
|
self.flickrData = json.dumps(ret)
|
|
self.save()
|
|
return ret
|
|
|
|
class Event(models.Model):
|
|
name = models.CharField(max_length=255)
|
|
description = models.TextField(blank=True)
|
|
start_time = models.DateField()
|
|
end_time = models.DateField()
|
|
|
|
def __unicode__(self):
|
|
return self.name
|
|
|
|
def get_dict(self):
|
|
return {
|
|
'name': self.name,
|
|
'description': self.description,
|
|
'start_time': self.start_time.strftime("%Y-%m-%d"),
|
|
'end_time': self.end_time.strftime("%Y-%m-%d")
|
|
}
|
|
|
|
def get_media(self):
|
|
d = {
|
|
'images': [],
|
|
'yvideos': [],
|
|
'pvideos': []
|
|
}
|
|
images = Image.objects.filter(event=self)
|
|
yvideos = YoutubeVideo.objects.filter(event=self)
|
|
pvideos = PadmaVideo.objects.filter(event=self)
|
|
for i in images:
|
|
d['images'].append(i.get_dict())
|
|
for y in yvideos:
|
|
d['yvideos'].append(y.get_dict())
|
|
for p in pvideos:
|
|
d['pvideos'].append(p.get_dict())
|
|
return d
|
|
|
|
class PadmaVideo(models.Model):
|
|
url = models.URLField("Padma URL")
|
|
title = models.CharField(max_length=255)
|
|
location = models.ForeignKey(Location)
|
|
event = models.ForeignKey(Event)
|
|
|
|
def __unicode__(self):
|
|
return self.title
|
|
|
|
class YoutubeVideo(models.Model):
|
|
url = models.URLField("Youtube URL")
|
|
caption = models.CharField(max_length=255, blank=True, null=True)
|
|
# category = models.ForeignKey("VideoCategory")
|
|
location = models.ForeignKey(Location)
|
|
event = models.ForeignKey(Event)
|
|
youtubeData = models.TextField(blank=True, editable=False)
|
|
|
|
def __unicode__(self):
|
|
return self.url + " - " + self.caption
|
|
|
|
def get_youtube_id(self):
|
|
reg = r'http.*?v=(.*?)$'
|
|
id = oxlib.findRe(self.url, reg)
|
|
if id == '':
|
|
reg = r'http://.*?/user/.*?/u/.*?/(.*?)$'
|
|
id = oxlib.findRe(self.url, reg)
|
|
return id
|
|
|
|
def get_dict(self):
|
|
if self.youtubeData != '':
|
|
return json.loads(self.youtubeData)
|
|
else:
|
|
youtube_id = self.get_youtube_id()
|
|
info = youtube.getMovieInfo(youtube_id)
|
|
# info['title'] = '' #Nasty, to avoid UnicodeDecodeError's when trying to do json.dumps on some video titles. FIXME.
|
|
|
|
#this is just ugly -- this shud be fixed by oxweb -- please remove when necessary:
|
|
info['thumbnail'] = "http://img.youtube.com/vi/%s/0.jpg" % (info['id'][:-22],)
|
|
|
|
ret = {
|
|
'info': info,
|
|
'location': {
|
|
'id': self.location.id,
|
|
'name': self.location.name
|
|
},
|
|
'event': self.event.get_dict()
|
|
}
|
|
self.youtubeData = json.dumps(ret)
|
|
self.save()
|
|
return ret
|
|
|
|
class Audio(models.Model):
|
|
fil = models.FileField(upload_to='audio/')
|
|
caption = models.CharField(max_length=255, blank=True, null=True)
|
|
txt = models.TextField(blank=True)
|
|
# category = models.ForeignKey("AudioCategory")
|
|
location = models.ForeignKey(Location)
|
|
event = models.ForeignKey("Event")
|
|
|
|
def __unicode__(self):
|
|
return self.url + " - " + self.caption
|
|
|
|
class Text(models.Model):
|
|
# url = models.URLField("FlickR URL")
|
|
text = models.TextField()
|
|
# category = models.ForeignKey("TextCategory")
|
|
location = models.ForeignKey(Location)
|
|
event = models.ForeignKey(Event)
|
|
|
|
def __unicode__(self):
|
|
return self.text + " - " + self.category.name
|
|
|
|
def get_dict(self):
|
|
ret = {
|
|
'text': self.text,
|
|
'location': {
|
|
'id': self.location.id,
|
|
'name': self.location.name
|
|
}
|
|
}
|
|
return ret
|
|
|
|
|
|
|
|
# Create your models here.
|