<s> """<STR_LIT>""" <EOL> from django . core . exceptions import ValidationError <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , HttpResponse <EOL> from django . shortcuts import get_object_or_404 <EOL> from expedient . clearinghouse . aggregate . models import Aggregate <EOL> from expedient . clearinghouse . slice . models import Slice <EOL> from expedient . common . messaging . models import DatedMessage <EOL> from expedient . common . utils . plugins . plugincommunicator import * <EOL> from expedient . common . utils . plugins . resources . link import Link <EOL> from expedient . common . utils . plugins . resources . node import Node <EOL> from expedient . common . utils . views import generic_crud <EOL> from sample_resource . controller . resource import SampleResource as SampleResourceController <EOL> from sample_resource . forms . SampleResource import SampleResource as SampleResourceModelForm <EOL> from sample_resource . models import SampleResource as SampleResourceModel , SampleResourceAggregate as SampleResourceAggregateModel <EOL> import copy <EOL> import logging <EOL> import xmlrpclib <EOL> def create_resource ( request , slice_id , agg_id ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT:POST>" : <EOL> agg = Aggregate . objects . get ( id = agg_id ) <EOL> if agg . check_status ( ) == False : <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % agg . name , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR , ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) <EOL> if '<STR_LIT>' in request . POST : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ slice_id , agg_id ] ) ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) <EOL> def resource_crud ( request , slice_id , agg_id , resource_id = None ) : <EOL> """<STR_LIT>""" <EOL> slice = get_object_or_404 ( Slice , id = slice_id ) <EOL> aggregate = Aggregate . objects . get ( id = agg_id ) <EOL> error_crud = "<STR_LIT>" <EOL> def pre_save ( instance , created ) : <EOL> """<STR_LIT>""" <EOL> instance = SampleResourceController . fill ( instance , slice , agg_id , resource_id ) <EOL> try : <EOL> return generic_crud ( request , obj_id = resource_id , model = SampleResourceModel , <EOL> form_class = SampleResourceModelForm , <EOL> template = "<STR_LIT>" , <EOL> redirect = lambda inst : reverse ( "<STR_LIT>" , args = [ slice_id ] ) , <EOL> extra_context = { "<STR_LIT>" : aggregate , "<STR_LIT>" : slice , "<STR_LIT>" : error_crud , "<STR_LIT>" : ( <EOL> ( "<STR_LIT>" , reverse ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" % slice . project . name , reverse ( "<STR_LIT>" , args = [ slice . project . id ] ) ) , <EOL> ( "<STR_LIT>" % slice . name , reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) , <EOL> ( "<STR_LIT>" % "<STR_LIT>" if resource_id else "<STR_LIT>" , reverse ( "<STR_LIT>" , args = [ slice_id , agg_id ] ) ) , ) <EOL> } , extra_form_params = { } , template_object_name = "<STR_LIT:object>" , pre_save = pre_save , <EOL> post_save = None , success_msg = None ) <EOL> except ValidationError as e : <EOL> error_crud = "<STR_LIT:;>" . join ( e . messages ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % str ( e ) <EOL> DatedMessage . objects . post_message_to_user ( <EOL> "<STR_LIT>" % str ( e ) , <EOL> request . user , msg_type = DatedMessage . TYPE_ERROR ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ slice_id ] ) ) <EOL> def manage_resource ( request , resource_id , action_type ) : <EOL> """<STR_LIT>""" <EOL> if action_type == "<STR_LIT>" : <EOL> SampleResourceController . delete ( resource_id ) <EOL> return HttpResponse ( "<STR_LIT>" ) <EOL> def get_sr_list ( slice ) : <EOL> return SampleResourceModel . objects . filter ( slice_id = slice . uuid ) <EOL> def get_sr_aggregates ( slice ) : <EOL> sr_aggs = [ ] <EOL> try : <EOL> sr_aggs = slice . aggregates . filter ( leaf_name = SampleResourceAggregateModel . __name__ . lower ( ) ) <EOL> except : <EOL> pass <EOL> return sr_aggs <EOL> def get_node_description ( node ) : <EOL> description = "<STR_LIT>" + node . name + "<STR_LIT>" <EOL> description += "<STR_LIT>" % ( str ( node . get_temperature ( ) ) , str ( node . get_temperature_scale ( ) ) ) <EOL> connections = "<STR_LIT>" <EOL> node_connections = node . get_connections ( ) <EOL> for i , connection in enumerate ( node_connections ) : <EOL> connections += connection . name <EOL> if i < len ( node_connections ) - <NUM_LIT:1> : <EOL> connections += "<STR_LIT:U+002CU+0020>" <EOL> description += "<STR_LIT>" % str ( connections ) <EOL> return description <EOL> def get_nodes_links ( slice , chosen_group = None ) : <EOL> nodes = [ ] <EOL> links = [ ] <EOL> sr_aggs = get_sr_aggregates ( slice ) <EOL> try : <EOL> image_url = reverse ( '<STR_LIT>' , args = ( "<STR_LIT>" , ) ) <EOL> except : <EOL> image_url = '<STR_LIT>' <EOL> for i , sr_agg in enumerate ( sr_aggs ) : <EOL> sr_agg = sr_agg . sampleresourceaggregate <EOL> for sr in sr_agg . get_resources ( ) : <EOL> sr = sr . sampleresource <EOL> nodes . append ( Node ( <EOL> name = sr . name , value = sr . id , aggregate = sr . aggregate , type = "<STR_LIT>" , <EOL> description = get_node_description ( sr ) , image = image_url ) <EOL> ) <EOL> for connection in sr . get_connections ( ) : <EOL> links . append ( <EOL> Link ( <EOL> target = str ( sr . id ) , source = str ( connection . id ) , <EOL> value = "<STR_LIT>" % ( connection . id , sr . id ) <EOL> ) , <EOL> ) <EOL> links . append ( <EOL> Link ( <EOL> target = str ( sr . id ) , source = str ( connection . id ) , <EOL> value = "<STR_LIT>" % ( sr . id , connection . id ) <EOL> ) , <EOL> ) <EOL> return [ nodes , links ] <EOL> def get_ui_data ( slice ) : <EOL> """<STR_LIT>""" <EOL> ui_context = dict ( ) <EOL> try : <EOL> ui_context [ '<STR_LIT>' ] = get_sr_list ( slice ) <EOL> ui_context [ '<STR_LIT>' ] = get_sr_aggregates ( slice ) <EOL> ui_context [ '<STR_LIT>' ] , ui_context [ '<STR_LIT>' ] = get_nodes_links ( slice ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % str ( e ) <EOL> return ui_context </s>
