-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
145 lines (130 loc) · 5.21 KB
/
models.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from django.contrib.sites.models import Site
from django.core.urlresolvers import reverse
from django.db import models
from django.http import HttpResponse
from django.utils import simplejson
from ap_wfm.templatetags.humanize_list import humanize_list
from cuddlybuddly.storage.s3.storage import S3Error
from sorl.thumbnail import ImageField, get_thumbnail
def json_response(func):
"""
https://coderwall.com/p/k8vb_a
A decorator that takes a view response and turns it
into json. If a callback is added through GET or POST
the response is JSONP.
"""
def decorator(request, *args, **kwargs):
objects = func(request, *args, **kwargs)
if isinstance(objects, HttpResponse):
return objects
try:
data = simplejson.dumps(objects)
if 'callback' in request.REQUEST:
# a jsonp response!
data = '%s(%s);' % (request.REQUEST['callback'], data)
return HttpResponse(data, "text/javascript")
except:
data = simplejson.dumps(str(objects))
return HttpResponse(data, "application/json")
return decorator
class Category(models.Model):
CATEGORY_CHOICES = (
(22, 'arts'),
(19, 'business'),
(13, 'federal'),
(27, 'entertainment'),
(21, 'health'),
(20, 'international'),
(25, 'oddities'),
(15, 'oregon'),
(23, 'politics'),
(14, 'region'),
(17, 'science'),
(24, 'sports'),
(18, 'technology'),
(26, 'top news'),
(1, 'national'),
(4, 'washington state'),
(12, 'weather'),
)
name = models.CharField(max_length=100, unique=True, choices=CATEGORY_CHOICES)
class Meta:
verbose_name_plural = 'Categories'
ordering = ['name']
def __unicode__(self):
return self.name
class APStory(models.Model):
category = models.ManyToManyField(Category)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(blank=True)
published = models.DateTimeField(blank=True)
management_id = models.CharField(max_length=150)
consumer_ready = models.BooleanField(default=True)
media_type = models.CharField(max_length=32)
priority_numeric = models.IntegerField(blank=True)
priority_legacy = models.CharField(max_length=5, blank=True)
subject_code = models.CharField(max_length=128, blank=True)
location = models.CharField(max_length=200, blank=True)
contributor = models.CharField(max_length=100, blank=True)
contributor_uri = models.CharField(max_length=125, blank=True)
byline = models.CharField(max_length=220, blank=True)
byline_title = models.CharField(max_length=150, blank=True)
slugline = models.CharField(max_length=300)
title = models.CharField(max_length=175)
keywords = models.CharField(max_length=180)
headline = models.CharField(max_length=260, blank=True)
slug = models.SlugField(max_length=300)
body = models.TextField(blank=True)
class Meta:
verbose_name_plural = 'stories'
ordering = ['-updated']
def __unicode__(self):
return '%s ID:%s' % (self.headline, self.id)
# http://django.readthedocs.io/en/1.3.X/ref/models/instances.html#the-permalink-decorator
@models.permalink
def get_absolute_url(self):
return ('ap_story_detail', [self.category.all()[0].name, self.slug])
def image_count(self):
return self.image_set.count()
def practical_update(self):
'''
Answers the question of whether the interval between published and
updated is greater than 120 seconds.
'''
time_diff = self.updated - self.published
if time_diff.seconds < 120:
return False
else:
return True
def categories(self):
return humanize_list([ item.name for item in self.category.all() ])
def to_json_dict(self):
return {
'headline': self.headline,
# 'link': self.slug,
'url': '%s%s' % (Site.objects.get_current().name, reverse('ap_story_detail', args=[self.category.filter(apstory__slug=self.slug)[0].name, self.slug]))
}
class Image(models.Model):
apstory = models.ForeignKey(APStory, null=True, on_delete=models.SET_NULL)
original_filename = models.CharField(max_length=125, default='AP photo')
image = ImageField(upload_to='ap/images', max_length=225)
alt_text = models.CharField(max_length=235, default='alt text')
caption = models.TextField(blank=True)
source = models.CharField(max_length=220, blank=True)
photo_type = models.CharField(max_length=50, help_text=u'Horizontal or vertical', blank=True) # only an attribute on full-size image
def __unicode__(self):
return self.original_filename
def to_json_image_dict(self):
try:
json_image = get_thumbnail(self.image, '990x990')
return {
'description': self.caption,
'byline': self.source,
'image': json_image.url
}
except (S3Error, IOError, TypeError):
return {
'description': self.caption,
'byline': self.source,
'image': ''
}