<s> from __future__ import absolute_import , unicode_literals <EOL> from collections import defaultdict <EOL> import datetime as dt <EOL> from django . views . generic import DetailView , TemplateView , View <EOL> from django . template . loader import render_to_string <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . core . urlresolvers import reverse <EOL> from django . conf import settings <EOL> from django . forms import FileField <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import force_text <EOL> from django . template import RequestContext <EOL> from django . contrib . auth import get_user_model <EOL> from django . http import Http404 <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from . . backend import utils <EOL> from . . models import WooeyJob , Script , UserFile , Favorite , ScriptVersion <EOL> from . . import settings as wooey_settings <EOL> from . . django_compat import JsonResponse <EOL> class WooeyScriptBase ( DetailView ) : <EOL> model = Script <EOL> slug_field = '<STR_LIT>' <EOL> slug_url_kwarg = '<STR_LIT>' <EOL> @ staticmethod <EOL> def render_fn ( s ) : <EOL> return s <EOL> def get_object ( self , queryset = None ) : <EOL> script_version = self . kwargs . get ( '<STR_LIT>' ) <EOL> script_iteration = self . kwargs . get ( '<STR_LIT>' ) <EOL> if script_version is not None : <EOL> if queryset is None : <EOL> queryset = self . get_queryset ( ) <EOL> slug = self . kwargs . get ( self . slug_url_kwarg , None ) <EOL> if slug is not None : <EOL> slug_field = self . get_slug_field ( ) <EOL> queryset = queryset . filter ( ** { slug_field : slug , '<STR_LIT>' : script_version } ) <EOL> if script_iteration : <EOL> queryset . filter ( script_iteration = script_iteration ) <EOL> else : <EOL> queryset . latest ( '<STR_LIT>' ) <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . __class__ . __name__ ) <EOL> try : <EOL> obj = queryset . get ( ) <EOL> except queryset . model . DoesNotExist : <EOL> raise Http404 ( _ ( "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : queryset . model . _meta . verbose_name } ) <EOL> return obj <EOL> else : <EOL> return super ( WooeyScriptBase , self ) . get_object ( queryset = queryset ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( WooeyScriptBase , self ) . get_context_data ( ** kwargs ) <EOL> job_id = self . kwargs . get ( '<STR_LIT>' ) <EOL> initial = defaultdict ( list ) <EOL> if job_id : <EOL> job = WooeyJob . objects . get ( pk = job_id ) <EOL> if job . user is None or ( self . request . user . is_authenticated ( ) and job . user == self . request . user ) : <EOL> context [ '<STR_LIT>' ] = { '<STR_LIT>' : job_id , '<STR_LIT:url>' : job . get_resubmit_url ( ) , '<STR_LIT>' : job . script_version . script . get_url ( ) } <EOL> for i in job . get_parameters ( ) : <EOL> value = i . value <EOL> if value is not None : <EOL> initial [ i . parameter . slug ] . append ( value ) <EOL> context [ '<STR_LIT>' ] = utils . get_form_groups ( script_version = self . object . latest_version , initial_dict = initial , render_fn = self . render_fn , pk = self . object . pk ) <EOL> return context <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> post = request . POST . copy ( ) <EOL> user = request . user if request . user . is_authenticated ( ) else None <EOL> if not wooey_settings . WOOEY_ALLOW_ANONYMOUS and user is None : <EOL> return { '<STR_LIT>' : False , '<STR_LIT>' : { '<STR_LIT>' : [ force_text ( _ ( '<STR_LIT>' ) ) ] } } <EOL> form = utils . get_master_form ( pk = post [ '<STR_LIT>' ] ) <EOL> utils . validate_form ( form = form , data = post , files = request . FILES ) <EOL> to_delete = [ ] <EOL> for i in post : <EOL> if isinstance ( form . fields . get ( i ) , FileField ) : <EOL> new_values = list ( filter ( lambda x : x , post . getlist ( i ) ) ) <EOL> cleaned_values = [ ] <EOL> for new_value in new_values : <EOL> if i not in request . FILES and ( i not in form . cleaned_data or ( new_value and ( form . cleaned_data [ i ] is None or not [ j for j in form . cleaned_data [ i ] if j ] ) ) ) : <EOL> if new_value is not None : <EOL> cleaned_values . append ( utils . get_storage ( local = False ) . open ( new_value ) ) <EOL> to_delete . append ( i ) <EOL> if cleaned_values : <EOL> form . cleaned_data [ i ] = cleaned_values <EOL> for i in to_delete : <EOL> if i in form . errors : <EOL> del form . errors [ i ] <EOL> for i in request . FILES : <EOL> v = request . FILES . getlist ( i ) <EOL> if i in form . cleaned_data : <EOL> cleaned = form . cleaned_data [ i ] <EOL> cleaned = cleaned if isinstance ( cleaned , list ) else [ cleaned ] <EOL> form . cleaned_data [ i ] = list ( set ( cleaned ) . union ( set ( v ) ) ) <EOL> if not form . errors : <EOL> version_pk = form . cleaned_data . get ( '<STR_LIT>' ) <EOL> script_version = ScriptVersion . objects . get ( pk = version_pk ) <EOL> valid = utils . valid_user ( script_version . script , request . user ) . get ( '<STR_LIT>' ) <EOL> if valid is True : <EOL> group_valid = utils . valid_user ( script_version . script . script_group , request . user ) . get ( '<STR_LIT>' ) <EOL> if valid is True and group_valid is True : <EOL> job = utils . create_wooey_job ( script_version_pk = version_pk , user = user , data = form . cleaned_data ) <EOL> job . submit_to_celery ( ) <EOL> return { '<STR_LIT>' : True , '<STR_LIT>' : job . id } <EOL> return { '<STR_LIT>' : False , '<STR_LIT>' : { '<STR_LIT>' : [ force_text ( _ ( '<STR_LIT>' ) ) ] } } <EOL> return { '<STR_LIT>' : False , '<STR_LIT>' : form . errors } <EOL> class WooeyScriptJSON ( WooeyScriptBase ) : <EOL> @ staticmethod <EOL> def render_fn ( form ) : <EOL> return form . as_table ( ) <EOL> def render_to_response ( self , context , * args , ** kwargs ) : <EOL> return JsonResponse ( context ) <EOL> def post ( self , * args , ** kwargs ) : <EOL> data = super ( WooeyScriptJSON , self ) . post ( * args , ** kwargs ) <EOL> return JsonResponse ( data ) <EOL> class WooeyScriptView ( WooeyScriptBase ) : <EOL> template_name = '<STR_LIT>' <EOL> def post ( self , * args , ** kwargs ) : <EOL> data = super ( WooeyScriptView , self ) . post ( * args , ** kwargs ) <EOL> if data [ '<STR_LIT>' ] : <EOL> data [ '<STR_LIT>' ] = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : data [ '<STR_LIT>' ] } ) <EOL> return JsonResponse ( data ) <EOL> class WooeyHomeView ( TemplateView ) : <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( WooeyHomeView , self ) . get_context_data ( ** kwargs ) <EOL> ctx [ '<STR_LIT>' ] = utils . get_current_scripts ( ) <EOL> if self . request . user . is_authenticated ( ) : <EOL> ctype = ContentType . objects . get_for_model ( Script ) <EOL> ctx [ '<STR_LIT>' ] = Favorite . objects . filter ( content_type = ctype , user__id = self . request . user . id ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> else : <EOL> ctx [ '<STR_LIT>' ] = [ ] <EOL> return ctx <EOL> class WooeyProfileView ( TemplateView ) : <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( WooeyProfileView , self ) . get_context_data ( ** kwargs ) <EOL> if '<STR_LIT:username>' in self . kwargs : <EOL> user = get_user_model ( ) <EOL> ctx [ '<STR_LIT>' ] = user . objects . get ( username = self . kwargs . get ( '<STR_LIT:username>' ) ) <EOL> else : <EOL> if self . request . user and self . request . user . is_authenticated ( ) : <EOL> ctx [ '<STR_LIT>' ] = self . request . user <EOL> return ctx <EOL> class WooeyScrapbookView ( TemplateView ) : <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self , ** kwargs ) : <EOL> ctx = super ( WooeyScrapbookView , self ) . get_context_data ( ** kwargs ) <EOL> ctype = ContentType . objects . get_for_model ( UserFile ) <EOL> favorite_file_ids = Favorite . objects . filter ( content_type = ctype , user = self . request . user ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> out_files = utils . get_file_previews_by_ids ( favorite_file_ids ) <EOL> all = out_files . pop ( '<STR_LIT:all>' , [ ] ) <EOL> archives = out_files . pop ( '<STR_LIT>' , [ ] ) <EOL> ctx [ '<STR_LIT>' ] = out_files <EOL> ctx [ '<STR_LIT>' ] = favorite_file_ids <EOL> return ctx <EOL> class WooeySearchBase ( View ) : <EOL> model = None <EOL> search_fields = [ ] <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> self . search_results = None <EOL> if '<STR_LIT:q>' in request . GET : <EOL> query_string = request . GET [ '<STR_LIT:q>' ] . strip ( ) <EOL> query = utils . get_query ( query_string , self . search_fields ) <EOL> self . search_results = self . model . objects . filter ( query ) <EOL> return self . search ( request , * args , ** kwargs ) <EOL> class WooeyScriptSearchBase ( WooeySearchBase ) : <EOL> model = Script <EOL> search_fields = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class WooeyScriptSearchJSON ( WooeyScriptSearchBase ) : <EOL> """<STR_LIT>""" <EOL> def search ( self , request ) : <EOL> results = [ ] <EOL> for script in self . search_results : <EOL> results . append ( { <EOL> '<STR_LIT:id>' : script . id , <EOL> '<STR_LIT:name>' : script . script_name , <EOL> '<STR_LIT:description>' : script . script_description , <EOL> '<STR_LIT:url>' : reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : script . slug } ) , <EOL> } ) <EOL> return JsonResponse ( { '<STR_LIT>' : results } ) <EOL> class WooeyScriptSearchJSONHTML ( WooeyScriptSearchBase ) : <EOL> """<STR_LIT>""" <EOL> def search ( self , request ) : <EOL> results = [ ] <EOL> for script in self . search_results : <EOL> results . append ( render_to_string ( '<STR_LIT>' , { '<STR_LIT>' : script } , context_instance = RequestContext ( request ) ) ) <EOL> return JsonResponse ( { '<STR_LIT>' : results } ) </s>
