<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os <EOL> import logging <EOL> import shutil <EOL> import lxml . etree as ET <EOL> from django . contrib . auth import authenticate , login , logout <EOL> from django . http import HttpResponse <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from explorer . utils . cxml import Cxml <EOL> from explorer . utils . misc import Response <EOL> from explorer . utils . adapter import Adapter <EOL> from explorer . utils . collection import Collection <EOL> from explorer . utils . misc import ServerSettings <EOL> from explorer . utils . admin import ModuleAdmin <EOL> from explorer . utils . schema import get_schema , download_schema , add_schema <EOL> import explorer . utils . uploader as Uploader <EOL> logging . basicConfig ( level = logging . WARNING ) <EOL> @ csrf_exempt <EOL> def login_handler ( request ) : <EOL> """<STR_LIT>""" <EOL> session = ET . Element ( '<STR_LIT>' ) <EOL> if request . POST : <EOL> action = request . POST [ '<STR_LIT:action>' ] <EOL> if action == '<STR_LIT>' : <EOL> username = request . POST [ '<STR_LIT:username>' ] <EOL> password = request . POST [ '<STR_LIT:password>' ] <EOL> user = authenticate ( username = username , password = password ) <EOL> if user is not None and user . is_active : <EOL> login ( request , user ) <EOL> session . text = username <EOL> else : <EOL> return HttpResponse ( Response . error ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> else : <EOL> try : <EOL> if request . session . session_key is not None and request . session . session_key != '<STR_LIT>' : <EOL> session_dir = ServerSettings . session_path ( request . session . session_key ) <EOL> if os . path . exists ( session_dir ) : <EOL> logging . debug ( '<STR_LIT>' + session_dir ) <EOL> shutil . rmtree ( session_dir ) <EOL> logout ( request ) <EOL> except : <EOL> logging . exception ( "<STR_LIT>" ) <EOL> else : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> return HttpResponse ( Response . success ( action , '<STR_LIT>' , session ) ) <EOL> return HttpResponse ( Response . error ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> @ csrf_exempt <EOL> def session_handler ( request ) : <EOL> """<STR_LIT>""" <EOL> session = ET . Element ( '<STR_LIT>' ) <EOL> if request . user . is_authenticated ( ) : <EOL> session . text = request . user . username <EOL> return HttpResponse ( Response . success ( '<STR_LIT>' , '<STR_LIT>' , session ) ) <EOL> @ csrf_exempt <EOL> def upload_handler ( request ) : <EOL> """<STR_LIT>""" <EOL> mode = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logging . debug ( request . method + '<STR_LIT>' + mode ) <EOL> if not request . user . is_authenticated ( ) : <EOL> logging . warning ( '<STR_LIT>' ) <EOL> return HttpResponse ( Response . error ( mode , '<STR_LIT>' ) ) <EOL> if not ServerSettings . user_aware ( ) : <EOL> if not request . user . has_perm ( '<STR_LIT>' ) or not request . user . has_perm ( '<STR_LIT>' ) : <EOL> logging . warning ( '<STR_LIT>' ) <EOL> return HttpResponse ( Response . error ( mode , '<STR_LIT>' ) ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> directory = ServerSettings . session_path ( request . session . session_key ) <EOL> _file = Uploader . upload_file ( request . FILES [ '<STR_LIT>' ] , directory ) <EOL> if _file is not None : <EOL> module = ET . Element ( '<STR_LIT>' ) <EOL> module . text = _file <EOL> rval = Response . success ( '<STR_LIT>' , '<STR_LIT>' , xml = module ) <EOL> logging . debug ( rval ) <EOL> return HttpResponse ( rval ) <EOL> return HttpResponse ( Response . error ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> elif request . method == '<STR_LIT:GET>' : <EOL> if mode == '<STR_LIT>' : <EOL> filename = request . GET . get ( '<STR_LIT:file>' , '<STR_LIT>' ) <EOL> index = request . GET . get ( '<STR_LIT:index>' , '<STR_LIT>' ) <EOL> logging . info ( '<STR_LIT>' + filename + '<STR_LIT>' + index ) <EOL> success , response = Uploader . sync_file ( request . user . username , <EOL> request . session . session_key , <EOL> filename , index ) <EOL> if success : <EOL> return HttpResponse ( Response . success ( mode , '<STR_LIT>' ) ) <EOL> return HttpResponse ( Response . error ( mode , '<STR_LIT>' , xml = response ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> success , modules = Uploader . commit_files ( request . user . username , request . session . session_key ) <EOL> if success : <EOL> return HttpResponse ( Response . success ( '<STR_LIT>' , '<STR_LIT>' , modules ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> success , modules = Uploader . get_upload_files ( request . user . username , request . session . session_key ) <EOL> if success : <EOL> return HttpResponse ( Response . success ( mode , '<STR_LIT>' , modules ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> success , modules = Uploader . clear_upload_files ( request . user . username , request . session . session_key ) <EOL> if success : <EOL> return HttpResponse ( Response . success ( mode , '<STR_LIT>' , modules ) ) <EOL> return HttpResponse ( Response . error ( mode , '<STR_LIT>' ) ) <EOL> return render_to_response ( '<STR_LIT>' ) <EOL> def admin_handler ( request ) : <EOL> """<STR_LIT>""" <EOL> if not request . user . is_authenticated ( ) : <EOL> return HttpResponse ( Response . error ( None , '<STR_LIT>' ) ) <EOL> if request . method != '<STR_LIT:GET>' : <EOL> return HttpResponse ( Response . error ( None , '<STR_LIT>' ) ) <EOL> action = request . GET . get ( '<STR_LIT:action>' , '<STR_LIT>' ) <EOL> logging . info ( '<STR_LIT>' % ( action , request . user . username ) ) <EOL> if action in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> payload = request . GET . get ( '<STR_LIT>' , None ) <EOL> print ( str ( payload ) ) <EOL> ( rc , msg ) = ModuleAdmin . admin_action ( request . user . username , payload , action ) <EOL> if not rc : <EOL> return HttpResponse ( Response . error ( action , msg ) ) <EOL> if action == '<STR_LIT>' : <EOL> return HttpResponse ( Response . success ( action , msg ) ) <EOL> modules = ModuleAdmin . get_modules ( request . user . username ) <EOL> return HttpResponse ( Response . success ( action , '<STR_LIT>' , xml = modules ) ) <EOL> def request_handler ( request ) : <EOL> """<STR_LIT>""" <EOL> mode = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> reply_xml = None <EOL> logging . info ( '<STR_LIT>' % mode ) <EOL> if mode == '<STR_LIT>' : <EOL> reply_xml = Collection . list ( ) <EOL> elif mode == '<STR_LIT>' : <EOL> metadata = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> reply_xml = Collection . load ( request . user . username , metadata ) <EOL> if reply_xml is None : <EOL> return HttpResponse ( Response . error ( mode , '<STR_LIT>' ) ) <EOL> if isinstance ( reply_xml , str ) : <EOL> return HttpResponse ( Response . success ( mode , reply_xml ) ) <EOL> elif mode == '<STR_LIT>' : <EOL> metadata = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> payload = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logging . debug ( '<STR_LIT>' + metadata ) <EOL> logging . debug ( '<STR_LIT>' + payload ) <EOL> if not Collection . add ( metadata , payload ) : <EOL> return HttpResponse ( Response . error ( mode , '<STR_LIT>' ) ) <EOL> reply_xml = Collection . list ( ) <EOL> mode = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> metadata = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not Collection . remove ( metadata ) : <EOL> return HttpResponse ( Response . error ( mode , '<STR_LIT>' ) ) <EOL> reply_xml = Collection . list ( ) <EOL> mode = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> req = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> reply_xml = Adapter . gen_rpc ( request . user . username , req ) <EOL> if isinstance ( reply_xml , str ) : <EOL> return HttpResponse ( Response . success ( mode , reply_xml ) ) <EOL> elif mode in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> payload = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logging . debug ( '<STR_LIT>' + payload ) <EOL> reply_xml = Adapter . run_request ( request . user . username , payload ) <EOL> return HttpResponse ( Response . success ( mode , '<STR_LIT>' , reply_xml ) ) <EOL> def module_handler ( request ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( "<STR_LIT>" ) <EOL> lst = [ ] <EOL> if request . user . is_authenticated ( ) : <EOL> path = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> username = request . user . username <EOL> if path == '<STR_LIT:root>' : <EOL> modules = ModuleAdmin . get_modulelist ( username ) <EOL> modules . sort ( ) <EOL> path = '<STR_LIT>' <EOL> else : <EOL> modules = [ path . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] ] <EOL> for module in modules : <EOL> filename = ModuleAdmin . cxml_path ( username , module ) <EOL> if filename is not None : <EOL> logging . debug ( "<STR_LIT>" + filename ) <EOL> module = Cxml ( filename ) <EOL> nodes = module . get_lazy_node ( path ) <EOL> lst . extend ( [ ET . tostring ( node ) for node in nodes ] ) <EOL> else : <EOL> logging . error ( "<STR_LIT>" + module ) <EOL> logging . debug ( "<STR_LIT>" ) <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : lst } , RequestContext ( request ) ) <EOL> def schema_handler ( request ) : <EOL> '''<STR_LIT>''' <EOL> logging . debug ( "<STR_LIT>" ) <EOL> req = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> action = request . GET . get ( '<STR_LIT:action>' , '<STR_LIT>' ) <EOL> logging . debug ( '<STR_LIT>' % action ) <EOL> if not request . user . is_authenticated ( ) : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return HttpResponse ( Response . error ( action , '<STR_LIT>' ) ) <EOL> if action == '<STR_LIT>' : <EOL> return get_schema ( request , req ) <EOL> elif action == '<STR_LIT>' : <EOL> return get_schema ( request , req , all = True ) <EOL> elif action == '<STR_LIT>' : <EOL> return download_schema ( request , req ) <EOL> elif action == '<STR_LIT>' : <EOL> return add_schema ( request , req ) </s>
