<s> import json <EOL> import logging <EOL> from django . db import transaction <EOL> from desktop . models import Document , DocumentPermission , DocumentTag , Document2 , Directory , Document2Permission , FilesystemException <EOL> from notebook . models import import_saved_beeswax_query <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class DocumentConverter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , user ) : <EOL> self . user = user <EOL> self . home_dir = Document2 . objects . create_user_directories ( self . user ) <EOL> self . imported_tag = DocumentTag . objects . get_imported2_tag ( user = self . user ) <EOL> self . imported_docs = [ ] <EOL> def convert ( self ) : <EOL> try : <EOL> from beeswax . models import SavedQuery , HQL , IMPALA , RDBMS <EOL> docs = self . _get_unconverted_docs ( SavedQuery ) . filter ( extra__in = [ HQL , IMPALA , RDBMS ] ) <EOL> for doc in docs : <EOL> if doc . content_object : <EOL> notebook = import_saved_beeswax_query ( doc . content_object ) <EOL> data = notebook . get_data ( ) <EOL> doc2 = self . _create_doc2 ( <EOL> document = doc , <EOL> doctype = data [ '<STR_LIT:type>' ] , <EOL> name = data [ '<STR_LIT:name>' ] , <EOL> description = data [ '<STR_LIT:description>' ] , <EOL> data = notebook . get_json ( ) <EOL> ) <EOL> self . imported_docs . append ( doc2 ) <EOL> except ImportError , e : <EOL> LOG . warn ( '<STR_LIT>' ) <EOL> try : <EOL> from oozie . models import Workflow <EOL> docs = self . _get_unconverted_docs ( Workflow ) <EOL> for doc in docs : <EOL> if doc . content_object : <EOL> data = doc . content_object . data_dict <EOL> data . update ( { '<STR_LIT>' : doc . content_type . model , '<STR_LIT>' : doc . object_id } ) <EOL> doc2 = self . _create_doc2 ( <EOL> document = doc , <EOL> doctype = '<STR_LIT>' , <EOL> description = doc . description , <EOL> data = json . dumps ( data ) <EOL> ) <EOL> self . imported_docs . append ( doc2 ) <EOL> except ImportError , e : <EOL> LOG . warn ( '<STR_LIT>' ) <EOL> try : <EOL> from pig . models import PigScript <EOL> docs = self . _get_unconverted_docs ( PigScript ) <EOL> for doc in docs : <EOL> if doc . content_object : <EOL> data = doc . content_object . dict <EOL> data . update ( { '<STR_LIT>' : doc . content_type . model , '<STR_LIT>' : doc . object_id } ) <EOL> doc2 = self . _create_doc2 ( <EOL> document = doc , <EOL> doctype = '<STR_LIT>' , <EOL> description = doc . description , <EOL> data = json . dumps ( data ) <EOL> ) <EOL> self . imported_docs . append ( doc2 ) <EOL> except ImportError , e : <EOL> LOG . warn ( '<STR_LIT>' ) <EOL> if self . imported_docs : <EOL> LOG . info ( '<STR_LIT>' % len ( self . imported_docs ) ) <EOL> def _get_unconverted_docs ( self , content_type ) : <EOL> docs = Document . objects . get_docs ( self . user , content_type ) . filter ( owner = self . user ) <EOL> return docs . exclude ( tags__in = [ <EOL> DocumentTag . objects . get_trash_tag ( user = self . user ) , <EOL> DocumentTag . objects . get_history_tag ( user = self . user ) , <EOL> DocumentTag . objects . get_example_tag ( user = self . user ) , <EOL> self . imported_tag <EOL> ] ) <EOL> def _get_parent_directory ( self , document ) : <EOL> """<STR_LIT>""" <EOL> parent_dir = self . home_dir <EOL> project_tags = document . tags . exclude ( tag__in = DocumentTag . RESERVED ) <EOL> if project_tags . exists ( ) : <EOL> first_tag = project_tags [ <NUM_LIT:0> ] <EOL> parent_dir , created = Directory . objects . get_or_create ( <EOL> owner = self . user , <EOL> name = first_tag . tag , <EOL> parent_directory = self . home_dir <EOL> ) <EOL> return parent_dir <EOL> def _sync_permissions ( self , document , document2 ) : <EOL> """<STR_LIT>""" <EOL> doc_permissions = DocumentPermission . objects . filter ( doc = document ) <EOL> for perm in doc_permissions : <EOL> doc2_permission , created = Document2Permission . objects . get_or_create ( doc = document2 , perms = perm . perms ) <EOL> if perm . users : <EOL> doc2_permission . users . add ( * perm . users . all ( ) ) <EOL> if perm . groups : <EOL> doc2_permission . groups . add ( * perm . groups . all ( ) ) <EOL> def _create_doc2 ( self , document , doctype , name = None , description = None , data = None ) : <EOL> with transaction . atomic ( ) : <EOL> document2 = Document2 . objects . create ( <EOL> owner = self . user , <EOL> parent_directory = self . _get_parent_directory ( document ) , <EOL> name = name if name else document . name , <EOL> type = doctype , <EOL> description = description , <EOL> data = data <EOL> ) <EOL> self . _sync_permissions ( document , document2 ) <EOL> document . add_tag ( self . imported_tag ) <EOL> document . save ( ) <EOL> return document2 </s>
