Show More
@@ -1,67 +1,69 | |||
|
1 | 1 | from django.conf.urls import patterns, url, include |
|
2 | 2 | from boards import views |
|
3 | 3 | from boards.rss import AllThreadsFeed, TagThreadsFeed, ThreadPostsFeed |
|
4 | 4 | from boards.views import api |
|
5 | 5 | |
|
6 | 6 | js_info_dict = { |
|
7 | 7 | 'packages': ('boards',), |
|
8 | 8 | } |
|
9 | 9 | |
|
10 | 10 | urlpatterns = patterns('', |
|
11 | 11 | |
|
12 | 12 | # /boards/ |
|
13 | 13 | url(r'^$', views.index, name='index'), |
|
14 | 14 | # /boards/page/ |
|
15 | 15 | url(r'^page/(?P<page>\w+)/$', views.index, name='index'), |
|
16 | 16 | |
|
17 | 17 | url(r'^archive/$', views.archive, name='archive'), |
|
18 | 18 | url(r'^archive/page/(?P<page>\w+)/$', views.archive, name='archive'), |
|
19 | 19 | |
|
20 | 20 | # login page |
|
21 | 21 | url(r'^login/$', views.login, name='login'), |
|
22 | 22 | |
|
23 | 23 | # /boards/tag/tag_name/ |
|
24 | 24 | url(r'^tag/(?P<tag_name>\w+)/$', views.tag, name='tag'), |
|
25 | 25 | # /boards/tag/tag_id/page/ |
|
26 | 26 | url(r'^tag/(?P<tag_name>\w+)/page/(?P<page>\w+)/$', views.tag, name='tag'), |
|
27 | 27 | |
|
28 | 28 | # /boards/tag/tag_name/unsubscribe/ |
|
29 | 29 | url(r'^tag/(?P<tag_name>\w+)/subscribe/$', views.tag_subscribe, |
|
30 | 30 | name='tag_subscribe'), |
|
31 | 31 | # /boards/tag/tag_name/unsubscribe/ |
|
32 | 32 | url(r'^tag/(?P<tag_name>\w+)/unsubscribe/$', views.tag_unsubscribe, |
|
33 | 33 | name='tag_unsubscribe'), |
|
34 | 34 | |
|
35 | 35 | # /boards/thread/ |
|
36 | 36 | url(r'^thread/(?P<post_id>\w+)/$', views.thread, name='thread'), |
|
37 | 37 | url(r'^thread/(?P<post_id>\w+)/(?P<mode>\w+)/$', views.thread, name='thread_mode'), |
|
38 | 38 | url(r'^settings/$', views.settings, name='settings'), |
|
39 | 39 | url(r'^tags/$', views.all_tags, name='tags'), |
|
40 | 40 | url(r'^captcha/', include('captcha.urls')), |
|
41 | 41 | url(r'^jump/(?P<post_id>\w+)/$', views.jump_to_post, name='jumper'), |
|
42 | 42 | url(r'^authors/$', views.authors, name='authors'), |
|
43 | 43 | url(r'^delete/(?P<post_id>\w+)/$', views.delete, name='delete'), |
|
44 | 44 | url(r'^ban/(?P<post_id>\w+)/$', views.ban, name='ban'), |
|
45 | 45 | |
|
46 | 46 | url(r'^banned/$', views.you_are_banned, name='banned'), |
|
47 | 47 | url(r'^staticpage/(?P<name>\w+)/$', views.static_page, name='staticpage'), |
|
48 | 48 | |
|
49 | 49 | # RSS feeds |
|
50 | 50 | url(r'^rss/$', AllThreadsFeed()), |
|
51 | 51 | url(r'^page/(?P<page>\w+)/rss/$', AllThreadsFeed()), |
|
52 | 52 | url(r'^tag/(?P<tag_name>\w+)/rss/$', TagThreadsFeed()), |
|
53 | 53 | url(r'^tag/(?P<tag_name>\w+)/page/(?P<page>\w+)/rss/$', TagThreadsFeed()), |
|
54 | 54 | url(r'^thread/(?P<post_id>\w+)/rss/$', ThreadPostsFeed()), |
|
55 | 55 | |
|
56 | 56 | # i18n |
|
57 | 57 | url(r'^jsi18n/$', 'boards.views.cached_js_catalog', js_info_dict, name='js_info_dict'), |
|
58 | 58 | |
|
59 | 59 | # API |
|
60 | 60 | url(r'^api/post/(?P<post_id>\w+)/$', api.get_post, name="get_post"), |
|
61 | 61 | url(r'^api/diff_thread/(?P<thread_id>\w+)/(?P<last_update_time>\w+)/$', |
|
62 | 62 | api.api_get_threaddiff, name="get_thread_diff"), |
|
63 | 63 | url(r'^api/threads/(?P<count>\w+)/$', api.api_get_threads, |
|
64 | 64 | name='get_threads'), |
|
65 | 65 | url(r'api/tags/$', api.api_get_tags, name='get_tags'), |
|
66 | url(r'api/thread/(?P<opening_post_id>\w+)/$', api.api_get_thread_posts, | |
|
67 | name='get_thread'), | |
|
66 | 68 | |
|
67 | 69 | ) |
@@ -1,157 +1,189 | |||
|
1 | 1 | from datetime import datetime |
|
2 | 2 | import json |
|
3 | 3 | from django.db import transaction |
|
4 | 4 | from django.http import HttpResponse |
|
5 | 5 | from django.shortcuts import get_object_or_404, render |
|
6 | 6 | from django.template import RequestContext |
|
7 | 7 | from django.utils import timezone |
|
8 | 8 | from boards.forms import ThreadForm, PlainErrorList |
|
9 | 9 | from boards.models import Post, Thread, Tag |
|
10 | 10 | from boards.views import _datetime_to_epoch, _new_post, \ |
|
11 | 11 | _ban_current_user |
|
12 | 12 | |
|
13 | 13 | __author__ = 'neko259' |
|
14 | 14 | |
|
15 | 15 | PARAMETER_TRUNCATED = 'truncated' |
|
16 | 16 | PARAMETER_TAG = 'tag' |
|
17 | 17 | PARAMETER_OFFSET = 'offset' |
|
18 | 18 | |
|
19 | 19 | |
|
20 | 20 | @transaction.atomic |
|
21 | 21 | def api_get_threaddiff(request, thread_id, last_update_time): |
|
22 | 22 | """Get posts that were changed or added since time""" |
|
23 | 23 | |
|
24 | 24 | thread = get_object_or_404(Post, id=thread_id).thread_new |
|
25 | 25 | |
|
26 | 26 | filter_time = datetime.fromtimestamp(float(last_update_time) / 1000000, |
|
27 | 27 | timezone.get_current_timezone()) |
|
28 | 28 | |
|
29 | 29 | json_data = { |
|
30 | 30 | 'added': [], |
|
31 | 31 | 'updated': [], |
|
32 | 32 | 'last_update': None, |
|
33 | 33 | } |
|
34 | 34 | added_posts = Post.objects.filter(thread_new=thread, |
|
35 | 35 | pub_time__gt=filter_time) \ |
|
36 | 36 | .order_by('pub_time') |
|
37 | 37 | updated_posts = Post.objects.filter(thread_new=thread, |
|
38 | 38 | pub_time__lte=filter_time, |
|
39 | 39 | last_edit_time__gt=filter_time) |
|
40 | 40 | for post in added_posts: |
|
41 | 41 | json_data['added'].append(get_post(request, post.id).content.strip()) |
|
42 | 42 | for post in updated_posts: |
|
43 | 43 | json_data['updated'].append(get_post(request, post.id).content.strip()) |
|
44 | 44 | json_data['last_update'] = _datetime_to_epoch(thread.last_edit_time) |
|
45 | 45 | |
|
46 | 46 | return HttpResponse(content=json.dumps(json_data)) |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | # TODO This method needs to be implemented properly |
|
50 | 50 | # def api_add_post(request, form): |
|
51 | 51 | # """ |
|
52 | 52 | # Add a post and return the JSON response for it |
|
53 | 53 | # """ |
|
54 | 54 | # |
|
55 | 55 | # status = 'ok' |
|
56 | 56 | # errors = [] |
|
57 | 57 | # |
|
58 | 58 | # if request.method == 'POST': |
|
59 | 59 | # form = ThreadForm(request.POST, request.FILES, |
|
60 | 60 | # error_class=PlainErrorList) |
|
61 | 61 | # form.session = request.session |
|
62 | 62 | # |
|
63 | 63 | # if form.is_valid(): |
|
64 | 64 | # # TODO Don't form a response here cause we'll not need it |
|
65 | 65 | # _new_post(request, form) |
|
66 | 66 | # if form.need_to_ban: |
|
67 | 67 | # # Ban user because he is suspected to be a bot |
|
68 | 68 | # _ban_current_user(request) |
|
69 | 69 | # else: |
|
70 | 70 | # status = 'error' |
|
71 | 71 | # for field in form.fields: |
|
72 | 72 | # if field.errors: |
|
73 | 73 | # errors.append(field.errors) |
|
74 | 74 | # |
|
75 | 75 | # response = { |
|
76 | 76 | # 'status': status, |
|
77 | 77 | # 'errors': errors, |
|
78 | 78 | # } |
|
79 | 79 | # |
|
80 | 80 | # return HttpResponse(content=json.dumps(response)) |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | def get_post(request, post_id): |
|
84 | 84 | """ |
|
85 | 85 | Get the html of a post. Used for popups. Post can be truncated if used |
|
86 | 86 | in threads list with 'truncated' get parameter. |
|
87 | 87 | """ |
|
88 | 88 | |
|
89 | 89 | post = get_object_or_404(Post, id=post_id) |
|
90 | 90 | thread = post.thread_new |
|
91 | 91 | |
|
92 | 92 | context = RequestContext(request) |
|
93 | 93 | context['post'] = post |
|
94 | 94 | context['can_bump'] = thread.can_bump() |
|
95 | 95 | if PARAMETER_TRUNCATED in request.GET: |
|
96 | 96 | context[PARAMETER_TRUNCATED] = True |
|
97 | 97 | |
|
98 | 98 | return render(request, 'boards/post.html', context) |
|
99 | 99 | |
|
100 | 100 | |
|
101 | # TODO Test this | |
|
101 | 102 | def api_get_threads(request, count): |
|
102 | 103 | """ |
|
103 | 104 | Get the JSON thread opening posts list. |
|
104 | 105 | Parameters that can be used for filtering: |
|
105 | 106 | tag, offset (from which thread to get results) |
|
106 | 107 | """ |
|
107 | 108 | |
|
108 | 109 | if PARAMETER_TAG in request.GET: |
|
109 | 110 | tag_name = request.GET[PARAMETER_TAG] |
|
110 | 111 | if tag_name is not None: |
|
111 | 112 | tag = get_object_or_404(Tag, name=tag_name) |
|
112 | 113 | threads = tag.threads.filter(archived=False) |
|
113 | 114 | else: |
|
114 | 115 | threads = Thread.objects.filter(archived=False) |
|
115 | 116 | |
|
116 | 117 | if PARAMETER_OFFSET in request.GET: |
|
117 | 118 | offset = request.GET[PARAMETER_OFFSET] |
|
118 | 119 | offset = int(offset) if offset is not None else 0 |
|
119 | 120 | else: |
|
120 | 121 | offset = 0 |
|
121 | 122 | |
|
122 | 123 | threads = threads.order_by('-bump_time') |
|
123 | 124 | threads = threads[offset:offset + int(count)] |
|
124 | 125 | |
|
125 | 126 | opening_posts = [] |
|
126 | 127 | for thread in threads: |
|
127 | 128 | opening_post = thread.get_opening_post() |
|
128 | 129 | |
|
129 | 130 | # TODO Add pub time, tags, replies and images count |
|
130 | 131 | post_json = { |
|
131 | 132 | 'id' : opening_post.id, |
|
132 | 133 | 'title' : opening_post.title, |
|
133 | 134 | 'text' : opening_post.text.rendered, |
|
134 | 135 | } |
|
135 | 136 | if opening_post.image: |
|
136 | 137 | post_json += { |
|
137 | 138 | 'image' : opening_post.image.url, |
|
138 | 139 | 'image_preview' : opening_post.image.url_200x150, |
|
139 | 140 | } |
|
140 | 141 | opening_posts.append(post_json) |
|
141 | 142 | |
|
142 | 143 | return HttpResponse(content=json.dumps(opening_posts)) |
|
143 | 144 | |
|
144 | 145 | |
|
146 | # TODO Test this | |
|
145 | 147 | def api_get_tags(request): |
|
146 | 148 | """ |
|
147 | 149 | Get all tags or user tags. |
|
148 | 150 | """ |
|
149 | 151 | |
|
150 | 152 | # TODO Get favorite tags for the given user ID |
|
151 | 153 | |
|
152 | 154 | tags = Tag.objects.get_not_empty_tags() |
|
153 | 155 | tag_names = [] |
|
154 | 156 | for tag in tags: |
|
155 | 157 | tag_names.append(tag.name) |
|
156 | 158 | |
|
157 | 159 | return HttpResponse(content=json.dumps(tag_names)) |
|
160 | ||
|
161 | ||
|
162 | # TODO The result can be cached by the thread last update time | |
|
163 | # TODO Test this | |
|
164 | def api_get_thread_posts(request, opening_post_id): | |
|
165 | """ | |
|
166 | Get the JSON array of thread posts | |
|
167 | """ | |
|
168 | ||
|
169 | opening_post = get_object_or_404(Post, id=opening_post_id) | |
|
170 | thread = opening_post.thread_new | |
|
171 | posts = thread.get_replies() | |
|
172 | ||
|
173 | json_post_list = [] | |
|
174 | ||
|
175 | for post in posts: | |
|
176 | # TODO Add pub time and replies | |
|
177 | post_json = { | |
|
178 | 'id' : post.id, | |
|
179 | 'title' : post.title, | |
|
180 | 'text' : post.text.rendered, | |
|
181 | } | |
|
182 | if post.image: | |
|
183 | post_json += { | |
|
184 | 'image' : post.image.url, | |
|
185 | 'image_preview' : post.image.url_200x150, | |
|
186 | } | |
|
187 | json_post_list.append(post_json) | |
|
188 | ||
|
189 | return HttpResponse(content=json.dumps(json_post_list)) |
General Comments 0
You need to be logged in to leave comments.
Login now