Skip to content

Commit a3f3cdb

Browse files
committed
Black strings
1 parent d348470 commit a3f3cdb

File tree

1 file changed

+98
-95
lines changed

1 file changed

+98
-95
lines changed

contentcuration/contentcuration/utils/archive.py

Lines changed: 98 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
# TASKS
1616
################################################################################
1717

18-
def archive_channel_tree(channel_id, tree='main'):
18+
19+
def archive_channel_tree(channel_id, tree="main"):
1920
"""
2021
Convert the `tree`_tree of `channel_id` to JSON and save it to archives dir.
2122
"""
2223
channel = Channel.objects.get(id=channel_id)
2324
# 1. serialize tree
24-
root = getattr(channel, tree + '_tree')
25+
root = getattr(channel, tree + "_tree")
2526
tree_serializer = ContentNodeArchiveSerializer(root)
2627
tree_data = tree_serializer.data
2728

@@ -31,31 +32,28 @@ def archive_channel_tree(channel_id, tree='main'):
3132

3233
# 3. manually transplant attributes from tree root node onto channel node
3334
# TODO: review if all these are necessay and archive-worthy
34-
channel_data['children'] = tree_data['children']
35-
channel_data['tree_name'] = tree + '_tree' # to know what we're archiving
36-
channel_data['tree_id'] = tree_data['tree_id'] # to know what we're archiving
37-
channel_data['created'] = tree_data['created']
38-
channel_data['modified'] = tree_data['modified']
39-
channel_data['extra_fields'] = tree_data['extra_fields']
40-
channel_data['publishing'] = tree_data['publishing']
41-
channel_data['published'] = tree_data['published']
42-
channel_data['complete'] = tree_data['complete']
43-
channel_data['changed'] = tree_data['changed']
44-
channel_data['freeze_authoring_data'] = tree_data['freeze_authoring_data']
35+
channel_data["children"] = tree_data["children"]
36+
channel_data["tree_name"] = tree + "_tree" # to know what we're archiving
37+
channel_data["tree_id"] = tree_data["tree_id"] # to know what we're archiving
38+
channel_data["created"] = tree_data["created"]
39+
channel_data["modified"] = tree_data["modified"]
40+
channel_data["extra_fields"] = tree_data["extra_fields"]
41+
channel_data["publishing"] = tree_data["publishing"]
42+
channel_data["published"] = tree_data["published"]
43+
channel_data["complete"] = tree_data["complete"]
44+
channel_data["changed"] = tree_data["changed"]
45+
channel_data["freeze_authoring_data"] = tree_data["freeze_authoring_data"]
4546

4647
# 4. dict -> json
4748
tree_data_json_str = json.dumps(channel_data, indent=4, ensure_ascii=False)
4849

4950
# 5. save dat
5051
archive_time = datetime.now().strftime("%Y-%m-%d__%H%M")
51-
filename_ext = channel_id + '_' + tree + '_' + archive_time + '.json'
52+
filename_ext = channel_id + "_" + tree + "_" + archive_time + ".json"
5253
save_to_path = tmpcontent_write(filename_ext, tree_data_json_str)
5354
return save_to_path
5455

5556

56-
57-
58-
5957
# ARCHIVAL SERIALIZERS
6058
################################################################################
6159

@@ -69,63 +67,65 @@ def archive_channel_tree(channel_id, tree='main'):
6967

7068
NODE_ATTRIBUTES = [
7169
# ids
72-
'kind_id',
73-
'id',
74-
'source_domain',
75-
'source_id',
76-
'content_id',
77-
'node_id',
70+
"kind_id",
71+
"id",
72+
"source_domain",
73+
"source_id",
74+
"content_id",
75+
"node_id",
7876
# data
79-
'title',
80-
'description',
81-
'language',
82-
'author',
83-
'aggregator',
84-
'provider',
85-
'thumbnail_encoding',
77+
"title",
78+
"description",
79+
"language",
80+
"author",
81+
"aggregator",
82+
"provider",
83+
"thumbnail_encoding",
8684
# licensing metadata
87-
'license_id',
88-
'license_description',
89-
'copyright_holder',
85+
"license_id",
86+
"license_description",
87+
"copyright_holder",
9088
# domain-specific metadata
91-
'role_visibility',
89+
"role_visibility",
9290
# content provenance
93-
'original_node_id',
94-
'cloned_source_id',
95-
'original_channel_id',
96-
'source_channel_id',
97-
'original_source_node_id',
98-
'source_node_id',
91+
"original_node_id",
92+
"cloned_source_id",
93+
"original_channel_id",
94+
"source_channel_id",
95+
"original_source_node_id",
96+
"source_node_id",
9997
# workflows
100-
'publishing',
101-
'published',
102-
'complete',
103-
'changed',
104-
'freeze_authoring_data', # needed?
98+
"publishing",
99+
"published",
100+
"complete",
101+
"changed",
102+
"freeze_authoring_data", # needed?
105103
# structural
106-
'parent_id',
107-
'sort_order',
104+
"parent_id",
105+
"sort_order",
108106
# via MPTTModel
109-
'tree_id',
110-
'level', # TODO: remove me (info not neeeded)
111-
'lft', 'rght', # TODO: remove me (info not neeeded)
107+
"tree_id",
108+
"level", # TODO: remove me (info not neeeded)
109+
"lft",
110+
"rght", # TODO: remove me (info not neeeded)
112111
# timestamps
113-
'created',
114-
'modified',
112+
"created",
113+
"modified",
115114
# kind-specific extended attributes
116-
'extra_fields',
115+
"extra_fields",
117116
]
118117

