-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.py
114 lines (92 loc) · 3.54 KB
/
views.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse
from PIL import Image
from aesthetic_computation.models import Post, Category
# custom error handlers
def handler404(request, *args, **argv):
return render(request, 'ac/404.html', {})
def handler500(request, *args, **argv):
return render(request, 'ac/500.html', {})
# home page (and category listing page)
def home(request, category_name=None):
# get posts
posts = Post.objects.all()
# filter to only posts in a specified category
if category_name is not None:
try:
cat = Category.objects.get(name=category_name)
posts = cat.post_set.all()
except:
# category does not exist, redirect to home
return HttpResponseRedirect(reverse('home'))
context = {'posts':posts[::-1]}
return render(request, 'ac/index.html', context)
# single post page
def post(request, id):
# get post
try:
post = Post.objects.get(id=id)
cats = post.categories.all()
except:
# post does not exist, redirect to home
return HttpResponseRedirect(reverse('home'))
# get previous and next posts for arrow links
try:
prev_post_id = Post.objects.get(id=int(id)-1).id
except:
prev_post_id = None
try:
next_post_id = Post.objects.get(id=int(id)+1).id
except:
next_post_id = None
# check if image is sufficiently large that we can allow a big view
wide_entry = ''
try:
with Image.open(post.image_large) as img:
width, height = img.size
if width >= 1400:
wide_entry = ' wide-entry'
if width >= 1700:
wide_entry += ' wide-entry-xl'
if width >= 2300:
wide_entry += ' wide-entry-xxl'
except:
pass
# render
context = {'post':post, 'cats':cats, 'wide_entry':wide_entry, 'width':width,
'prev_post_id':prev_post_id, 'next_post_id':next_post_id}
return render(request, 'ac/post.html', context)
# arxiv functionality
def arxiv(request, date=None):
from .settings import MEDIA_ROOT, MEDIA_URL
import os
import glob
baseurl = MEDIA_URL.replace("projects/","arxiv/")
# get list of directories (daily image scrape)
dirs = glob.glob(MEDIA_ROOT + "../arxiv/*")
dirs.sort(key = os.path.getmtime)
dirs = [dir.rsplit("/")[-1] for dir in dirs]
# get list of directories (ITA gallery)
dirs2 = glob.glob(MEDIA_ROOT + "../arxivpubs/*")
dirs2.sort(key = os.path.getmtime)
dirs2 = [dir.rsplit("/")[-1] for dir in dirs2 if '.txt' not in dir]
# if a date specified, get list of all filenames too
images = []
if date is not None and date in dirs + dirs2:
images = glob.glob(MEDIA_ROOT + "../arxiv/%s/*" % date)
if len(images) == 0:
# hack to detect arxivpubs/ request
images = glob.glob(MEDIA_ROOT + "../arxivpubs/%s/*" % date)
baseurl = MEDIA_URL.replace("projects/","arxivpubs/")
images = [image.rsplit("/")[-1] for image in images]
images.sort()
# list of (image filename, arxiv ID) tuples
images = [[image,image.split("_")[0]] for image in images]
context = {'dirs':dirs, 'date':date, 'images':images, 'baseurl':baseurl, # daily image scrape
'dirs2':dirs2} # ITA gallery
return render(request, 'ac/arxiv.html', context)
# aas job map
def aasjobmap(request):
context = {}
return render(request, 'ac/aasjobmap.html', context)