-
Notifications
You must be signed in to change notification settings - Fork 35
/
views.py
1019 lines (762 loc) · 35.3 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Main views of PSKB app
"""
from functools import wraps
import os
import re
from urlparse import urlparse
import requests
from flask import redirect, Response, url_for, session, request, render_template, flash, json, g
from . import PUBLISHED, IN_REVIEW, DRAFT, STATUSES
from . import app
from . import remote
from . import models
from . import forms
from . import tasks
from . import filters
from . import utils
SLACK_URL = u'https://hackguides.herokuapp.com'
def is_logged_in():
"""Determine if user is logged in or not"""
return 'github_token' in session and 'login' in session
def login_required(func):
"""
Decorator to require login and save URL for redirecting user after login
"""
@wraps(func)
def decorated_function(*args, **kwargs):
"""decorator args"""
if not is_logged_in():
# Save off the page so we can redirect them to what they were
# trying to view after logging in.
session['previously_requested_page'] = request.url
return redirect(url_for('login'))
return func(*args, **kwargs)
return decorated_function
def collaborator_required(func):
"""
Decorator to require login and logged in user to be collaborator
This should be used instead of @login_required when the URL endpoint should
be protected by login and the logged in user being a collaborator on the
repo. This will NOT redirect to login. It's meant to kick a user back to
the homepage if they tried something they do not have permissions for.
"""
@wraps(func)
def decorated_function(*args, **kwargs):
"""decorator args"""
if not is_logged_in():
flash('Must be logged in', category='error')
# Save off the page so we can redirect them to what they were
# trying to view after logging in.
session['previously_requested_page'] = request.url
return redirect(url_for('index'))
if 'collaborator' not in session or not session['collaborator']:
flash('Must be a repo collaborator for that functionality.',
category='error')
# Save off the page so we can redirect them to what they were
# trying to view after logging in.
session['previously_requested_page'] = request.url
return redirect(url_for('index'))
return func(*args, **kwargs)
return decorated_function
@app.route('/')
def index():
"""Homepage"""
# Send to login, application not fully setup yet.
if app.config['REPO_OWNER_ACCESS_TOKEN'] is None:
return redirect(url_for('login'))
return render_published_articles()
@app.route('/login/')
def login():
"""Login page"""
prev_url = session.get('previously_requested_page')
# See if user got here from write page and highlight that tab to indicate
# that they're trying to write and the click succeeded in getting them
# closer to writing; specific suggestion from Ed.
if prev_url is not None and '/write/' in prev_url:
g.write_active = True
return render_template('login.html')
@app.route('/gh_rate_limit')
def gh_rate_limit():
"""Debug request to view rate limit on Github"""
return repr(remote.check_rate_limit())
@app.route('/contributors/')
def contributors():
"""Contributors page"""
# The commit stats will have the avatar for all users because these stats
# come directly from github. However, the guide_stats are based on what
# metadata we have stored so the image url could be empty. So, fill in any
# missing images with the commit stats to avoid additional API calls.
commit_stats = models.contribution_stats()
guide_stats = models.author_stats(statuses=(PUBLISHED,))
for user, (count, image_url) in guide_stats.iteritems():
if image_url is None:
try:
avatar_url = commit_stats[user]['avatar_url']
except KeyError:
pass
else:
guide_stats[user] = [count, avatar_url]
# FIXME: Would be better to automatically ignore all collaborators on a
# repo but that requires 1 API request per user and we might want to count
# some collaborators and not others anyway.
# We could pass this ignore_users down but then we'd have to be mindful of
# which version was cached, etc. It's easier to do this trimming here
# because we can trim all stats independent of lower layers and caching
# even though this might not be as efficient. Ideally we won't be
# ignoring large amounts of users so shouldn't be a big issue.
ignore_users = []
for user in app.config.get('IGNORE_STATS_FOR', '').split(','):
ignore_users.append(user.strip())
return render_template('contributors.html',
commit_stats=commit_stats,
guide_stats=guide_stats,
ignore_users=ignore_users)
@app.route('/faq/')
def faq():
"""FAQ page"""
g.slack_url = SLACK_URL
# Read and cache this for an hour, the FAQ doesn't change very frequently
text = models.read_file('faq.md', rendered_text=True, use_cache=True,
timeout=60 * 60)
# Screen-scrape slack signup app since it's dynamic with node.js and grabs
# from slack API.
resp = requests.get(g.slack_url)
if resp.status_code == 200:
user_count = re.search(r'<p class="status">(.*?)</p>', resp.content)
if user_count is not None:
g.slack_stats = user_count.group(1)
return render_template('faq.html', text=text)
@app.route('/github_login')
def github_login():
"""Callback for github oauth"""
return remote.github.authorize(callback=url_for('authorized', _external=True))
@app.route('/logout')
@login_required
def logout():
"""Logout page"""
session.pop('github_token', None)
session.pop('login', None)
session.pop('name', None)
session.pop('collaborator', None)
session.pop('user_image', None)
return redirect(url_for('index'))
@app.route('/github/authorized')
def authorized():
"""URL for Github auth callback"""
resp = remote.github.authorized_response()
if resp is None:
flash('Access denied: reason=%s error=%s' % (
request.args['error'], request.args['error_description']),
category='error')
return redirect(url_for('index'))
session['github_token'] = (resp['access_token'], '')
session['collaborator'] = False
# Workaround for the first time you setup application
if app.config['REPO_OWNER_ACCESS_TOKEN'] is None:
app.config['REPO_OWNER_ACCESS_TOKEN'] = resp['access_token']
app.logger.critical('Please set your REPO_OWNER_ACCESS_TOKEN environment variable to: %s', resp['access_token'])
user = models.find_user()
if user is None:
flash('Unable to read user from Github API')
return redirect(url_for('index'))
if user.avatar_url:
session['user_image'] = user.avatar_url
if user.name:
session['name'] = user.name
if user.login:
session['login'] = user.login
if 'name' not in session:
session['name'] = user.login
session['collaborator'] = user.is_collaborator
url = session.pop('previously_requested_page', None)
if url is not None:
return redirect(url)
flash('Thanks for logging in. You can now browse guides <a href="%s"> in review</a> or <a href="%s">write new guides</a>.' % (url_for('in_review'), url_for('write')), category='info')
return redirect(url_for('user_profile', author_name=user.login))
@app.route('/user/<author_name>', methods=['GET'])
def old_profile(author_name):
return redirect(url_for('user_profile', author_name=author_name), 301)
# Note this URL is directly linked to the filters.url_for_user filter.
# These must be changed together!
@app.route('/author/<author_name>', methods=['GET'])
def user_profile(author_name):
"""Profile page"""
if author_name is None:
author_name = session.get('login', None)
# Must pass author name via URL if not logged in
if author_name is None:
return redirect(url_for('index'))
user = models.find_user(author_name)
if not user:
flash('Unable to find user "%s"' % (author_name), category='error')
return redirect(url_for('index'))
# Only owners can see their own drafts
if is_logged_in() and user.login == session['login']:
articles = models.get_articles_for_author(user.login)
else:
articles = models.get_public_articles_for_author(user.login)
return render_template('profile.html', user=user, articles=articles)
@app.route('/my-drafts/')
@login_required
def my_drafts():
"""Users drafts"""
g.drafts_active = True
articles = models.get_articles_for_author(session['login'],
status=DRAFT)
featured_article = find_featured_article()
return render_template('index.html', articles=articles,
featured_article=featured_article)
@app.route('/write/<path:article_path>/', methods=['GET'])
@app.route('/write/', defaults={'article_path': None})
@login_required
def write(article_path):
"""Editor page"""
article = None
selected_stack = None
if article_path is not None:
branch = request.args.get('branch', u'master')
article = models.read_article(article_path, rendered_text=False,
branch=branch)
if article is None:
flash('Failed reading guide', category='error')
return render_template('editor.html', article=article,
stacks=forms.STACK_OPTIONS,
selected_stack=selected_stack), 404
if article.sha is None:
article.sha = ''
# Only allowing a single stack choice now but the back-end article
# model can handle multiple.
if article.stacks:
selected_stack = article.stacks[0]
return render_template('editor.html', article=article,
stacks=forms.STACK_OPTIONS,
selected_stack=selected_stack)
@app.route('/partner/import/')
@login_required
def partner_import():
"""Special 'hidden' URL to import articles to secondary repo"""
article = None
branch_article = False
secondary_repo = True
flash('You are posting an guide to the partner repository!',
category='info')
return render_template('editor.html', article=article,
branch_article=branch_article,
secondary_repo=secondary_repo)
@app.route('/in-review/', methods=['GET'])
def in_review():
"""In review page"""
return render_article_list_view(IN_REVIEW)
@app.route('/review/<title>', methods=['GET'])
def review(title):
"""
This URL only exists for legacy reasons so try to find the article where
it is in the new scheme and return 301 to indicate moved.
"""
branch = request.args.get('branch', u'master')
article = models.search_for_article(title)
if article is not None:
return redirect(filters.url_for_article(article, branch=branch), 301)
return missing_article(request.base_url, title=title, branch=branch)
# Note this URL is directly linked to the filters.url_for_article filter.
# These must be changed together!
@app.route('/<stack>/<title>', methods=['GET'])
def article_view(stack, title):
"""
Find article with given stack/stack combination and display it
Note all publish statuses are searched and the first one found is returned.
This allows us to keep the same URL through the publish workflow process
since the status is only a 'hint' and query string.
By default, the statuses are searched in the order of importance:
published, in-review, and finally draft.
GET parameters used:
- status: Hint on what publish status to search for FIRST
- Default is 'published' which makes the published articles have
clean URLs without any query string.
- branch: Branch of article to display
- Default is master
"""
# Support for old URL /title/article.md
if title == 'article.md':
return redirect(url_for('review', title=stack))
# We don't allow any of these characters in stack or title so go ahead and
# reject this without doing any github API requests. This especially
# prevents issues when articles have bad image links in them that do not
# have a path, which would end up making requests to this URL. For example,
# <img src="test.png"/> on a /python/my-article page would try to find the
# image at /python/test.png.
if '.' in stack or '.' in title:
return not_found()
branch = request.args.get('branch', u'master')
# Search all status so an article's canonical URL can always stay the same
# regardless of the status, i.e we use the status argument as a hint on
# which file listing to use first but we always search the others until we
# find article.
status = request.args.get('status', PUBLISHED)
# draft articles are only visible by logged in users
if status == DRAFT and not is_logged_in():
session['previously_requested_page'] = request.url
return redirect(url_for('login'))
# Using a list here because we specifically want to check in this order but
# we don't want to check a single status more than once so don't want dups
# either.
statuses_to_check = [status]
for possible_status in STATUSES:
if possible_status not in statuses_to_check:
statuses_to_check.append(possible_status)
for status in statuses_to_check:
path = u'%s/%s/%s' % (status, stack, title)
# allow_missing is a workaround when we're looking for an article from
# old /review/ URL b/c we don't know what the status is we have to
# check them all. We don't want to log things as missing if we didn't
# know where they were and had to check all locations.
article = models.read_article(path, branch=branch, allow_missing=True)
if article is not None:
return render_article_view(request, article)
return missing_article(request.base_url, stack=stack, title=title,
branch=branch)
def render_article_list_view(status):
"""
Render list of articles with given status
:param status: PUBLISHED, IN_REVIEW, or DRAFT
"""
articles = models.get_available_articles(status=status)
return render_template('review.html', articles=articles,
stacks=forms.STACK_OPTIONS)
def render_article_view(request_obj, article, only_visible_by_user=None):
"""
Render article view
:param request_obj: Request object
:param article: Article object to render view for
:param branch: Branch of article to read
:param only_visible_by_user: Name of user that is allowed to view article
or None to allow anyone to read it
"""
g.review_active = True
login = session.get('login', None)
collaborator = session.get('collaborator', False)
recently_saved = request.args.get('saved', 0)
status = request.args.get('status', PUBLISHED)
publish_statuses = ()
if login == article.branch or article.author_name == login:
allow_delete = True
# Regular users cannot directly publish
publish_statuses = (IN_REVIEW, DRAFT)
else:
allow_delete = False
# Collaborators aka editors can use all statuses
if collaborator:
publish_statuses = STATUSES
# Use http as canonical protocol for url to avoid having two separate
# comment threads for an article. Disqus uses this variable to save
# comments.
canonical_url = request_obj.base_url.replace('https://', 'http://')
article_identifier = article.first_commit
redirect_url = None
if article_identifier is None:
# Backwards compatability for disqus comments. We didn't track the
# first commit before version .2 and all disqus comments used the
# slugified title for the unique id. Disqus doesn't allow for changing
# this so we're stuck with it if we want to maintain the comments
# before version .2.
article_identifier = utils.slugify(article.title)
# Hack to save our old social shares. The po.st service doesn't handle
# 301 redirects so need to share with the old url to keep the counts.
redirect_url = u'%s/review/%s' % (app.config['DOMAIN'],
article_identifier)
# Filter out the current branch from the list of branches
branches = [b for b in article.branches if b != article.branch]
# Always include a link to original article if this is a branched version
if article.branch != u'master':
branches.append(u'master')
g.header_white = True
user = models.find_user(article.author_name)
if only_visible_by_user is not None and only_visible_by_user != user.login:
return redirect(url_for('index'))
# Don't allow comments when we're testing b/c disqus will create a
# 'discussion' for every article and there's no way to delete them!
allow_comments = not app.debug
return render_template('article.html',
article=article,
allow_delete=allow_delete,
canonical_url=canonical_url,
article_identifier=article_identifier,
branches=branches,
collaborator=collaborator,
user=user,
publish_statuses=publish_statuses,
redirect_url=redirect_url,
allow_comments=allow_comments,
recently_saved=recently_saved,
status=status)
@app.route('/partner/<path:article_path>', methods=['GET'])
@app.route('/partner/', defaults={'article_path': None}, methods=['GET'])
def partner(article_path):
"""
URL for articles from hackhands blog -- these articles are not
editable.
"""
try:
repo_path = '%s/%s' % (app.config['SECONDARY_REPO_OWNER'],
app.config['SECONDARY_REPO_NAME'])
except KeyError:
flash('No secondary guide configuration', category='error')
return redirect(url_for('index'))
if article_path is None:
articles = models.get_available_articles(status=PUBLISHED,
repo_path=repo_path)
return render_template('review.html', articles=articles)
article = models.read_article(article_path, repo_path=repo_path)
if article is None:
flash('Failed reading guide', category='error')
return redirect(url_for('index'))
# Use http as canonical protocol for url to avoid having two separate
# comment threads for an article. Disqus uses this variable to save
# comments.
canonical_url = request.base_url.replace('https://', 'http://')
form = forms.SignupForm()
return render_template('article.html',
article=article,
allow_edits=False,
canonical_url=canonical_url,
form=form,
disclaimer=True)
@app.route('/api/save/', methods=['POST'])
@login_required
def api_save():
"""Api: POST /api/save {path:'', title: '', sha:'', original_stack: '', content: '', stacks: []}"""
g.slack_url = SLACK_URL
user = models.find_user()
if user is None:
redirect_to = url_for('index')
data = {'error': 'Cannot save unless logged in', 'redirect': redirect_to}
return Response(response=json.dumps(data), status=401, mimetype='application/json')
if user.email is None:
flash('Unable to read email address from Github API to properly attribute your commit to your account. Please make sure you have authorized the application to access your email.', category='warning')
# FIXME: stop using flash
content = request.form['content']
path = request.form['path']
title = request.form['title']
sha = request.form['sha']
orig_stack = request.form['original_stack']
if not content.strip() or not title.strip():
redirect_to = url_for('write')
data = {'error': 'Must enter title and body of guide', 'redirect': redirect_to}
return Response(response=json.dumps(data), status=400, mimetype='application/json')
# Form only accepts 1 stack right now but we can handle multiple on the
# back-end.
if not request.form['stacks']:
stacks = None
else:
stacks = request.form.getlist('stacks')
# FIXME: This is not the best solution. We're making this task
# synchronous but it's just a few git commands so hoping it will be
# quick. Also it only happens in the rare case where a stack is
# changed. We need to wait for the file move so we can maintain the
# history of the article through the move.
if path and orig_stack and stacks[0] != orig_stack:
new_path = models.change_article_stack(path, orig_stack, stacks[0],
title, user.login,
user.email)
if new_path is None:
flash('Failed changing guide stack', category='error')
# FIXME? return an error?
else:
path = new_path
new_article = False
if path:
message = 'Updates to "%s"' % (title)
else:
new_article = True
message = 'New guide, "%s"' % (title)
# Go ahead and make sure we don't have an article with the same stack
# and title. This would lead to duplicate URLs and we want to
# prevent users from ever creating a clash instead of detecting this
# change
article = models.search_for_article(title, stacks=stacks)
if article is not None:
if stacks is None:
msg = u'Please try choosing a stack. The title "%s" is already used by a guide.' % (title)
else:
msg = u'Please try choosing a different stack/title combination. The title "%s" is already used by a guide with the stack "%s".' % (title, ','.join(stacks))
redirect_to = url_for('write')
data = {'error': msg, 'redirect': redirect_to}
return Response(response=json.dumps(data), status=422, mimetype='application/json')
# Hidden option for admin to save articles to our other repo that's not
# editable
# TODO: move this to another endpoint
repo_path = None
if request.form.get('secondary_repo', None) is not None:
repo_path = '%s/%s' % (app.config['SECONDARY_REPO_OWNER'],
app.config['SECONDARY_REPO_NAME'])
article = models.branch_or_save_article(title, path, message, content,
user.login, user.email, sha,
user.avatar_url,
stacks=stacks,
repo_path=repo_path,
author_real_name=user.name)
if not article:
redirect_to = url_for('index')
data = {'error': 'Failed creating guide on github', 'redirect': redirect_to}
return Response(response=json.dumps(data), status=500, mimetype='application/json')
# TODO: move this to another endpoint
if repo_path is not None:
redirect_to = url_for('partner', article_path=article.path, branch=article.branch)
data = {'msg': 'Saved into admin repository', 'redirect': redirect_to}
if new_article:
return Response(response=json.dumps(data), status=201, mimetype='application/json')
else:
return Response(response=json.dumps(data), status=200, mimetype='application/json')
# We only have to worry about this on the master branch because we never
# actually use file listings on other branches.
if article.branch == u'master':
# Use these filter wrappers so we get absolute URL instead of relative
# URL to this specific site.
url = filters.url_for_article(article)
author_url = filters.url_for_user(article.author_name)
tasks.update_listing.delay(url,
article.title,
author_url,
article.author_real_name,
user.login,
user.email,
author_img_url=article.image_url,
thumbnail_url=article.thumbnail_url,
stacks=article.stacks,
branch=article.branch,
status=article.publish_status)
redirect_to = filters.url_for_article(article, branch=article.branch, saved=1)
if new_article:
data = {'msg': 'Guide created', 'redirect': redirect_to}
return Response(response=json.dumps(data), status=201, mimetype='application/json')
else:
data = {'msg': 'Guide updated', 'redirect': redirect_to}
return Response(response=json.dumps(data), status=200, mimetype='application/json')
@app.route('/delete/', methods=['POST'])
@login_required
def delete():
"""Delete POST page"""
user = models.find_user()
if user is None:
flash('Cannot delete unless logged in', category='error')
return render_published_articles(status_code=401)
path = request.form['path']
branch = request.form['branch']
article = models.read_article(path, rendered_text=False, branch=branch)
if article is None:
flash('Cannot find guide to delete', category='error')
return redirect(url_for('index'))
msg = u'Removing guide "%s"' % (article.title)
if not models.delete_article(article, msg, user.login, user.email):
flash('Failed removing guide', category='error')
else:
flash('Guide successfully deleted', category='info')
# This article should have only been on one of these lists but trying to
# remove it doesn't hurt so just forcefully remove it from both just in
# case.
for status in STATUSES:
tasks.remove_from_listing(article.title, status, user.login,
user.email, branch=article.branch)
return redirect(url_for('index'))
@app.route('/publish/', methods=['POST'])
@login_required
def change_publish_status():
"""Publish or unpublish article via POST"""
user = models.find_user()
if user is None:
flash('Cannot change publish status unless logged in', category='error')
return render_published_articles(status_code=401)
path = request.form['path']
branch = request.form['branch']
publish_status = request.form['publish_status']
if publish_status not in STATUSES:
flash('Invalid publish status, must be one of "%s"' % (STATUSES),
category='error')
return render_template('index.html')
if branch != u'master':
flash('Cannot change publish status on guides from branches other than master', category='error')
return redirect(url_for('index'))
article = models.read_article(path, rendered_text=False, branch=branch)
if article is None:
flash('Cannot find guide to change publish status', category='error')
return redirect(url_for('index'))
if article.publish_status == publish_status:
flash('Guide already in %s publish status' % (publish_status),
category='warning')
return redirect(filters.url_for_article(article))
if not user.is_collaborator:
if article.author_name != user.login:
flash('Only collaborators can change publish status on guides they do not start',
category='error')
return redirect(url_for('index'))
if publish_status == PUBLISHED:
flash('Only collaborators can publish guides')
return redirect(url_for('index'))
curr_path = article.path
app.logger.info(u'Requesting publish change for "%s" from "%s" to "%s"',
article.title, article.publish_status, publish_status)
article.publish_status = publish_status
author_url = filters.url_for_user(article.author_name)
# Create this link AFTER changing the status b/c the URL will have the
# status in it if the article is not published yet.
article_url = filters.url_for_article(article)
tasks.update_listing.delay(article_url,
article.title,
author_url,
article.author_real_name,
user.login,
user.email,
author_img_url=article.image_url,
thumbnail_url=article.thumbnail_url,
stacks=article.stacks,
branch=article.branch,
status=article.publish_status)
tasks.move_article.delay(curr_path, article.path, article.title,
user.login, user.email,
new_publish_status=article.publish_status)
return redirect(filters.url_for_article(article, saved=1))
@app.route('/subscribe/', methods=['POST'])
def subscribe():
"""Subscribe POST page"""
form = forms.SignupForm()
# Note this helper automatically grabs request.form
if form.validate_on_submit():
app.logger.debug('Adding new subscriber: %s - %s' % (form.email.data,
form.stacks.data))
sub_id = models.add_subscriber(form.email.data, form.stacks.data)
if not sub_id:
flash('Failed adding to list', category='error')
else:
flash('Thanks for subscribing!', category='info')
return redirect(request.referrer)
else:
for input_name, errors in form.errors.iteritems():
for error in errors:
flash('%s - %s' % (input_name, error), category='error')
return redirect(request.referrer)
@app.route('/img_upload/', methods=['POST'])
@login_required
def img_upload():
"""Image upload POST page"""
user = models.find_user()
if user is None:
app.logger.error('Cannot upload image unless logged in')
return Response(response='', status=500, mimetype='application/json')
file_ = request.files['file']
try:
ext = file_.filename.split(os.extsep)[1]
except IndexError:
ext = ''
# Always save images to master branch because image uploads might happen
# before the article is saved so don't know the article name or branch to
# save alongside.
url = models.save_image(file_.stream, ext, 'Saving new guide image',
user.login, user.email, branch=u'master')
if url is None:
app.logger.error('Failed uploading image')
return Response(response='', status=500, mimetype='application/json')
return Response(response=json.dumps(url), status=200,
mimetype='application/json')
@app.route('/sync_listing/<publish_status>')
@collaborator_required
def sync_listing(publish_status):
"""Sync listing page"""
user = models.find_user()
if user is None:
app.logger.error('Cannot sync listing unless logged in')
return render_template('index.html'), 500
if publish_status not in STATUSES:
flash('Invalid publish status, must be one of "%s"' % (u','.join(STATUSES)),
category='error')
return render_template('index.html')
tasks.synchronize_listing.delay(publish_status, user.login, user.email)
flash('Queued up %s sync' % (publish_status), category='info')
return redirect(url_for('index'))
@app.context_processor
def template_globals():
"""Global variables available to all responses"""
return {'repo_url': remote.default_repo_url(),
'form': forms.SignupForm(), 'stack_options': forms.STACK_OPTIONS}
@app.errorhandler(500)
def internal_error(error=None):
"""Unknown error page"""
return render_template('error.html'), 500
@app.errorhandler(404)
def not_found(error=None):
"""Not found error page"""
return render_template('error.html'), 404
def find_featured_article(articles=None):
"""
Find featured article in list of articles or published articles
:params articles: List of article objects to search for featured article or
use published articles if no list is given
:returns: Article object of featured article or None if not found
"""
featured = os.environ.get('FEATURED_TITLE')
if featured is None:
return None
if articles is None:
# FIXME: This should only fetch the most recent x number.
articles = list(models.get_available_articles(status=PUBLISHED))
for ii, article in enumerate(articles):
if article.title == featured:
return article
return None
def render_published_articles(status_code=200):
"""
Render published article listing and featured article
This is extracted into a stand-alone function so we can render this in
multiple locations without redirects which could hurt SEO and usability.
"""
# FIXME: This should only fetch the most recent x number.
articles = list(models.get_available_articles(status=PUBLISHED))
featured_article = find_featured_article(articles)
if featured_article:
articles.remove(featured_article)
return render_template('index.html', articles=articles,
featured_article=featured_article), status_code
def missing_article(requested_url=None, stack=None, title=None, branch=None):
"""
Handle missing articles by checking if URL is should be 301 redirect or
showing published articles in the URL is truly bad
"""
# See if this URL is setup as a redirect due to an title change, etc.
if requested_url is not None:
new_url = lookup_url_redirect(requested_url)
if new_url is not None:
return redirect(new_url, code=301)
app.logger.error(
'Failed finding guide - stack: "%s", title: "%s", branch: "%s"',
stack, title, branch)
flash('We could not find that guide. Give these fresh ones a try.')
return render_published_articles(status_code=404)
def lookup_url_redirect(requested_url):
"""
Lookup given URL for a 301 redirect
:param requested_url: URL to look for a redirect
:returns: URL to redirect to or None if no redirect found
"""
new_url = None
redirects = models.read_redirects()
# All our URLs should be ASCII!
try:
old_url = str(requested_url)
except UnicodeEncodeError:
return None