<s> from django . db import models <EOL> from django . contrib . auth . models import User , Group <EOL> from django . utils . safestring import mark_safe <EOL> import json <EOL> import markdown2 <EOL> SANDBOX_STATUS_CHOICES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> RELATED_MODEL_CHOICES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> DEPLOYMENT_ENVIRONMENTS = ( <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> ) <EOL> VM_STATES = ( <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> ) <EOL> class SshKey ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> user = models . ForeignKey ( User , blank = False , null = False , related_name = "<STR_LIT>" ) <EOL> key = models . TextField ( ) <EOL> def fingerprint ( self ) : <EOL> try : <EOL> pubkey = sshpubkeys . SSHKey ( self . key ) <EOL> return pubkey . hash ( ) <EOL> except : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> def keytype ( self ) : <EOL> try : <EOL> pubkey = sshpubkeys . SSHKey ( self . key ) <EOL> return "<STR_LIT>" % ( pubkey . key_type . decode ( '<STR_LIT:ascii>' ) , pubkey . bits ) <EOL> except : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> def isValid ( self ) : <EOL> try : <EOL> pubkey = sshpubkeys . SSHKey ( self . key ) <EOL> return True <EOL> except : <EOL> return False <EOL> class UserConfigManager ( models . Manager ) : <EOL> def findOrCreate_ProxyHttp ( self , user ) : <EOL> """<STR_LIT>""" <EOL> objs = super ( UserConfigManager , self ) . get_queryset ( ) . filter ( key = "<STR_LIT>" ) <EOL> if len ( objs ) > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> return objs [ <NUM_LIT:0> ] <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> obj = super ( UserConfigManager , self ) . create ( <EOL> label = "<STR_LIT>" , <EOL> key = "<STR_LIT>" , <EOL> val = "<STR_LIT>" , <EOL> user = user <EOL> ) <EOL> obj . save ( ) <EOL> return obj <EOL> def findOrCreate_ProxyHttps ( self , user ) : <EOL> """<STR_LIT>""" <EOL> objs = super ( UserConfigManager , self ) . get_queryset ( ) . filter ( key = "<STR_LIT>" ) <EOL> if len ( objs ) > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> return objs [ <NUM_LIT:0> ] <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> obj = super ( UserConfigManager , self ) . create ( <EOL> label = "<STR_LIT>" , <EOL> key = "<STR_LIT>" , <EOL> val = "<STR_LIT>" , <EOL> user = user <EOL> ) <EOL> obj . save ( ) <EOL> return obj <EOL> def findOrCreate_AwsSecret ( self , user ) : <EOL> """<STR_LIT>""" <EOL> objs = super ( UserConfigManager , self ) . get_queryset ( ) . filter ( key = "<STR_LIT>" ) <EOL> if len ( objs ) > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> return objs [ <NUM_LIT:0> ] <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> obj = super ( UserConfigManager , self ) . create ( <EOL> label = "<STR_LIT>" , <EOL> key = "<STR_LIT>" , <EOL> val = "<STR_LIT>" , <EOL> user = user <EOL> ) <EOL> obj . save ( ) <EOL> return obj <EOL> def findOrCreate_AwsKey ( self , user ) : <EOL> """<STR_LIT>""" <EOL> objs = super ( UserConfigManager , self ) . get_queryset ( ) . filter ( key = "<STR_LIT>" ) <EOL> if len ( objs ) > <NUM_LIT:0> : <EOL> print ( "<STR_LIT>" ) <EOL> return objs [ <NUM_LIT:0> ] <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> obj = super ( UserConfigManager , self ) . create ( <EOL> label = "<STR_LIT>" , <EOL> key = "<STR_LIT>" , <EOL> val = "<STR_LIT>" , <EOL> user = user <EOL> ) <EOL> obj . save ( ) <EOL> return obj <EOL> def hasAwsKey ( self ) : <EOL> objs = super ( UserConfigManager , self ) . get_queryset ( ) . filter ( key = "<STR_LIT>" ) . exclude ( val = "<STR_LIT>" ) <EOL> if len ( objs ) == <NUM_LIT:0> : <EOL> return False <EOL> else : <EOL> return True <EOL> class UserConfig ( models . Model ) : <EOL> label = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> key = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> val = models . TextField ( blank = True , null = True ) <EOL> user = models . ForeignKey ( User , blank = False , null = False , related_name = "<STR_LIT>" ) <EOL> objects = UserConfigManager ( ) <EOL> class TestNotification ( models . Model ) : <EOL> text = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> class Repository ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> uri = models . CharField ( max_length = <NUM_LIT> ) <EOL> creator = models . ForeignKey ( User , blank = False , null = False , related_name = "<STR_LIT>" ) <EOL> created_at = models . DateTimeField ( auto_now_add = True ) <EOL> class VMSandboxTag ( models . Model ) : <EOL> tag = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> class VMPlayground ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> creator = models . ForeignKey ( User , blank = False , null = False ) <EOL> created_at = models . DateTimeField ( auto_now_add = True ) <EOL> description = models . TextField ( blank = True , null = True ) <EOL> description_is_markdown = models . BooleanField ( null = False , default = False ) <EOL> environment = models . CharField ( max_length = <NUM_LIT:4> , choices = DEPLOYMENT_ENVIRONMENTS , blank = False , null = False , default = "<STR_LIT>" ) <EOL> access_groups = models . ManyToManyField ( Group , related_name = "<STR_LIT>" ) <EOL> access_users = models . ManyToManyField ( User , related_name = "<STR_LIT>" ) <EOL> def desc_html ( self ) : <EOL> if self . description_is_markdown : <EOL> fmt = markdown2 . markdown ( self . description , extras = [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> return mark_safe ( fmt ) <EOL> else : <EOL> return self . description <EOL> def getDemos ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ sandbox for sandbox in self . sandboxes . all ( ) if sandbox . is_demo ] <EOL> class VMSandbox ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:255> ) <EOL> logical_name = models . CharField ( max_length = <NUM_LIT:255> , blank = False , null = False ) <EOL> creator = models . ForeignKey ( User , blank = False , null = False ) <EOL> created_at = models . DateTimeField ( auto_now_add = True ) <EOL> playground = models . ForeignKey ( VMPlayground , related_name = "<STR_LIT>" ) <EOL> metadata = models . TextField ( blank = True , null = True ) <EOL> keys = models . ManyToManyField ( SshKey , related_name = "<STR_LIT>" ) <EOL> state = models . CharField ( max_length = <NUM_LIT:4> , choices = VM_STATES , blank = False , null = False , default = "<STR_LIT>" ) <EOL> tags = models . ManyToManyField ( VMSandboxTag , related_name = "<STR_LIT>" ) <EOL> is_demo = models . BooleanField ( blank = False , null = False , default = False ) <EOL> repository = models . ForeignKey ( Repository , blank = True , null = True , related_name = "<STR_LIT>" ) <EOL> def recipe ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _getJsonMeta ( '<STR_LIT>' ) <EOL> def sshKeyName ( self ) : <EOL> return self . _getJsonMeta ( '<STR_LIT>' ) <EOL> def privateIp ( self ) : <EOL> return self . _getJsonMeta ( '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> def privateDns ( self ) : <EOL> return self . _getJsonMeta ( '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> def awsInstanceId ( self ) : <EOL> return self . _getJsonMeta ( '<STR_LIT>' ) <EOL> def ami ( self ) : <EOL> return self . _getJsonMeta ( '<STR_LIT>' ) <EOL> def vpc ( self ) : <EOL> return self . _getJsonMeta ( '<STR_LIT>' ) <EOL> def instanceType ( self ) : <EOL> return self . _getJsonMeta ( '<STR_LIT:type>' ) <EOL> def subnet ( self ) : <EOL> return self . _getJsonMeta ( '<STR_LIT>' ) <EOL> def _getJsonMeta ( self , key ) : <EOL> try : <EOL> meta = json . loads ( self . metadata ) <EOL> return meta [ key ] <EOL> except : <EOL> return None <EOL> def isInAWS ( self ) : <EOL> return self . playground . environment in [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def isPausable ( self ) : <EOL> return self . state == '<STR_LIT>' <EOL> def isRebootable ( self ) : <EOL> return self . state == '<STR_LIT>' <EOL> def isStartable ( self ) : <EOL> return self . state == '<STR_LIT>' <EOL> def pause ( self ) : <EOL> self . state = "<STR_LIT>" <EOL> self . save ( ) <EOL> def reboot ( self ) : <EOL> self . state = "<STR_LIT>" <EOL> self . save ( ) <EOL> def start ( self ) : <EOL> self . state = "<STR_LIT>" <EOL> self . save ( ) <EOL> class Playground ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> owner = models . ForeignKey ( User , blank = False , null = False ) <EOL> description = models . TextField ( blank = True , null = True , default = "<STR_LIT>" ) <EOL> environment = models . CharField ( max_length = <NUM_LIT:4> , choices = DEPLOYMENT_ENVIRONMENTS , blank = False , null = False , default = "<STR_LIT>" ) <EOL> class PlaygroundRequest ( models . Model ) : <EOL> requested_by = models . ForeignKey ( User , related_name = "<STR_LIT>" , blank = False , null = False ) <EOL> created_at = models . DateTimeField ( auto_now_add = True ) <EOL> modified_at = models . DateTimeField ( auto_now = True , blank = True , null = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT:50> ) <EOL> justification = models . TextField ( ) <EOL> request_status = models . CharField ( max_length = <NUM_LIT:3> , choices = SANDBOX_STATUS_CHOICES , blank = False , null = False , default = "<STR_LIT>" ) <EOL> playground = models . ForeignKey ( Playground , blank = False , null = False ) <EOL> class SandboxTemplate ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( max_length = <NUM_LIT:100> , null = False , blank = False ) <EOL> recipe = models . CharField ( max_length = <NUM_LIT:100> , null = False , blank = False ) <EOL> version = models . CharField ( max_length = <NUM_LIT:50> , null = False , blank = True ) <EOL> description = models . TextField ( null = False , blank = True , default = "<STR_LIT>" ) <EOL> notes = models . TextField ( null = False , blank = True , default = "<STR_LIT>" ) <EOL> class Sandbox ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> owner = models . ForeignKey ( User , blank = False , null = False ) <EOL> playground = models . ForeignKey ( Playground , blank = False , null = False , related_name = "<STR_LIT>" ) <EOL> hostname = models . CharField ( max_length = <NUM_LIT:50> , blank = False , null = True ) <EOL> ip_address = models . CharField ( max_length = <NUM_LIT:15> , blank = False , null = True ) <EOL> url = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> class SandboxRequest ( models . Model ) : <EOL> requested_by = models . ForeignKey ( User , related_name = "<STR_LIT>" , blank = False , null = False ) <EOL> created_at = models . DateTimeField ( auto_now_add = True ) <EOL> modified_at = models . DateTimeField ( auto_now = True , blank = True , null = True ) <EOL> sandbox_name = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> justification = models . TextField ( null = False , blank = True ) <EOL> sandbox_lifetime = models . DateField ( null = False , blank = False , default = '<STR_LIT>' ) <EOL> request_status = models . CharField ( max_length = <NUM_LIT:3> , choices = SANDBOX_STATUS_CHOICES , blank = False , null = False , default = "<STR_LIT>" ) <EOL> sandbox = models . ForeignKey ( Sandbox , blank = False , null = False ) <EOL> template = models . ForeignKey ( SandboxTemplate , blank = False , null = True ) <EOL> request_progress = models . FloatField ( default = <NUM_LIT:0> ) <EOL> request_progress_msg = models . CharField ( max_length = <NUM_LIT:100> , default = '<STR_LIT>' ) <EOL> def isAWS ( self ) : <EOL> return len ( self . aws_options . all ( ) ) > <NUM_LIT:0> <EOL> class SandboxAWSOptions ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( max_length = <NUM_LIT:100> , null = False , blank = False ) <EOL> recipe = models . CharField ( max_length = <NUM_LIT:100> , null = False , blank = False ) <EOL> ami = models . CharField ( max_length = <NUM_LIT:100> , null = False , blank = False ) <EOL> vpc = models . CharField ( max_length = <NUM_LIT:100> , null = False , blank = False ) <EOL> json = models . TextField ( null = False , blank = False , default = "<STR_LIT>" ) <EOL> class NotificationManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def unreadForUser ( self , user_id ) : <EOL> from django . db import connection <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , ( user_id , ) ) <EOL> result_list = [ ] <EOL> for row in cursor . fetchall ( ) : <EOL> p = self . model ( <EOL> id = row [ <NUM_LIT:0> ] , <EOL> added_at = row [ <NUM_LIT:1> ] , <EOL> owner_id = row [ <NUM_LIT:2> ] , <EOL> read = row [ <NUM_LIT:3> ] , <EOL> msg = row [ <NUM_LIT:4> ] , <EOL> related_model_type = row [ <NUM_LIT:5> ] , <EOL> related_model_id = row [ <NUM_LIT:6> ] , <EOL> progress = row [ <NUM_LIT:7> ] , <EOL> object_name = row [ <NUM_LIT:8> ] , <EOL> error = row [ <NUM_LIT:9> ] <EOL> ) <EOL> result_list . append ( p ) <EOL> return result_list <EOL> def unreadCountForUser ( self , user_id ) : <EOL> from django . db import connection <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , ( user_id , ) ) <EOL> return cursor . fetchone ( ) [ <NUM_LIT:0> ] <EOL> def markAllReadForUser ( self , user_id ) : <EOL> from django . db import connection <EOL> cursor = connection . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , ( user_id , ) ) <EOL> connection . commit ( ) <EOL> class Notification ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> added_at = models . DateTimeField ( auto_now_add = True ) <EOL> owner = models . ForeignKey ( User , blank = False , null = False ) <EOL> read = models . BooleanField ( null = False , default = False ) <EOL> msg = models . TextField ( null = False , blank = True ) <EOL> related_model_type = models . CharField ( max_length = <NUM_LIT:3> , choices = RELATED_MODEL_CHOICES , blank = False , null = False ) <EOL> related_model_id = models . IntegerField ( ) <EOL> objects = NotificationManager ( ) <EOL> progress = models . FloatField ( default = <NUM_LIT:0> ) <EOL> object_name = models . CharField ( max_length = <NUM_LIT:50> , default = '<STR_LIT>' ) <EOL> error = models . BooleanField ( null = False , default = False ) <EOL> def isSandboxRequest ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . related_model_type == '<STR_LIT>' </s>
