/
database.py
1521 lines (1212 loc) · 54.2 KB
/
database.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
import contextlib
import logging
import os
import shutil
from datetime import datetime
from math import ceil
from pathlib import Path
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.db.models import CASCADE
from django.db.models.signals import pre_delete, post_delete, pre_save
from django.dispatch import receiver
from django.http import Http404
from django.urls import reverse
from django.utils.http import urlencode
from django.utils.translation import gettext_lazy as _
from elasticsearch_dsl import Mapping, Q as ES_Q
from elasticsearch_dsl.field import Text, Keyword, Date, Boolean
from git import Repo, BadObject
from gitdb.exc import BadName
from zds import json_handler
from zds.forum.models import Topic
from zds.gallery.models import Image, Gallery, UserGallery, GALLERY_WRITE
from zds.member.utils import get_external_account
from zds.mp.models import PrivateTopic
from zds.searchv2.models import (
AbstractESDjangoIndexable,
AbstractESIndexable,
delete_document_in_elasticsearch,
ESIndexManager,
)
from zds.tutorialv2.managers import PublishedContentManager, PublishableContentManager, ReactionManager
from zds.tutorialv2.models import TYPE_CHOICES, STATUS_CHOICES, CONTENT_TYPES_REQUIRING_VALIDATION, PICK_OPERATIONS
from zds.tutorialv2.models.goals import Goal
from zds.tutorialv2.models.mixins import TemplatableContentModelMixin, OnlineLinkableContentMixin
from zds.tutorialv2.models.versioned import NotAPublicVersion
from zds.tutorialv2.utils import get_content_from_json, BadManifestError, get_blob
from zds.utils import get_current_user
from zds.utils.models import SubCategory, Licence, Comment, Tag
from zds.tutorialv2.models.help_requests import HelpWriting
from zds.utils.templatetags.emarkdown import render_markdown_stats
from zds.utils.uuslug_wrapper import uuslug
ALLOWED_TYPES = ["pdf", "md", "epub", "zip", "tex"]
logger = logging.getLogger(__name__)
class PublishableContent(models.Model, TemplatableContentModelMixin):
"""A publishable content.
A PublishableContent retains metadata about a content in database, such as
- authors, description, source (if the content comes from another website), subcategory, tags and licence ;
- Thumbnail and gallery ;
- Creation, publication and update date ;
- Public, beta, validation and draft sha, for versioning ;
- Comment support ;
- Type, which is either ``'ARTICLE'``, ``'TUTORIAL'`` or ``'OPINION'``
"""
class Meta:
verbose_name = "Contenu"
verbose_name_plural = "Contenus"
content_type_attribute = "type"
title = models.CharField("Titre", max_length=80)
slug = models.CharField("Slug", max_length=80)
description = models.CharField("Description", max_length=200)
source = models.URLField("Source", max_length=200, blank=True, null=True)
authors = models.ManyToManyField(User, verbose_name="Auteurs", db_index=True)
old_pk = models.IntegerField(db_index=True, default=0)
subcategory = models.ManyToManyField(SubCategory, verbose_name="Sous-Catégorie", blank=True, db_index=True)
tags = models.ManyToManyField(Tag, verbose_name="Tags du contenu", blank=True, db_index=True)
goals = models.ManyToManyField(
Goal, verbose_name="Objectifs du contenu", blank=True, db_index=True, related_name="contents"
)
# store the thumbnail for tutorial or article
image = models.ForeignKey(Image, verbose_name="Image du tutoriel", blank=True, null=True, on_delete=models.SET_NULL)
# every publishable content has its own gallery to manage images
gallery = models.ForeignKey(
Gallery, verbose_name="Galerie d'images", blank=True, null=True, db_index=True, on_delete=models.SET_NULL
)
creation_date = models.DateTimeField("Date de création")
pubdate = models.DateTimeField("Date de publication", blank=True, null=True, db_index=True)
update_date = models.DateTimeField("Date de mise à jour", blank=True, null=True)
picked_date = models.DateTimeField("Date de mise en avant", db_index=True, blank=True, null=True, default=None)
sha_public = models.CharField("Sha1 de la version publique", blank=True, null=True, max_length=80, db_index=True)
sha_beta = models.CharField("Sha1 de la version beta publique", blank=True, null=True, max_length=80, db_index=True)
sha_validation = models.CharField(
"Sha1 de la version en validation", blank=True, null=True, max_length=80, db_index=True
)
sha_draft = models.CharField("Sha1 de la version de rédaction", blank=True, null=True, max_length=80, db_index=True)
sha_picked = models.CharField(
"Sha1 de la version choisie (contenus publiés sans validation)",
blank=True,
null=True,
max_length=80,
db_index=True,
)
beta_topic = models.ForeignKey(
Topic, verbose_name="Sujet beta associé", default=None, blank=True, null=True, on_delete=models.SET_NULL
)
validation_private_message = models.ForeignKey(
PrivateTopic,
verbose_name="Message de suivi staff",
default=None,
blank=True,
null=True,
on_delete=models.SET_NULL,
)
licence = models.ForeignKey(
Licence, verbose_name="Licence", blank=True, null=True, db_index=True, on_delete=models.SET_NULL
)
# as of ZEP 12 this field is no longer the size but the type of content (article/tutorial/opinion)
type = models.CharField(max_length=10, choices=TYPE_CHOICES, db_index=True)
# zep03 field
helps = models.ManyToManyField(HelpWriting, verbose_name="Aides", blank=True, db_index=True)
relative_images_path = models.CharField("chemin relatif images", blank=True, null=True, max_length=200)
last_note = models.ForeignKey(
"ContentReaction",
blank=True,
null=True,
related_name="last_note",
verbose_name="Derniere note",
on_delete=models.SET_NULL,
)
is_locked = models.BooleanField("Est verrouillé", default=False)
js_support = models.BooleanField("Support du Javascript", default=False)
must_reindex = models.BooleanField("Si le contenu doit-être ré-indexé", default=True)
is_obsolete = models.BooleanField("Est obsolète", default=False)
public_version = models.ForeignKey(
"PublishedContent", verbose_name="Version publiée", blank=True, null=True, on_delete=models.SET_NULL
)
# FK to an opinion which has been converted to article. Useful to keep track of history and
# to add a canonical link
converted_to = models.ForeignKey(
"self",
verbose_name="Contenu promu",
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name="converted_from",
)
objects = PublishableContentManager()
def __str__(self):
return self.title
def update(self, **fields):
"""
wrapper arround ``self.objects.update``
:param fields: Fields to update
:return: modified self
"""
self.__class__.objects.filter(pk=self.pk).update(**fields)
self.refresh_from_db(fields=list(fields.keys()))
return self
def save(self, *args, force_slug_update=False, update_date=True, **kwargs):
"""
Rewrite the ``save()`` function to handle slug uniqueness
:param update_date: if ``True`` will assign "update_date" property to now
:param force_slug_update: if ``True`` will try to update the slug
"""
if self.slug == "" or force_slug_update:
self.slug = uuslug(self.title, instance=self, max_length=80)
if update_date:
self.update_date = datetime.now()
super().save(*args, **kwargs)
def get_absolute_url_beta(self):
"""NOTE: it's better to use the version contained in `VersionedContent`, if possible !
:return: absolute URL to the beta version the content
:rtype: str
"""
return reverse("content:beta-view", kwargs={"pk": self.pk, "slug": self.slug})
def get_absolute_url_online(self):
"""NOTE: it's better to use the version contained in `VersionedContent`, if possible !
:return: absolute URL to the public version the content, if `self.public_version` is defined
:rtype: str
"""
if self.public_version:
return self.public_version.get_absolute_url_online()
return ""
def get_absolute_contact_url(self, title="Collaboration"):
"""Get url to send a new PM for collaboration
:param title: what is going to be in the title of the PM before the name of the content
:type title: str
:return: url to the PM creation form
:rtype: str
"""
get = "?" + urlencode({"title": f"{title} - {self.title}"})
for author in self.authors.all():
get += "&" + urlencode({"username": author.username})
return reverse("mp:create") + get
def get_repo_path(self, relative=False):
"""Get the path to the tutorial repository
:param relative: if ``True``, the path will be relative, absolute otherwise.
:return: physical path
:rtype: str
"""
if relative:
return ""
else:
# get the full path (with tutorial/article before it)
return os.path.join(settings.ZDS_APP["content"]["repo_private_path"], self.slug)
def ensure_author_gallery(self):
"""
ensure all authors subscribe to gallery
"""
author_set = UserGallery.objects.filter(user__in=list(self.authors.all()), gallery=self.gallery).values_list(
"user__pk", flat=True
)
for author in self.authors.all():
if author.pk in author_set:
continue
user_gallery = UserGallery()
user_gallery.gallery = self.gallery
user_gallery.mode = GALLERY_WRITE # write mode
user_gallery.user = author
user_gallery.save()
def in_beta(self):
"""A tutorial is not in beta if sha_beta is ``None`` or empty
:return: ``True`` if the tutorial is in beta, ``False`` otherwise
:rtype: bool
"""
return (self.sha_beta is not None) and (self.sha_beta.strip() != "")
def in_validation(self):
"""A tutorial is not in validation if sha_validation is ``None`` or empty
:return: ``True`` if the tutorial is in validation, ``False`` otherwise
:rtype: bool
"""
return (self.sha_validation is not None) and (self.sha_validation.strip() != "")
def in_drafting(self):
"""A tutorial is not in draft if sha_draft is ``None`` or empty
:return: ``True`` if the tutorial is in draft, ``False`` otherwise
:rtype: bool
"""
return (self.sha_draft is not None) and (self.sha_draft.strip() != "")
def in_public(self):
"""A tutorial is not in on line if sha_public is ``None`` or empty
:return: ``True`` if the tutorial is on line, ``False`` otherwise
:rtype: bool
"""
return (self.sha_public is not None) and (self.sha_public.strip() != "")
def is_beta(self, sha):
"""Is this version of the content the beta version ?
:param sha: version
:return: ``True`` if the tutorial is in beta, ``False`` otherwise
:rtype: bool
"""
return self.in_beta() and sha == self.sha_beta
def is_validation(self, sha):
"""Is this version of the content the validation version ?
:param sha: version
:return: ``True`` if the tutorial is in validation, ``False`` otherwise
:rtype: bool
"""
return self.in_validation() and sha == self.sha_validation
def is_public(self, sha):
"""Is this version of the content the published version ?
:param sha: version
:return: ``True`` if the tutorial is in public, ``False`` otherwise
:rtype: bool
"""
return self.in_public() and sha == self.sha_public
def is_permanently_unpublished(self):
"""Is this content permanently unpublished by a moderator ?"""
return PickListOperation.objects.filter(content=self, operation="REMOVE_PUB", is_effective=True).exists()
def load_version_or_404(self, sha=None, public=None):
"""Using git, load a specific version of the content. if `sha` is `None`, the draft/public version is used (if
`public` is `True`).
:param sha: version
:param public: if set with the right object, return the public version
:type public: PublishedContent
:raise Http404: if sha is not None and related version could not be found
:return: the versioned content
:rtype: zds.tutorialv2.models.versioned.ViersionedContent
"""
try:
return self.load_version(sha, public)
except (BadObject, BadName, OSError) as error:
raise Http404(
"Le code sha existe mais la version demandée ne peut pas être trouvée à cause de {}:{}".format(
type(error), str(error)
)
)
@property
def first_publication_date(self):
"""
traverse PublishedContent instances to find the first ever published and get its date
:return: the first publication date
:rtype: datetime
"""
return (
Validation.objects.filter(content=self, status="ACCEPT")
.order_by("date_validation")
.values_list("date_validation", flat=True)[0]
)
def load_manifest(self, sha=None, public=None):
"""Using git, load a specific version of the manifest. if ``sha`` is ``None``,
the draft/public version is used (if ``public`` is ``True``).
:param sha: version
:param public: if set with the right object, return the public version
:type public: PublishedContent
:raise BadObject: if sha is not None and related version could not be found
:raise OSError: if the path to the repository is wrong
:raise NotAPublicVersion: if the sha does not correspond to a public version
:return: the manifest
:rtype: dict
"""
# load the good manifest.json
if sha is None:
if not public:
sha = self.sha_draft
else:
sha = self.sha_public
if public and isinstance(public, PublishedContent): # use the public (altered and not versioned) repository
path = public.get_prod_path()
if not os.path.isdir(path):
raise OSError(path)
if sha != public.sha_public:
raise NotAPublicVersion
with open(os.path.join(path, "manifest.json"), encoding="utf-8") as f:
manifest = json_handler.loads(f.read())
else: # draft version, use the repository (slower, but allows manipulation)
path = self.get_repo_path()
if not os.path.isdir(path):
raise OSError(path)
repo = Repo(path)
data = get_blob(repo.commit(sha).tree, "manifest.json")
try:
manifest = json_handler.loads(data)
logger.debug("loaded json")
except ValueError:
raise BadManifestError(
_("Une erreur est survenue lors de la lecture du manifest.json, est-ce du JSON ?")
)
return manifest
def load_version(self, sha=None, public=None):
"""Using git, load a specific version of the content. if ``sha`` is ``None``,
the draft/public version is used (if ``public`` is ``True``).
.. attention::
for practical reason, the returned object is filled with information from DB.
:param sha: version
:param public: if set with the right object, return the public version
:type public: PublishedContent
:raise BadObject: if sha is not None and related version could not be found
:raise OSError: if the path to the repository is wrong
:raise NotAPublicVersion: if the sha does not correspond to a public version
:return: the versioned content
:rtype: zds.tutorialv2.models.versioned.VersionedContent
"""
# load the good manifest.json
if sha is None:
if not public:
sha = self.sha_draft
else:
sha = self.sha_public
max_title_length = PublishableContent._meta.get_field("title").max_length
json = self.load_manifest(sha=sha, public=public)
if public and isinstance(public, PublishedContent):
versioned = get_content_from_json(
json,
public.sha_public,
public.content_public_slug,
public=True,
max_title_len=max_title_length,
hint_licence=self.licence,
)
else:
versioned = get_content_from_json(json, sha, self.slug, max_title_len=max_title_length)
self.insert_data_in_versioned(versioned)
return versioned
def insert_data_in_versioned(self, versioned):
"""Insert some additional data from database in a VersionedContent
:param versioned: the VersionedContent to fill
"""
attrs = [
"pk",
"authors",
"subcategory",
"image",
"creation_date",
"pubdate",
"update_date",
"source",
"sha_draft",
"sha_beta",
"sha_validation",
"sha_public",
"tags",
"sha_picked",
"converted_to",
"type",
]
fns = ["in_beta", "in_validation", "in_public", "get_absolute_contact_url", "get_note_count", "antispam"]
# load functions and attributs in `versioned`
for attr in attrs:
setattr(versioned, attr, getattr(self, attr))
for fn in fns:
setattr(versioned, fn, getattr(self, fn)())
# general information
versioned.is_beta = self.is_beta(versioned.current_version)
versioned.is_validation = self.is_validation(versioned.current_version)
versioned.is_public = self.is_public(versioned.current_version)
def get_note_count(self):
"""Count all the reactions to this content. Warning, if you did not pre process this number, \
a query will be sent
:return: number of notes in the content.
:rtype: int
"""
try:
return self.count_note
except AttributeError:
self.count_note = ContentReaction.objects.filter(related_content__pk=self.pk, is_visible=True).count()
return self.count_note
def get_last_note(self):
"""
:return: the last answer in the thread, if any.
:rtype: ContentReaction|None
"""
return (
ContentReaction.objects.all()
.select_related("related_content")
.select_related("related_content__public_version")
.filter(related_content__pk=self.pk)
.order_by("-pubdate")
.first()
)
def first_note(self):
"""
:return: the first post of a topic, written by topic's author, if any.
:rtype: ContentReaction
"""
return (
ContentReaction.objects.select_related("related_content")
.select_related("related_content__public_version")
.filter(related_content=self)
.order_by("pubdate")
.first()
)
def last_read_note(self):
"""
:return: the last post the user has read.
:rtype: ContentReaction
"""
user = get_current_user()
if user and user.is_authenticated:
try:
read = (
ContentRead.objects.select_related("note")
.select_related("note__related_content")
.select_related("note__related_content__public_version")
.filter(content=self, user__pk=user.pk)
.latest("note__pubdate")
)
if read is not None and read.note: # one case can show a read without note : the author has just
# published his content and one comment has been posted by someone else.
return read.note
except ContentRead.DoesNotExist:
pass
return self.first_note()
def first_unread_note(self, user=None):
"""
:return: Return the first note the user has unread.
:rtype: ContentReaction
"""
if user is None:
user = get_current_user()
if user and user.is_authenticated:
try:
read = ContentRead.objects.filter(content=self, user__pk=user.pk).latest("note__pubdate")
if read and read.note:
last_note = read.note
next_note = (
ContentReaction.objects.select_related("related_content")
.select_related("related_content__public_version")
.filter(related_content__pk=self.pk, pk__gt=last_note.pk)
.select_related("author")
.first()
)
if next_note:
return next_note
else:
return last_note
except ContentRead.DoesNotExist:
pass
return self.first_note()
def antispam(self, user=None):
"""Check if the user is allowed to post in an tutorial according to the SPAM_LIMIT_SECONDS value.
:param user: the user to check antispam. If ``None``, current user is used.
:return: ``True`` if the user is not able to note (the elapsed time is not enough), ``False`` otherwise.
:rtype: bool
"""
if user is None:
user = get_current_user()
if user and user.is_authenticated:
last_user_notes = (
ContentReaction.objects.filter(related_content=self).filter(author=user.pk).order_by("-position")
)
if last_user_notes and last_user_notes[0] == self.last_note:
last_user_note = last_user_notes[0]
t = datetime.now() - last_user_note.pubdate
if t.total_seconds() < settings.ZDS_APP["forum"]["spam_limit_seconds"]:
return True
def repo_delete(self):
"""
Delete the entities and their filesystem counterparts
"""
if os.path.exists(self.get_repo_path()):
shutil.rmtree(self.get_repo_path(), False)
if self.in_public() and self.public_version:
if os.path.exists(self.public_version.get_prod_path()):
shutil.rmtree(self.public_version.get_prod_path())
Validation.objects.filter(content=self).delete()
def add_tags(self, tag_collection):
"""
Add all tags contained in `tag_collection` to this content.
If a tag is unknown, it is added to the system.
:param tag_collection: A collection of tags.
:type tag_collection: list
"""
for tag in filter(None, tag_collection):
try:
current_tag, created = Tag.objects.get_or_create(title=tag.lower().strip())
self.tags.add(current_tag)
except ValueError as e:
logger.warning(e)
logger.debug("Initial number of tags=%s, after filtering=%s", len(tag_collection), len(self.tags.all()))
self.save()
def requires_validation(self):
"""
Check if content required a validation before publication.
Used to check if JsFiddle is available too.
:return: Whether validation is required before publication.
:rtype: bool
"""
return self.type in CONTENT_TYPES_REQUIRING_VALIDATION
@receiver(pre_delete, sender=PublishableContent)
def delete_repo(sender, instance, **kwargs):
"""catch the pre_delete signal to ensure the deletion of the repository if a PublishableContent is deleted"""
instance.repo_delete()
@receiver(post_delete, sender=PublishableContent)
def delete_gallery(sender, instance, **kwargs):
"""catch the post_delete signal to ensure the deletion of the gallery (otherwise, you generate a loop)"""
if instance.gallery:
instance.gallery.delete()
class PublishedContent(AbstractESDjangoIndexable, TemplatableContentModelMixin, OnlineLinkableContentMixin):
"""A class that contains information on the published version of a content.
Used for quick url resolution, quick listing, and to know where the public version of the files are.
Linked to a ``PublishableContent`` for the rest. Don't forget to add a ``.prefetch_related('content')`` !!
"""
objects_per_batch = 25
class Meta:
verbose_name = "Contenu publié"
verbose_name_plural = "Contenus publiés"
content = models.ForeignKey(PublishableContent, verbose_name="Contenu", on_delete=models.CASCADE)
content_type = models.CharField(max_length=10, choices=TYPE_CHOICES, db_index=True, verbose_name="Type de contenu")
content_public_slug = models.CharField("Slug du contenu publié", max_length=80)
content_pk = models.IntegerField("Pk du contenu publié", db_index=True)
publication_date = models.DateTimeField("Date de publication", db_index=True, blank=True, null=True)
update_date = models.DateTimeField("Date de mise à jour", db_index=True, blank=True, null=True, default=None)
sha_public = models.CharField("Sha1 de la version publiée", blank=True, null=True, max_length=80, db_index=True)
char_count = models.IntegerField(default=None, null=True, verbose_name=b"Nombre de lettres du contenu", blank=True)
# NOTE: removing the spurious space in the field description requires a database migration !
must_redirect = models.BooleanField(
"Redirection vers une version plus récente", blank=True, db_index=True, default=False
)
authors = models.ManyToManyField(User, verbose_name="Auteurs", db_index=True)
objects = PublishedContentManager()
versioned_model = None
# sizes contain a python dict (as a string in database) with all information about file sizes
sizes = models.CharField("Tailles des fichiers téléchargeables", max_length=512, default="{}")
_meta_description = None
_manifest = None
@staticmethod
def get_slug_from_file_path(file_path):
return os.path.splitext(os.path.split(file_path)[1])[0]
def __str__(self):
return _('Version publique de "{}"').format(self.content.title)
def title(self):
if self.versioned_model:
return self.versioned_model.title
else:
title = "Default title"
try:
self._manifest = self._manifest or self.content.load_manifest(sha=self.sha_public, public=self)
title = self._manifest.get("title", title)
except OSError:
title = self.content.title
return title
def description(self):
if self.versioned_model:
return self.versioned_model.description
else:
self._manifest = self._manifest or self.content.load_manifest(sha=self.sha_public, public=self)
return self._manifest.get("description", "Default description")
def meta_description(self):
"""Generate a description for the HTML tag <meta name='description'>"""
if self._meta_description:
return self._meta_description
if not self.versioned_model:
self.load_public_version()
self._meta_description = self.versioned_model.description or self.versioned_model.get_introduction() or ""
if self._meta_description:
self._meta_description = self._meta_description[: settings.ZDS_APP["forum"]["description_size"]]
return self._meta_description
def get_prod_path(self, relative=False):
if not relative:
return str(Path(settings.ZDS_APP["content"]["repo_public_path"], self.content_public_slug).absolute())
else:
return ""
def load_public_version_or_404(self):
"""
:return: the public content
:rtype: zds.tutorialv2.models.database.PublicContent
:raise Http404: if the version is not available
"""
with contextlib.suppress(AttributeError):
self.content.count_note = self.count_note
self.versioned_model = self.content.load_version_or_404(sha=self.sha_public, public=self)
return self.versioned_model
def load_public_version(self):
"""
:rtype: zds.tutorialv2.models.database.PublicContent
:return: the public content
"""
with contextlib.suppress(AttributeError):
self.content.count_note = self.count_note
self.versioned_model = self.content.load_version(sha=self.sha_public, public=self)
return self.versioned_model
def get_extra_contents_directory(self):
"""
:return: path to all the 'extra contents'
:rtype: str
"""
return os.path.join(self.get_prod_path(), settings.ZDS_APP["content"]["extra_contents_dirname"])
def has_type(self, type_):
"""check if a given extra content exists
:return: ``True`` if the file exists, ``False`` otherwhise
:rtype: bool
"""
if type_ in ALLOWED_TYPES:
return Path(self.get_extra_contents_directory(), self.content_public_slug + "." + type_).is_file()
return False
def is_exported(self):
"""
If the content has at least one export, it returns ``True``
"""
return any(self.has_type(t) for t in ALLOWED_TYPES)
def has_md(self):
"""Check if the flat markdown version of the content is available
:return: ``True`` if available, ``False`` otherwise
:rtype: bool
"""
return self.has_type("md")
def has_pdf(self):
"""Check if the pdf version of the content is available
:return: ``True`` if available, ``False`` otherwise
:rtype: bool
"""
return self.has_type("pdf")
def has_epub(self):
"""Check if the standard epub version of the content is available
:return: ``True`` if available, ``False`` otherwise
:rtype: bool
"""
return self.has_type("epub")
def has_zip(self):
"""Check if the standard zip version of the content is available
:return: ``True`` if available, ``False`` otherwise
:rtype: bool
"""
return self.has_type("zip")
def has_tex(self):
"""Check if the latex version of the content is available
:return: ``True`` if available, ``False`` otherwise
:rtype: bool
"""
return self.has_type("tex")
def get_size_file_type(self, type_):
"""
Get the size of a given extra content.
Is the size is not in database we get it and store it for next time.
:return: size of file
:rtype: int
"""
if type_ in ALLOWED_TYPES:
sizes = eval(str(self.sizes))
try:
size = sizes[type_]
except KeyError:
# if size is not in database we store it
sizes[type_] = os.path.getsize(
os.path.join(self.get_extra_contents_directory(), self.content_public_slug + "." + type_)
)
self.sizes = sizes
self.save()
size = sizes[type_]
return size
return None
def get_size_md(self):
"""Get the size of md
:return: size of file
:rtype: int
"""
return self.get_size_file_type("md")
def get_size_html(self):
"""Get the size of html
:return: size of file
:rtype: int
"""
return self.get_size_file_type("html")
def get_size_pdf(self):
"""Get the size of pdf
:return: size of file
:rtype: int
"""
return self.get_size_file_type("pdf")
def get_size_tex(self):
"""Get the size of LaTeX file
:return: size of file
:rtype: int
"""
return self.get_size_file_type("tex")
def get_size_epub(self):
"""Get the size of epub
:return: size of file
:rtype: int
"""
return self.get_size_file_type("epub")
def get_size_zip(self):
"""Get the size of zip
:return: size of file
:rtype: int
"""
return self.get_size_file_type("zip")
def get_absolute_url_to_extra_content(self, type_):
"""Get the url that point to the extra content the user may want to download
:param type_: the type inside allowed_type
:type type_: str
:return: URL to a given extra content (note that no check for existence is done)
:rtype: str
"""
if type_ in ALLOWED_TYPES:
reversed_ = self.content_type.lower()
return reverse(
reversed_ + ":download-" + type_, kwargs={"pk": self.content_pk, "slug": self.content_public_slug}
)
return ""
def get_absolute_url_md(self):
"""wrapper around ``self.get_absolute_url_to_extra_content('md')``
:return: URL to the full markdown version of the published content
:rtype: str
"""
return self.get_absolute_url_to_extra_content("md")
def get_absolute_url_html(self):
"""wrapper around ``self.get_absolute_url_to_extra_content('html')``
:return: URL to the HTML version of the published content
:rtype: str
"""
return self.get_absolute_url_to_extra_content("html")
def get_absolute_url_pdf(self):
"""wrapper around ``self.get_absolute_url_to_extra_content('pdf')``
:return: URL to the PDF version of the published content
:rtype: str
"""
return self.get_absolute_url_to_extra_content("pdf")
def get_absolute_url_tex(self):
"""wrapper around ``self.get_absolute_url_to_extra_content('tex')``
:return: URL to the tex version of the published content
:rtype: str
"""
return self.get_absolute_url_to_extra_content("tex")
def get_absolute_url_epub(self):
"""wrapper around ``self.get_absolute_url_to_extra_content('epub')``
:return: URL to the epub version of the published content
:rtype: str
"""
return self.get_absolute_url_to_extra_content("epub")
def get_absolute_url_zip(self):
"""wrapper around ``self.get_absolute_url_to_extra_content('zip')``
:return: URL to the zip archive of the published content
:rtype: str
"""
return self.get_absolute_url_to_extra_content("zip")
def get_char_count(self, md_file_path=None):
"""Compute the number of letters for a given content
:param md_file_path: use another file to compute the number of letter rather than the default one.
:type md_file_path: str
:return: Number of letters in the md file
:rtype: int
"""
if not md_file_path:
md_file_path = os.path.join(self.get_extra_contents_directory(), self.content_public_slug + ".md")
try:
with open(md_file_path, encoding="utf-8") as md_file_handler:
content = md_file_handler.read()
current_content = PublishedContent.objects.filter(content_pk=self.content_pk, must_redirect=False).first()
if current_content:
return render_markdown_stats(content)
except OSError as e:
logger.warning("could not get file %s to compute nb letters (error=%s)", md_file_path, e)
@property
def last_publication_date(self):
return max(self.publication_date, self.update_date or datetime.min)
@classmethod
def get_es_mapping(cls):
mapping = Mapping(cls.get_es_document_type())
mapping.field("content_pk", "integer")
mapping.field("publication_date", Date())
mapping.field("content_type", Keyword())
# not from PublishedContent directly:
mapping.field("title", Text(boost=1.5))
mapping.field("description", Text(boost=1.5))