-
Notifications
You must be signed in to change notification settings - Fork 0
/
entries.py
151 lines (113 loc) · 4.18 KB
/
entries.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
146
147
148
149
150
151
import os
import json
import webapp2
import json
import urllib
import feedparser
import utils
from google.appengine.ext.webapp import util
from google.appengine.api import users
from model import InputFeed
from model import Entry
from model import User
from model import Tag
import logging
class SpecificEntryHandler(webapp2.RequestHandler):
def post(self, user_id, feed_id, entry_id):
"""Updates a specific entry"""
current_user = utils.get_current_user()
if not current_user:
self.error(403)
return
user = User.get_by_id(int(user_id))
if user == None:
self.error(404)
return
if current_user.key() != user.key():
self.error(401)
return
feed = InputFeed.get_by_id(int(feed_id), parent=user)
if feed == None:
self.error(404)
return
entry = Entry.get_by_id(int(entry_id), parent=feed)
published = self.request.get('published')
if published != None:
entry.published = bool(int(published))
entry.save()
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.out.write(json.dumps(entry.to_struct()))
class TagEntryHandler(webapp2.RequestHandler):
def post(self, feed_id, entry_id, tag_title):
""" Create or modify tag """
current_user = utils.get_current_user()
if not current_user:
logging.info('no user logged in')
self.error(403)
return
logging.info(feed_id)
feed = InputFeed.get_by_id(int(feed_id), parent=current_user)
if not feed:
logging.info('no feed found')
self.error(404)
return
entry = Entry.get_by_id(int(entry_id), parent=feed)
if not entry:
logging.info('no entry found')
self.error(404)
return
if not tag_title:
logging.info('no tag_title provided found')
self.error(400)
return
tag_title = tag_title.decode('utf-8')
tag = Tag.all().ancestor(current_user).filter('title_lower =', tag_title.lower()).get()
if not tag:
logging.info('tag not found, creating new')
tag = Tag(
parent=current_user,
title=tag_title,
title_lower=tag_title.lower(),
)
tag.tag_entry(entry)
self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.out.write('ok')
def delete(self, feed_id, entry_id, tag_title):
""" Delete tag """
current_user = utils.get_current_user()
tag_title = tag_title.decode('utf-8')
if not current_user:
logging.info('no user logged in')
self.error(403)
return
logging.info(feed_id)
feed = InputFeed.get_by_id(int(feed_id), parent=current_user)
if not feed:
logging.info('no feed found')
self.error(404)
return
entry = Entry.get_by_id(int(entry_id), parent=feed)
if not entry:
logging.info('no entry found')
self.error(404)
return
if not tag_title:
logging.info('no tag_title provided found')
self.error(400)
return
tag = Tag.all().ancestor(current_user).filter('title_lower =', tag_title.lower()).get()
if not tag:
logging.info('no tag found')
self.error(404)
return
entry.tags.remove(tag.key())
entry.save()
self.response.headers['Content-Type'] = 'text/plain; charset=utf-8'
self.response.headers['Access-Control-Allow-Origin'] = '*'
self.response.out.write('ok')
app = webapp2.WSGIApplication([
('/(.*)/feeds/(.*)/entries/(.*)', SpecificEntryHandler),
('/feeds/(.*)/entries/(.*)/tags/(.*)', TagEntryHandler),
], debug=True)