summaryrefslogtreecommitdiff
path: root/lib/upload/views.py
blob: 53ac642300fb2de57db3dfab7e820cc803fee929 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from models import FileUpload
from django.shortcuts import render_to_response
from django.http import HttpResponse, Http404
from django.template import RequestContext
from django.conf import settings
import urllib, urlparse, datetime

def all(request):
    if not request.user.is_staff:
        raise Http404
    files = FileUpload.objects.all().order_by('-upload_date')
    return render_to_response('upload/base.html', {'files': files, 'textarea_id': request.GET['textarea']}, context_instance=RequestContext(request))

def images(request):
    if not request.user.is_staff:
        raise Http404
    files = FileUpload.objects.filter(content_type = 'image').order_by('-upload_date')
    return render_to_response('upload/base.html', {'files': files, 'textarea_id': request.GET['textarea']}, context_instance=RequestContext(request))
    
def files(request):
    if not request.user.is_staff:
        raise Http404
    not_files = ['video', 'image']
    files = FileUpload.objects.exclude(content_type__in = not_files).order_by('-upload_date')
    return render_to_response('upload/base.html', {'files': files, 'textarea_id': request.GET['textarea']}, context_instance=RequestContext(request))
    
def youtube(request):
    if not request.user.is_staff:
        raise Http404
    import elementtree.ElementTree as ET
    try:
        user = settings.YOU_TUBE_USER
        needs_user_setting = False
    except AttributeError:
        user = 'NBC'
        needs_user_setting = True
    gdata_feed = "http://gdata.youtube.com/feeds/videos?author=%s&orderby=updated" % (user,)
    root = ET.parse(urllib.urlopen(gdata_feed)).getroot()
    videos = []
    for e in root.findall('{http://www.w3.org/2005/Atom}entry'):
        video = {}
        video['title'] = e.findtext('{http://www.w3.org/2005/Atom}title')
        date = e.findtext('{http://www.w3.org/2005/Atom}published').split('T')[0]
        video['upload_date'] = date
        media = e.find('{http://search.yahoo.com/mrss/}group')
        video['description'] = media.findtext('{http://search.yahoo.com/mrss/}description')
        video['thumb'] = media.find('{http://search.yahoo.com/mrss/}thumbnail').attrib['url']
        video['image'] = media.findall('{http://search.yahoo.com/mrss/}thumbnail')[-1].attrib['url']
        video['url'] = media.find('{http://search.yahoo.com/mrss/}content').attrib['url']
        videos.append(video)
    return render_to_response('upload/youtube.html', {'videos': videos, 'textarea_id': request.GET['textarea'], 'needs_user_setting': needs_user_setting}, context_instance=RequestContext(request))
    
def flickr(request):
    if not request.user.is_staff:
         raise Http404
    import flickr
    try:
        user = settings.FLICKR_USER
        flickr.API_KEY = settings.FLICKR_API_KEY
    except AttributeError:
        return HttpResponse('You need to set <tt>FLICKR_USER</tt> and <tt>FLICKR_API_KEY</tt> in your settings file. <br />&larr; <a href="/uploads/?textarea=%s">Back to all uploads.</a>' % (request.GET['textarea'],))
    # Get first 12 photos for the user
    flickr_photos = flickr.people_getPublicPhotos(user, 12, 1)
    photos = []
    #this loop is too slow. needs caching or a better library?
    for f in flickr_photos:
        photo = {}
        photo['url'] = f.getURL('Small', 'source')
        photo['link'] = f.getURL()
        photo['title'] = f._Photo__title
        photo['upload_date'] = datetime.datetime.fromtimestamp(float(f._Photo__dateposted))
        photos.append(photo)
    return render_to_response('upload/flickr.html', {'photos': photos, 'textarea_id': request.GET['textarea']}, context_instance=RequestContext(request))
    
def download(request):
    '''Saves image from URL and returns ID for use with AJAX script'''
    if not request.user.is_staff:
        raise Http404
    if request.method == 'GET':
        #f = FileUpload();
        #f.title = request.GET['title'] or 'untitled'
        #f.description = request.GET['description']
        url = urllib.unquote(request.GET['photo'])
        file_content = urllib.urlopen(url).read()
        file_name = url.split('/')[-1]
        fullpath = '%s%s/%s' %(settings.IMAGES_ROOT,datetime.datetime.today().strftime("%Y"),file_name)
        photo = urllib.urlretrieve(request.GET['photo'], fullpath)
        f, created = FileUpload.objects.get_or_create(
                title = request.GET['title'] or 'untitled',
                description = request.GET['description'],
                upload = 'images/%s/%s' %(datetime.datetime.today().strftime("%Y"),file_name),
            )
        return HttpResponse('%s/?_popup=1' % (f.id))
    else:
        raise Http404