-
Notifications
You must be signed in to change notification settings - Fork 35
/
article.py
1048 lines (802 loc) · 37.2 KB
/
article.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
"""
Article related model API
"""
import collections
import itertools
import json
import subprocess
from . import lib
from . import file as file_mod
from .user import find_user
from .. import app
from .. import PUBLISHED, IN_REVIEW, DRAFT, STATUSES
from .. import cache
from .. import remote
from .. import utils
# FIXME: This file is fairly modular to the outside world but internally it's
# very fragmented and the layers of abstraction are all mixed up. Needs a lot
# of TLC...
FILE_EXTENSION = '.md'
ARTICLE_FILENAME = 'article%s' % (FILE_EXTENSION)
ARTICLE_METADATA_FILENAME = 'details.json'
path_details = collections.namedtuple('path_details', 'repo, filename')
def get_available_articles(status=None, repo_path=None):
"""
Get iterator for current article objects
:param status: PUBLISHED, IN_REVIEW, DRAFT, or None to read all articles
:param repo_path: Optional repo path to read from (<owner>/<name>)
:returns: Iterator through article objects
Note that article objects only have path, title, author name, and stacks
filled out. You'll need to call read_article() to get full article
details.
"""
if status is None or repo_path is not None:
for article in get_available_articles_from_api(status=status,
repo_path=repo_path):
yield article
raise StopIteration
# Shortcuts to read listing of files from a single file read instead of
# using the API to read every file
if status == PUBLISHED:
items = file_mod.published_articles()
elif status == IN_REVIEW:
items = file_mod.in_review_articles()
elif status == DRAFT:
items = file_mod.draft_articles()
for item in items:
article = Article(item.title, item.author_name,
author_real_name=item.author_real_name,
stacks=item.stacks)
article.publish_status = status
if item.thumbnail_url:
article.thumbnail_url = item.thumbnail_url
if item.author_img_url:
article.image_url = item.author_img_url
yield article
def search_for_article(title, stacks=None, status=None):
"""
Search for an article by the title and optionally stack and status
:param title: Title of article to search for
:param stacks: Optional list of stacks to search All stacks are searched if
None is given
:param status: Optional status to search for All possible statuses are
searched if None is given
:returns: Article object if found or None if not found
"""
statuses = [status] if status is not None else STATUSES
if stacks is None:
stacks = []
else:
# Normalize so we don't have to deal with case issues
stacks = [s.lower() for s in stacks]
for status in statuses:
articles = get_available_articles(status=status)
article = find_article_by_title(articles, title)
if article is None:
continue
if not stacks:
return article
for requested_stack in stacks:
for article_stack in article.stacks:
if article_stack.lower() == requested_stack:
return article
return None
def get_available_articles_from_api(status=None, repo_path=None):
"""
Get iterator for current article objects
:param status: PUBLISHED, IN_REVIEW, DRAFT, or None to read all articles
:param repo_path: Optional repo path to read from (<owner>/<name>)
:returns: Iterator through article objects
Note that article objects only have path, title and author name filled out.
You'll need to call read_article() to get full article details.
"""
# Go through the minimal listing of articles and turn it into the full
# article objects. This way the github layer only knows what's available
# on github and doesn't have knowledge of how we organize things, etc.
if repo_path is None:
repo_path = remote.default_repo_path()
articles = cache.read_file_listing(status)
if articles is not None:
for article in articles_from_json(articles):
yield article
raise StopIteration
files_to_cache = []
for file_details in remote.files_from_github(repo_path, ARTICLE_FILENAME):
# We're only caching published articles right now so don't waste a
# roundtrip to cache if that's not what caller wants.
article = None
if status == PUBLISHED:
article = _read_article_from_cache(file_details.path)
if article is None:
article = read_article_from_metadata(file_details)
if article is None:
continue
article.filename = ARTICLE_FILENAME
article.repo_path = repo_path
if status is None or article.publish_status == status:
yield article
if status == PUBLISHED and article.publish_status == PUBLISHED:
files_to_cache.append(lib.to_json(article))
if files_to_cache:
cache.save_file_listing('published', json.dumps(files_to_cache))
def articles_from_json(json_str):
"""
Generator to iterate through list of article objects in json format
:param json_str: JSON string
:returns: Generator through article objects
"""
for json_str in json.loads(json_str):
try:
yield Article.from_json(json_str)
except ValueError:
app.logger.error('Failed parsing json meta data from cache "%s"',
json_str)
continue
raise StopIteration
def get_articles_for_author(author_name, status=None):
"""
Get iterator for articles from given author
:param author_name: Name of author to find articles for
:param status: PUBLISHED, IN_REVIEW, DRAFT, or None to read all articles
:returns: Iterator through article objects
"""
if status is None:
articles = itertools.chain(get_available_articles(status=PUBLISHED),
get_available_articles(status=IN_REVIEW),
get_available_articles(status=DRAFT))
else:
articles = get_available_articles(status=status)
for article in articles:
if article.author_name == author_name:
yield article
def get_public_articles_for_author(author_name):
"""
Get iterator for all public i.e. non-draft articles from given author
:param author_name: Name of author to find articles for
:returns: Iterator through article objects
"""
articles = itertools.chain(get_available_articles(status=PUBLISHED),
get_available_articles(status=IN_REVIEW))
for article in articles:
if article.author_name == author_name:
yield article
def author_stats(statuses=None):
"""
Get number of articles for each author
:param statuses: List of statuses to aggregate stats for
:param statuses: Optional status to aggregate stats for, all possible
statuses are counted if None is given
:returns: Dictionary mapping author names to number of articles::
{author_name: [article_count, avatar_url]}
Note avatar_url can be None and is considered optional
"""
cache_key = 'author-stats'
stats = cache.get(cache_key)
if stats:
return json.loads(stats)
stats = {}
statuses = [get_available_articles(status=st) for st in statuses]
for article in itertools.chain(*statuses):
# This is ALMOST a good fit for collections.defaultdict() but we need
# to inspect the avatar URL each time to see if it can be replaced with
# a non-empty value since this is optional article information.
try:
prev_stats = stats[article.author_name]
except KeyError:
prev_stats = [1, None]
else:
prev_stats[0] += 1
if prev_stats[1] is None and article.image_url is not None:
prev_stats[1] = article.image_url
stats[article.author_name] = prev_stats
if not stats:
return stats
# Just fetch stats every 30 minutes, this is not a critical bit of data
cache.save(cache_key, json.dumps(stats), timeout=30 * 60)
return stats
def read_article(path, rendered_text=True, branch=u'master', repo_path=None,
allow_missing=False):
"""
Read article
:param path: Short path to article, not including repo or owner
:param rendered_text: Boolean to read rendered or raw text
:param branch: Name of branch to read file from
:param repo_path: Optional repo path to read from (<owner>/<name>)
:param allow_missing: False to log warning for missing or True to allow it
i.e. when you're just seeing if an article exists
:returns: Article object
"""
if repo_path is None:
repo_path = remote.default_repo_path()
full_path = '%s/%s' % (repo_path, path)
# Handle scenario where caller forgets to include filename, default it
if not path.endswith(FILE_EXTENSION):
slash = '' if path.endswith('/') else '/'
full_path = '%s%s%s' % (full_path, slash, ARTICLE_FILENAME)
# Only caching rendered text of articles since that's the 'front-end' of
# the site.
if rendered_text:
article = _read_article_from_cache(path, branch)
if article is not None:
return article
details = remote.read_file_from_github(full_path, branch, rendered_text,
allow_404=allow_missing)
# Allow empty sha when requesting rendered_text b/c of the way the
# underlying remote API works. See read_file_from_github for more
# information.
if details is None or details.text is None or (details.sha is None and not rendered_text):
if not allow_missing:
app.logger.error('Failed reading path: "%s" branch: %s', full_path,
branch)
return None
# Parse path to get article information but replace it with improved json
# meta-data if available.
path_info = parse_full_path(full_path)
json_str = read_meta_data_for_article_path(full_path)
if json_str is not None:
article = Article.from_json(json_str)
# Update it with what we pull from the article file and path
article.content = details.text
article.sha = details.sha
article.external_url = details.url
article.filename = path_info.filename
article.repo_path = path_info.repo
article.branch = branch
article.last_updated = details.last_updated
# We don't have a ton of cache space so reserve it for more
# high-traffic data like the rendered view of the articles.
if rendered_text:
# Force read of contributors and only cache it for published
# guides. Again, trying to save on cache space, and we're not too
# concerned with the list of contributors until a guide is
# published.
if article.published:
article._read_contributors_from_api()
cache.save_file(article.path, article.branch, lib.to_json(article))
else:
# We cannot properly show an article without metadata.
article = None
app.logger.error('Failed reading meta data for %s, full_path: %s, branch: %s',
path_info, full_path, branch)
if article is not None and article.image_url is None:
user = find_user(article.author_name)
if user is not None:
article.image_url = user.avatar_url
return article
def read_article_from_metadata(file_details):
"""
Read article object from json metadata
:param file_details: remote.file_details object
:returns: Article object with metadata filled out or None
Note the article contents are NOT filled out here!
"""
path_info = parse_full_path(file_details.path)
json_str = read_meta_data_for_article_path(file_details.path)
if json_str is None:
# Cannot do anything here b/c we do not know the title.
app.logger.error('Failed reading meta data for "%s", file_details: %s',
path_info, file_details)
return None
try:
return Article.from_json(json_str)
except ValueError:
app.logger.error('Failed parsing json meta data for "%s", file_details: %s, json: %s',
path_info, file_details, json_str)
return None
def save_article(title, message, new_content, author_name, email, sha,
branch=u'master', image_url=None, repo_path=None,
author_real_name=None, stacks=None, status=DRAFT):
"""
Create or save new (original) article, not branched article
:param title: Title of article
:param message: Commit message to save article with
:param content: Content of article
:param author_name: Name of author who wrote article
:param email: Email address of author
:param sha: Optional SHA of article if it already exists on github (This
must be the SHA of the current version of the article that is
being replaced.)
:param branch: Name of branch to commit file to (branch must already
exist)
:param image_url: Image to use for article
:param repo_path: Optional repo path to save into (<owner>/<name>)
:param author_real_name: Optional real name of author, not username
:param stacks: Optional list of stacks to associate with article
:param status: PUBLISHED, IN_REVIEW, or DRAFT
:returns: Article object updated or saved or None for failure
This function is not suitable for saving branched articles. The article
created here will be attributed to the given author_name whereas branched
articles should be created with branch_article() so the correct author
information is maintained.
"""
article = Article(title, author_name, branch=branch, image_url=image_url,
repo_path=repo_path, author_real_name=author_real_name,
stacks=stacks)
article.publish_status = status
commit_sha = remote.commit_file_to_github(article.full_path, message,
new_content, author_name, email,
sha, branch)
if commit_sha is None:
return commit_sha
# Had no previous SHA so this must be the first time we're saving it
if not sha:
article.first_commit = commit_sha
if branch != u'master':
commit_sha = save_branched_article_meta_data(article, author_name, email)
else:
commit_sha = save_article_meta_data(article, author_name, email, branch)
if commit_sha is None:
# FIXME: Handle error. This is interesting b/c now we created the
# article, but not the meta data.
return commit_sha
_delete_article_from_cache(article)
return read_article(article.path, rendered_text=True,
branch=article.branch, repo_path=repo_path)
def branch_article(article, message, new_content, author_name, email,
image_url, author_real_name=None):
"""
Create branch for article with new article contents
:param article: Article object to branch
:param message: Message describing article suggestions/changes
:param new_content: New article text
:param author_name: Name of author for article changes
:param email: Email of author for article changes
:param image_url: Image to use for article
:param author_real_name: Optional real name of author, not username
:returns: New article object
New branch will be named after author of changes
"""
branch = author_name
article_sha = article.sha
# Create branch if we needed to
repo_sha = remote.read_branch(article.repo_path, branch)
if repo_sha is None:
repo_sha = remote.read_branch(article.repo_path, u'master')
if repo_sha is None:
app.logger.error('Cannot find master branch "%s"',
article.repo_path)
return None
if not remote.create_branch(article.repo_path, branch, repo_sha):
return None
# Branch already exists
else:
# To make diffs look correct and history to be maintained between
# branches we take into account the situation here where the branch
# already existed but the article we're branching is not in the branch
# yet. In this case we want to merge the commits for this article into
# the branch. Then, any changes this branch is introducing will show up
# clearly in the diff instead of just adding this article to the branch
# as a new file.
if not remote.merge_branch(article.repo_path, branch, u'master',
'Merging recent changes from master'):
# This isn't ideal but if the merge fails we still allow the user
# to make their branched article. The diff/history will be a bit
# weird but we'd have to manually do the merge so it's not feasible
# via the API.
app.logger.warning('Failed merging branches so commiting branched article as new/updated file to branch')
# Try to read this article from the branch to update the SHA.
# This ensures we have the most up-to-date SHA for the file we're
# modifying, which is required by github API. For example, this article
# might already exist on the branch but the article.sha is the SHA for
# the ORIGINAL article, not branch.
branch_file = remote.file_details_from_github(article.full_path,
branch=branch)
if branch_file is not None:
article_sha = branch_file.sha
return save_article(article.title, message, new_content, author_name,
email, article_sha, branch=branch, image_url=image_url,
author_real_name=author_real_name,
stacks=article.stacks, status=article.publish_status)
def branch_or_save_article(title, path, message, content, author_name, email,
sha, image_url, repo_path=None,
author_real_name=None, stacks=None):
"""
Save article as original or as a branch depending on if given author is
the same as original article (if it already exists)
:param title: Title of article
:param path: Short path to article, not including repo or owner, or empty
for a new article
:param message: Commit message to save article with
:param content: Content of article
:param author_name: Name of author who wrote content
:param email: Email address of author
:param sha: Optional SHA of article if it already exists on github
:param branch: Name of branch to commit file to (branch must already
exist)
:param image_url: Image to use for article
:param repo_path: Optional repo path to save into (<owner>/<name>)
:param author_real_name: Optional real name of author, not username
:param stacks: Optional list of stacks to associate with article (this
argument is ignored if article is branched)
:returns: Article object updated, saved, or branched
"""
article = None
status = DRAFT
if path:
article = read_article(path, rendered_text=False, branch=u'master',
repo_path=repo_path)
if article is None:
app.logger.error('Failed reading article from %s to update', path)
return None
status = article.publish_status
if article and article.author_name != author_name and sha:
# Note branching an article cannot change the stacks!
new = branch_article(article, message, content, author_name, email,
image_url, author_real_name=author_real_name)
else:
new = save_article(title, message, content, author_name, email,
sha, image_url=image_url, repo_path=repo_path,
author_real_name=author_real_name,
stacks=stacks, status=status)
return new
def save_article_meta_data(article, author_name, email, branch=None):
"""
:param article: Article object
:param author_name: Name of author who wrote article
:param email: Email address of author
:param branch: Optional branch to save metadata, if not given
article.branch will be used
:returns: SHA of commit or None if commit failed
"""
filename = meta_data_path_for_article_path(article.full_path)
if branch is None:
branch = article.branch
# Get sha of meta data if it exists so we can update it if it already
# exists
details = remote.read_file_from_github(filename, rendered_text=False,
branch=branch, allow_404=True)
sha = None
text = None
if details is not None:
sha = details.sha
text = details.text
orig_article = Article.from_json(details.text)
# Merge the original article metadata with the new version. Currently
# the only thing that can change here is the list of branches. We only
# modify the list of branches when saving a branched article so we
# merge the two lists of branches here since removal of a branch should
# happen elsewhere.
for orig_branch in orig_article.branches:
if orig_branch not in article.branches:
article.branches.append(orig_branch)
# Don't need to serialize everything, just the important stuff that's not
# stored in the path and article.
exclude_attrs = ('content', 'external_url', 'sha', 'repo_path', '_path',
'last_updated', '_contributors')
json_content = lib.to_json(article, exclude_attrs=exclude_attrs)
# Nothing changed so no commit needed
if text is not None and json_content == text:
return True
message = u'Updating article metadata for "%s"' % (article.title)
_delete_article_from_cache(article)
# Article is on a branch so we have to update the master meta data file
# with this new branch as well as the branch meta data file.
return remote.commit_file_to_github(filename, message, json_content,
author_name, email, sha, branch=branch)
def read_meta_data_for_article_path(full_path):
"""
Read meta data for given article path from master branch
:param full_path: Full path to article
:returns: Meta-data for article as json
Always read meta data from master branch because metadata is never altered
or updated in branches to keep merging simple.
"""
filename = meta_data_path_for_article_path(full_path)
details = remote.read_file_from_github(filename, rendered_text=False)
if details is None:
return None
return details.text
def meta_data_path_for_article_path(full_path):
"""
Get path to meta data file for given article path
:param full_path: Article object
:returns: Full path to meta data file for article
"""
# Last part is the filename, which we're replacing. The meta data file will
# be stored right next to the article.
meta_data_path = '/'.join(full_path.split('/')[:-1])
return '%s/%s' % (meta_data_path, ARTICLE_METADATA_FILENAME)
def save_branched_article_meta_data(article, author_name, email,
add_branch=True):
"""
Save metadata for branched article
:param article: Article object with branch attribute set to branch name
:param name: Name of author who wrote branched article
:param email: Email address of branched article author
:param add_branch: True if article should be saved as a branch False if
article should be removed as a branch
:returns: SHA of commit or None if commit failed
Metadata for branched articles should be identical to the original article.
This makes it easier for automatically merging changes because metadata
differences won't get in the way. The author_name is the only thing useful
for a branched article. However, that should already be encoded in the
branch name and the commits. So, editors of original articles will get
credit for helping via those mechanisms, not metadata.
"""
orig_article = read_article(article.path, rendered_text=False,
branch=u'master', repo_path=article.repo_path)
# Nothing to save, we're already tracking this branch
if add_branch:
if article.branch in orig_article.branches:
return True
orig_article.branches.append(article.branch)
else:
try:
orig_article.branches.remove(article.branch)
except ValueError:
# Branch isn't being tracked anyway so nothing to remove
return True
# Note we don't ever change metadata on the branches. This keeps the
# metadata from showing in up in merges. We only want to deal with article
# text for merges.
return save_article_meta_data(orig_article, author_name, email)
def delete_article(article, message, name, email):
"""
Delete article from repository
:param article: Article object to remove
:param message: Message to include as commit when removing article
:param name: Name of user deleting article
:param email: Email address of user deleting article
:returns: True if article was successfully removed or False otherwise
This removes the article from the repository but not the history of
the file.
Only original author can remove file from master branch. Articles can be
removed from non-master branches only by the user who created that branch.
"""
# User didn't write original article and user isn't trying to remove from
# their own branch
if name != article.author_name and article.branch != name:
app.logger.error('Cannot delete article user does not own path: %s, author: %s deleter: %s',
article.full_path, article.author_name, name)
return False
# First remove from cache even if removing the actual file fails this will
# be OK b/c we'll just end up re-caching it.
_delete_article_from_cache(article)
# We don't save meta data for branches so either remove meta data file or
# update original articles meta data to remove the branch link.
if article.branch == u'master':
# Remove the meta data file next since that's the most important for
# us finding an article from the API.
meta_data_file = meta_data_path_for_article_path(article.full_path)
if not remote.remove_file_from_github(meta_data_file, message, name,
email, article.branch):
return False
else:
if not save_branched_article_meta_data(article, name, email,
add_branch=False):
return False
if not remote.remove_file_from_github(article.full_path, message,
name, email, article.branch):
return False
# FIXME: Need to update the cache reference to the original article so we
# don't think this branch still exists in cache.
return True
def parse_full_path(path):
"""
Parse full path and return tuple of details embedded in path
:param path: Full path to file including repo and owner
:returns: path_details tuple
"""
tokens = path.split('/')
# Repo path is user/repo_name
repo_path = '/'.join(tokens[:2])
filename = tokens[-1]
return path_details(repo_path, filename)
def find_article_by_title(articles, title):
"""
Search through list of article objects looking for article with given title
:param articles: List of article objects
:param title: Title to search for
:returns: article object or None if not found
"""
title = utils.slugify(title)
for article in articles:
if utils.slugify(article.title) == title:
return article
return None
def change_article_stack(orig_path, orig_stack, new_stack, title, author_name,
email):
"""
Change article stack
:param orig_path: Current path to article without repo or owner
:param orig_stack: Original stack
:param new_stack: New stack
:param author_name: Name of author who wrote article
:param email: Email address of author
:returns: New path of article or None if error
Note this function only makes changes to articles on the master branch!
"""
# Ugly circular imports
from .. import tasks
new_path = orig_path.replace(utils.slugify_stack(orig_stack),
utils.slugify_stack(new_stack))
try:
tasks.move_article(orig_path, new_path, title, author_name, email)
except subprocess.CalledProcessError as err:
app.logger.error(err)
return None
cache.delete_file(orig_path, u'master')
return new_path
def _delete_article_from_cache(article):
"""
Delete given article from cache if it exists
:param article: Article object to delete
Note this function is harmless if the article does not exist in the cache.
"""
# The point of this function is so outside article.py there is no knowledge
# of what the cache key is or how we cache it.
cache.delete_file(article.path, article.branch)
def _read_article_from_cache(path, branch=u'master'):
"""
Read article object from cache
:param path: Path to read file from github i.e. path it was cached with
:param branch: Branch to read file from
:returns: Article object if found in cache or None
"""
if path.endswith(FILE_EXTENSION):
# Don't cache with the filename b/c it just takes up cache space and
# right now it's always the same.
path = path.split('/')[-2]
json_str = cache.read_file(path, branch)
if json_str is None:
return None
return Article.from_json(json_str)
class Article(object):
"""
Object representing article
"""
def __init__(self, title, author_name, filename=ARTICLE_FILENAME,
repo_path=None, branch=u'master', stacks=None, sha=None,
content=None, external_url=None, image_url=None,
author_real_name=None):
"""
Initalize article object
:param title: Title of article
:param author_name: Name of original author
:param filename: Name of file to save article in
:param repo_path: Path to repository to save article in
:param branch: Branch to save article to
:param stacks: Language/stack article primarily covers
:param sha: Git SHA of article (if article already exists in repo)
:param content: Contents of article
:param external_url: External URL to view article at
:param image_url: URL to image to show for article
:param author_real_name: Optional real name of author, not username
"""
self.title = title
self.author_name = author_name
self.stacks = stacks or [u'other']
self.content = content
self.external_url = external_url
self.filename = filename
self.image_url = image_url
self.last_updated = None
self.thumbnail_url = None
self.author_real_name = author_real_name or author_name
self.first_commit = None
# Only useful if article has already been saved to github
self.sha = sha
self.repo_path = repo_path
if self.repo_path is None:
self.repo_path = remote.default_repo_path()
# Branch this article is on
self.branch = branch
# List of branch names where this article also exists
self.branches = []
self._path = None
self._publish_status = DRAFT
# List of User objects representing any 'author' i.e user who has
# contributed at least 1 line of text to this article.
self._contributors = None
@property
def path(self):
return u'%s/%s/%s' % (self.publish_status,
utils.slugify_stack(self.stacks[0]),
utils.slugify(self.title))
def __repr__(self):
return '<author_name: %s title: %s status: %s>' % (self.author_name,
self.title,
self.publish_status)
@property
def publish_status(self):
return self._publish_status
@publish_status.setter
def publish_status(self, new_status):
if new_status not in STATUSES:
raise ValueError('publish_status must be one of %s' % (STATUSES,))
# Lets go ahead and delete it b/c cached publish status would be out of
# date now
_delete_article_from_cache(self)
self._publish_status = new_status
@property
def published(self):
return self.publish_status == PUBLISHED
@property
def contributors(self):
"""
List of tuples representing any 'author' i.e user who has contributed
at least 1 line of text to this article. Each tuple is in the form of
(name, login) where name can be None.
We use plain tuples instead of named tuples or User objects so we can
easily seralize the contributors to JSON.
"""
# Small form of caching. This way we only fetch the contributors once.
# NOTE: This could result in some data out of data if we have new
# contributors after this is called but contributor information isn't
# super important so should be ok.
if self._contributors is not None:
return self._contributors
self._read_contributors_from_api()
return self._contributors
@staticmethod
def from_json(str_):
"""
Create article object from json string
:param str_: json string representing article
:returns: Article object
"""
dict_ = json.loads(str_)
# Required arguments
title = dict_.pop('title', None)
author_name = dict_.pop('author_name', None)
article = Article(title, author_name)
for attr, value in dict_.iteritems():
# Backwards-compatability, this field was renamed
if attr == 'published':
attr = '_publish_status'
if value:
value = PUBLISHED
else:
value = DRAFT
# Another rename so we could use a property
elif attr == 'publish_status':
attr = '_publish_status'
# This field used to be optional
elif attr == 'stacks' and not value:
value = [u'other']
if attr == '_publish_status' and value not in STATUSES:
raise ValueError('publish_status must be one of %s' % (STATUSES,))
setattr(article, attr, value)
return article
@property
def full_path(self):
"""
Get full path to article including repo information