2010-10-24 16:17:50 +02:00
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")
2010-10-25 14:48:29 +02:00
event = models . ForeignKey ( " Event " )
2010-10-24 16:17:50 +02:00
location = models . ForeignKey ( Location )
flickrData = models . TextField ( blank = True , editable = False )
2010-10-26 01:14:35 +02:00
#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.
2010-10-24 16:17:50 +02:00
def get_flickr_id ( self ) :
regex = r ' http \ : \ / \ /.*? \ / \ d {4} /(.*?)_.* '
2010-10-26 01:14:35 +02:00
id = oxlib . findRe ( self . url , regex )
if id != ' ' :
return id
else :
regex = r ' http://.*?/.*?/.*?/(.*?)/in/.* '
2010-10-24 16:17:50 +02:00
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 ( )
2010-10-26 01:49:01 +02:00
# photoInfo = flickrPhoto.getInfo()
2010-10-24 16:17:50 +02:00
ret = {
' url ' : self . url ,
' sizes ' : sizes ,
2010-10-26 01:49:01 +02:00
' photoInfo ' : {
' title ' : flickrPhoto . _Photo__title ,
' description ' : flickrPhoto . _Photo__description ,
' owner ' : flickrPhoto . _Photo__owner ,
' tags ' : flickrPhoto . _Photo__tags
} ,
2010-10-24 16:17:50 +02:00
' 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 {
2010-10-26 01:17:41 +02:00
' name ' : self . name ,
' description ' : self . description ,
2010-10-26 01:33:51 +02:00
' start_time ' : self . start_time . strftime ( " % Y- % m- %d " ) ,
' end_time ' : self . end_time . strftime ( " % Y- % m- %d " )
2010-10-24 16:17:50 +02:00
}
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 ) :
2010-10-25 14:48:29 +02:00
return self . url + " - " + self . caption
2010-10-24 16:17:50 +02:00
def get_youtube_id ( self ) :
reg = r ' http.*?v=(.*?)$ '
2010-10-26 01:42:50 +02:00
id = oxlib . findRe ( self . url , reg )
if id == ' ' :
reg = r ' http://.*?/user/.*?/u/.*?/(.*?)$ '
id = oxlib . findRe ( self . url , reg )
return id
2010-10-24 16:17:50 +02:00
def get_dict ( self ) :
if self . youtubeData != ' ' :
return json . loads ( self . youtubeData )
else :
youtube_id = self . get_youtube_id ( )
info = youtube . getMovieInfo ( youtube_id )
2010-10-26 01:33:51 +02:00
# info['title'] = '' #Nasty, to avoid UnicodeDecodeError's when trying to do json.dumps on some video titles. FIXME.
2010-10-24 16:17:50 +02:00
#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 )
2010-10-25 14:48:29 +02:00
event = models . ForeignKey ( " Event " )
2010-10-24 16:17:50 +02:00
def __unicode__ ( self ) :
2010-10-25 14:48:29 +02:00
return self . url + " - " + self . caption
2010-10-24 16:17:50 +02:00
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.