<s> import json <EOL> from celery import task <EOL> import github <EOL> from django . db import transaction <EOL> from ide . models . user import User <EOL> from ide . models . project import Project <EOL> from ide . models . files import SourceFile , ResourceFile , ResourceIdentifier , ResourceVariant <EOL> from ide . utils . sdk import dict_to_pretty_json <EOL> from ide . utils import generate_half_uuid <EOL> from utils . td_helper import send_td_event <EOL> import urllib2 <EOL> @ task ( acks_late = True ) <EOL> def import_gist ( user_id , gist_id ) : <EOL> user = User . objects . get ( pk = user_id ) <EOL> g = github . Github ( ) <EOL> try : <EOL> gist = g . get_gist ( gist_id ) <EOL> except github . UnknownObjectException : <EOL> send_td_event ( '<STR_LIT>' , data = { '<STR_LIT:data>' : { '<STR_LIT>' : gist_id } } , user = user ) <EOL> raise Exception ( "<STR_LIT>" ) <EOL> files = gist . files <EOL> default_name = gist . description or '<STR_LIT>' <EOL> project_type = '<STR_LIT>' <EOL> if '<STR_LIT>' in files : <EOL> settings = json . loads ( files [ '<STR_LIT>' ] . content ) <EOL> if '<STR_LIT>' in settings : <EOL> project_type = settings [ '<STR_LIT>' ] <EOL> elif len ( files ) == <NUM_LIT:2> : <EOL> if '<STR_LIT>' in files : <EOL> project_type = '<STR_LIT>' <EOL> elif '<STR_LIT>' in files : <EOL> project_type = '<STR_LIT>' <EOL> else : <EOL> settings = { } <EOL> if len ( files ) == <NUM_LIT:1> : <EOL> if '<STR_LIT>' in files : <EOL> project_type = '<STR_LIT>' <EOL> elif '<STR_LIT>' in files : <EOL> project_type = '<STR_LIT>' <EOL> project_settings = { <EOL> '<STR_LIT:name>' : settings . get ( '<STR_LIT>' , default_name ) , <EOL> '<STR_LIT>' : user , <EOL> '<STR_LIT>' : generate_half_uuid ( ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , default_name ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , default_name ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , user . username ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , '<STR_LIT:1.0>' ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : '<STR_LIT:U+002C>' . join ( settings . get ( '<STR_LIT>' , [ ] ) ) , <EOL> '<STR_LIT>' : dict_to_pretty_json ( settings . get ( '<STR_LIT>' , { } ) ) , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , False ) , <EOL> '<STR_LIT>' : project_type , <EOL> '<STR_LIT>' : settings . get ( '<STR_LIT>' , '<STR_LIT:2>' ) , <EOL> } <EOL> with transaction . atomic ( ) : <EOL> project = Project . objects . create ( ** project_settings ) <EOL> if project_type != '<STR_LIT>' : <EOL> for filename in gist . files : <EOL> if ( project_type == '<STR_LIT>' and filename . endswith ( '<STR_LIT>' ) or filename . endswith ( '<STR_LIT>' ) ) or filename . endswith ( '<STR_LIT>' ) : <EOL> if filename == '<STR_LIT>' : <EOL> cp_filename = '<STR_LIT>' <EOL> else : <EOL> cp_filename = filename <EOL> source_file = SourceFile . objects . create ( project = project , file_name = cp_filename ) <EOL> source_file . save_file ( gist . files [ filename ] . content ) <EOL> media = settings . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , [ ] ) <EOL> resources = { } <EOL> for resource in media : <EOL> kind = resource [ '<STR_LIT:type>' ] <EOL> def_name = resource [ '<STR_LIT:name>' ] <EOL> filename = resource [ '<STR_LIT:file>' ] <EOL> regex = resource . get ( '<STR_LIT>' , None ) <EOL> tracking = resource . get ( '<STR_LIT>' , None ) <EOL> memory_format = resource . get ( '<STR_LIT>' , None ) <EOL> storage_format = resource . get ( '<STR_LIT>' , None ) <EOL> space_optimisation = resource . get ( '<STR_LIT>' , None ) <EOL> is_menu_icon = resource . get ( '<STR_LIT>' , False ) <EOL> compatibility = resource . get ( '<STR_LIT>' , None ) <EOL> if filename not in gist . files : <EOL> continue <EOL> if filename not in resources : <EOL> resources [ filename ] = ResourceFile . objects . create ( project = project , file_name = filename , kind = kind , <EOL> is_menu_icon = is_menu_icon ) <EOL> default_variant = ResourceVariant . objects . create ( resource_file = resources [ filename ] , tags = ResourceVariant . TAGS_DEFAULT ) <EOL> default_variant . save_file ( urllib2 . urlopen ( gist . files [ filename ] . raw_url ) ) <EOL> ResourceIdentifier . objects . create ( <EOL> resource_file = resources [ filename ] , <EOL> resource_id = def_name , <EOL> character_regex = regex , <EOL> tracking = tracking , <EOL> compatibility = compatibility , <EOL> memory_format = memory_format , <EOL> storage_format = storage_format , <EOL> space_optimisation = space_optimisation <EOL> ) <EOL> else : <EOL> source_file = SourceFile . objects . create ( project = project , file_name = '<STR_LIT>' ) <EOL> source_file . save_file ( gist . files [ '<STR_LIT>' ] . content ) <EOL> send_td_event ( '<STR_LIT>' , data = { '<STR_LIT:data>' : { '<STR_LIT>' : gist_id } } , project = project ) <EOL> return project . id </s>
