1+ import itertools
12import os
23import uuid
34
1112from django .db .models import Avg , Count
1213from django .utils .translation import ugettext_lazy as _
1314from modeltranslation .utils import build_localized_fieldname
15+ from typing import Any
16+ from typing import Dict
17+ from typing import Iterable
1418
1519from orb import signals
1620from orb .analytics .models import UserLocationVisualization
@@ -151,7 +155,9 @@ def update_from_api(self, api_data):
151155
152156 import_user = get_import_user ()
153157
154- for attr , value in api_data .iteritems ():
158+ cleaned_api_data = clean_api_data (api_data , 'attribution' , 'description' , 'title' )
159+
160+ for attr , value in cleaned_api_data .iteritems ():
155161 setattr (self , attr , value )
156162
157163 self .update_user = import_user
@@ -172,6 +178,7 @@ def create_from_api(cls, api_data, peer=None):
172178 the Resource object created
173179
174180 """
181+
175182 resource_files = api_data .pop ('files' , [])
176183 languages = api_data .pop ('languages' , [])
177184 tags = api_data .pop ('tags' , [])
@@ -181,18 +188,37 @@ def create_from_api(cls, api_data, peer=None):
181188
182189 import_user = get_import_user ()
183190
184- resource = cls .resources .create (source_peer = peer , create_user = import_user , update_user = import_user , ** api_data )
191+ cleaned_api_data = clean_api_data (api_data , 'attribution' , 'description' , 'title' )
192+
193+ resource = cls .resources .create (
194+ source_peer = peer ,
195+ create_user = import_user ,
196+ update_user = import_user ,
197+ ** cleaned_api_data
198+ )
185199
186200 ResourceURL .objects .bulk_create ([
187- ResourceURL .from_url_data (resource , resource_url_data , import_user )
201+ ResourceURL .from_url_data (
202+ resource ,
203+ clean_api_data (resource_url_data , "description" , "title" ),
204+ import_user ,
205+ )
188206 for resource_url_data in resource_urls
189207 ] + [
190- ResourceURL .from_file_data (resource , resource_file_data , import_user )
208+ ResourceURL .from_file_data (
209+ resource ,
210+ clean_api_data (resource_file_data , "description" , "title" ),
211+ import_user ,
212+ )
191213 for resource_file_data in resource_files
192214 ])
193215
194216 for tag_data in tags :
195- ResourceTag .create_from_api_data (resource , tag_data ['tag' ], user = import_user )
217+ ResourceTag .create_from_api_data (
218+ resource ,
219+ clean_api_data (tag_data ['tag' ], "category" , "description" , "name" , "summary" ),
220+ user = import_user ,
221+ )
196222
197223 return resource
198224
@@ -668,6 +694,10 @@ def create_from_api_data(cls, resource, api_data, user=None):
668694 for field in category_fields
669695 }
670696
697+ for field in api_data .keys ():
698+ if field .startswith ("category" ):
699+ del api_data [field ]
700+
671701 category , created = Category .objects .get_or_create (name = category_name , defaults = category_name_translations )
672702
673703 api_data ['category' ] = category
@@ -903,3 +933,33 @@ def get_import_user():
903933 user .save ()
904934 return user
905935
936+
937+ def clean_api_data (data , * fields ):
938+ # type: (Dict[unicode, Any], Iterable[unicode]) -> Dict[unicode, Any]
939+ """
940+ Returns API resource data with unusable translations filtered
941+
942+ Args:
943+ data: original API data
944+ *fields: translated field names
945+
946+ Returns:
947+ same API data with unsupported translations dropped
948+
949+ """
950+ language_codes = [l [0 ].replace ('-' , '_' ) for l in settings .LANGUAGES ]
951+ supported_field_translations = [
952+ "{}_{}" .format (field , language )
953+ for (field , language ) in itertools .product (fields , language_codes )
954+ ]
955+ def allowed_field (test_field ):
956+ # type: (unicode) -> bool
957+ if test_field != 'id' or test_field in fields or test_field in supported_field_translations :
958+ return True
959+ return not any ([test_field .startswith (f ) for f in fields ])
960+
961+ return {
962+ key : value
963+ for key , value in data .items ()
964+ if allowed_field (key )
965+ }
0 commit comments