4
4
5
5
from dispatch .modules .content .models import (
6
6
Article , Image , ImageAttachment , ImageGallery , Issue ,
7
- File , Page , Author , Section , Tag , Topic , Video , VideoAttachment , Poll , PollAnswer , PollVote )
7
+ File , Page , Author , Section , Tag , Topic , Video ,
8
+ VideoAttachment , Poll , PollAnswer , PollVote , Subsection )
8
9
from dispatch .modules .auth .models import Person , User , Invite
9
10
from dispatch .admin .registration import send_invitation
10
11
from dispatch .theme .exceptions import WidgetNotFound , InvalidField
@@ -520,6 +521,83 @@ def insert_data(self, content):
520
521
521
522
return map (self .insert_instance , content )
522
523
524
+ class SubsectionArticleSerializer (DispatchModelSerializer ):
525
+ """Serializes articles for the Subsection model"""
526
+ id = serializers .ReadOnlyField (source = 'parent_id' )
527
+ authors = AuthorSerializer (many = True , read_only = True )
528
+
529
+ class Meta :
530
+ model = Article
531
+ fields = (
532
+ 'id' ,
533
+ 'headline' ,
534
+ 'authors' ,
535
+ 'published_version'
536
+ )
537
+
538
+ class SubsectionSerializer (DispatchModelSerializer ):
539
+ """Serializes the Subsection model"""
540
+
541
+ authors = AuthorSerializer (many = True , read_only = True )
542
+ author_ids = serializers .ListField (
543
+ write_only = True ,
544
+ child = serializers .JSONField (),
545
+ validators = [AuthorValidator ]
546
+ )
547
+ authors_string = serializers .CharField (source = 'get_author_string' , read_only = True )
548
+ articles = SubsectionArticleSerializer (many = True , read_only = True , source = 'get_articles' )
549
+ article_ids = serializers .ListField (
550
+ write_only = True ,
551
+ child = serializers .JSONField (),
552
+ required = False
553
+ )
554
+ section = SectionSerializer (read_only = True )
555
+ section_id = serializers .IntegerField (write_only = True )
556
+
557
+ slug = serializers .SlugField (validators = [SlugValidator ()])
558
+
559
+ class Meta :
560
+ model = Subsection
561
+ fields = (
562
+ 'id' ,
563
+ 'is_active' ,
564
+ 'name' ,
565
+ 'section' ,
566
+ 'section_id' ,
567
+ 'slug' ,
568
+ 'description' ,
569
+ 'authors' ,
570
+ 'author_ids' ,
571
+ 'authors_string' ,
572
+ 'articles' ,
573
+ 'article_ids'
574
+ )
575
+
576
+ def create (self , validated_data ):
577
+ instance = Subsection ()
578
+ return self .update (instance , validated_data )
579
+
580
+ def update (self , instance , validated_data ):
581
+ # Update basic fields
582
+ instance .name = validated_data .get ('name' , instance .name )
583
+ instance .slug = validated_data .get ('slug' , instance .slug )
584
+ instance .is_active = validated_data .get ('is_active' , instance .is_active )
585
+ instance .section_id = validated_data .get ('section_id' , instance .section_id )
586
+ instance .description = validated_data .get ('description' , instance .description )
587
+
588
+ # Save instance before processing/saving content in order to save associations to correct ID
589
+ instance .save ()
590
+
591
+ authors = validated_data .get ('author_ids' )
592
+
593
+ instance .save_authors (authors , is_publishable = False )
594
+
595
+ article_ids = validated_data .get ('article_ids' )
596
+
597
+ instance .save_articles (article_ids )
598
+
599
+ return instance
600
+
523
601
class ArticleSerializer (DispatchModelSerializer , DispatchPublishableSerializer ):
524
602
"""Serializes the Article model."""
525
603
@@ -529,6 +607,9 @@ class ArticleSerializer(DispatchModelSerializer, DispatchPublishableSerializer):
529
607
section = SectionSerializer (read_only = True )
530
608
section_id = serializers .IntegerField (write_only = True )
531
609
610
+ subsection = SubsectionSerializer (source = 'get_subsection' , read_only = True )
611
+ subsection_id = serializers .IntegerField (write_only = True , required = False , allow_null = True )
612
+
532
613
featured_image = ImageAttachmentSerializer (required = False , allow_null = True )
533
614
featured_video = VideoAttachmentSerializer (required = False , allow_null = True )
534
615
@@ -585,6 +666,8 @@ class Meta:
585
666
'authors_string' ,
586
667
'section' ,
587
668
'section_id' ,
669
+ 'subsection' ,
670
+ 'subsection_id' ,
588
671
'published_at' ,
589
672
'is_published' ,
590
673
'is_breaking' ,
@@ -642,7 +725,7 @@ def update(self, instance, validated_data):
642
725
instance .integrations = validated_data .get ('integrations' , instance .integrations )
643
726
instance .template = template
644
727
instance .template_data = template_data
645
-
728
+
646
729
instance .save ()
647
730
648
731
instance .content = validated_data .get ('content' , instance .content )
@@ -656,6 +739,7 @@ def update(self, instance, validated_data):
656
739
instance .save_featured_video (featured_video )
657
740
658
741
authors = validated_data .get ('author_ids' )
742
+
659
743
if authors :
660
744
instance .save_authors (authors , is_publishable = True )
661
745
@@ -666,6 +750,10 @@ def update(self, instance, validated_data):
666
750
if topic_id != False :
667
751
instance .save_topic (topic_id )
668
752
753
+ subsection_id = validated_data .get ('subsection_id' , None )
754
+ if subsection_id is not None :
755
+ instance .save_subsection (subsection_id )
756
+
669
757
# Perform a final save (without revision), update content and featured image
670
758
instance .save (
671
759
update_fields = ['content' , 'featured_image' , 'featured_video' , 'topic' ],
0 commit comments