119118
NODE_RELATIONS = [
120-
'children',
121-
'files',
122-
'assessment_items',
119+
"children",
120+
"files",
121+
"assessment_items",
123122
]
124123

125124

126125
# copied from
127126
# https://github.com/learningequality/studio/blob/develop/contentcuration/contentcuration/viewsets/file.py#L74-L95
128127

128+
129129
class FileArchiveSerializer(FileSerializer):
130130
class Meta:
131131
model = File
@@ -155,6 +155,7 @@ class Meta:
155155
# copied from
156156
# https://github.com/learningequality/studio/blob/develop/contentcuration/contentcuration/viewsets/assessmentitem.py#L202-L218
157157

158+
158159
class AssessmentItemArchiveSerializer(AssessmentItemSerializer):
159160
class Meta:
160161
model = AssessmentItem
@@ -175,10 +176,12 @@ class Meta:
175176
"contentnode": "contentnode_id",
176177
}
177178

179+
178180
class ContentNodeArchiveSerializer(serializers.ModelSerializer):
179181
"""
180182
This is a read-only content node serializer used for channel archiving.
181183
"""
184+
182185
files = FileArchiveSerializer(many=True)
183186
assessment_items = AssessmentItemArchiveSerializer(many=True)
184187
# TODO: finish all fields (reusing existing serializers as much as possible)
@@ -192,50 +195,51 @@ class Meta:
192195

193196
def get_fields(self):
194197
fields = super(ContentNodeArchiveSerializer, self).get_fields()
195-
fields['children'] = ContentNodeArchiveSerializer(many=True)
198+
fields["children"] = ContentNodeArchiveSerializer(many=True)
196199
return fields
197200

198201

199-
200202
CHANNEL_ATTRIBUTES = [
201-
'id',
202-
'name',
203-
'description',
204-
'tagline',
205-
'version',
206-
'thumbnail',
207-
'thumbnail_encoding',
208-
'language_id',
209-
'trash_tree_id',
210-
'clipboard_tree_id',
211-
'main_tree_id',
212-
'staging_tree_id',
213-
'chef_tree_id',
214-
'previous_tree_id',
215-
'deleted',
216-
'public',
217-
'preferences',
218-
'content_defaults',
219-
'priority',
220-
'last_published',
221-
'source_url',
222-
'demo_server_url',
223-
'source_id',
224-
'source_domain',
225-
'ricecooker_version',
226-
'published_data',
227-
'icon_encoding',
228-
'total_resource_count',
229-
'published_kind_count',
230-
'published_size',
203+
"id",
204+
"name",
205+
"description",
206+
"tagline",
207+
"version",
208+
"thumbnail",
209+
"thumbnail_encoding",
210+
"language_id",
211+
"trash_tree_id",
212+
"clipboard_tree_id",
213+
"main_tree_id",
214+
"staging_tree_id",
215+
"chef_tree_id",
216+
"previous_tree_id",
217+
"deleted",
218+
"public",
219+
"preferences",
220+
"content_defaults",
221+
"priority",
222+
"last_published",
223+
"source_url",
224+
"demo_server_url",
225+
"source_id",
226+
"source_domain",
227+
"ricecooker_version",
228+
"published_data",
229+
"icon_encoding",
230+
"total_resource_count",
231+
"published_kind_count",
232+
"published_size",
231233
]
232234

233235
CHANNEL_RELATIONS = []
234236

237+
235238
class ChannelMetadataArchiveSerializer(serializers.ModelSerializer):
236239
"""
237240
This is a read-only channel metadata serializer used for channel archiving.
238241
"""
242+
239243
# TODO: finish all fields
240244
# editors?
241245
# viewers?
@@ -246,25 +250,24 @@ class Meta:
246250
fields = CHANNEL_ATTRIBUTES + CHANNEL_RELATIONS
247251

248252

249-
250-
251253
# SAVE
252254
################################################################################
253255

254-
settings_ARCHIVES_ROOT = 'tmpcontent/archives' # TODO: move me to GCP bucket
255-
# i'm thinking archives could be
256-
# a sibling of content/databases
257-
# and content/storage
256+
settings_ARCHIVES_ROOT = "tmpcontent/archives"
257+
# TODO: move me to GCP bucket. This dir could be a sibling of content/databases
258+
# and content/storage, like content/archives/jsontrees/{channel_id}/?/?.json
259+
258260
if not os.path.exists(settings_ARCHIVES_ROOT):
259261
os.makedirs(settings_ARCHIVES_ROOT, exist_ok=True)
260262

261263

262264
def tmpcontent_write(filename_ext, jsondata):
263265
save_to_path = os.path.join(settings_ARCHIVES_ROOT, filename_ext)
264-
with open(save_to_path, 'w') as outf:
266+
with open(save_to_path, "w") as outf:
265267
outf.write(jsondata)
266268
return save_to_path
267269

270+
268271
# TODO (continued): replace tmpcontent_write; sample code below
269272
# def write(self, *args, **kwargs):
270273
# try:

0 commit comments

Comments
 (0)