<s> from piston . handler import BaseHandler , rc <EOL> from systems . models import System , SystemRack , SystemStatus , NetworkAdapter , KeyValue , ScheduledTask <EOL> from truth . models import Truth , KeyValue as TruthKeyValue <EOL> from dhcp . DHCP import DHCP as DHCPInterface <EOL> from dhcp . models import DHCP <EOL> from MacroExpansion import MacroExpansion <EOL> from KeyValueTree import KeyValueTree <EOL> import re <EOL> try : <EOL> import json <EOL> except : <EOL> from django . utils import simplejson as json <EOL> from django . test . client import RequestFactory <EOL> from settings import API_ACCESS <EOL> from api_v2 . keyvalue_handler import KeyValueHandler <EOL> factory = RequestFactory ( ) <EOL> class DHCPHandler ( BaseHandler ) : <EOL> allowed_methods = API_ACCESS <EOL> exclude = ( ) <EOL> def read ( self , request , dhcp_scope = None , dhcp_action = None ) : <EOL> if dhcp_scope and dhcp_action == '<STR_LIT>' : <EOL> tasks = [ ] <EOL> for task in ScheduledTask . objects . get_all_dhcp ( ) : <EOL> tasks . append ( task . task ) <EOL> return tasks <EOL> if dhcp_scope and dhcp_action == '<STR_LIT>' : <EOL> truths = Truth . objects . select_related ( ) . filter ( keyvalue__key = '<STR_LIT>' , keyvalue__value = '<STR_LIT:True>' ) . order_by ( '<STR_LIT:name>' ) <EOL> truth_list = [ ] <EOL> for t in truths : <EOL> truth_list . append ( { '<STR_LIT:name>' : t . name . strip ( ) , '<STR_LIT:description>' : t . description . strip ( ) } ) <EOL> return truth_list <EOL> if dhcp_scope and dhcp_action == '<STR_LIT>' : <EOL> scope_options = [ ] <EOL> h = KeyValueHandler ( ) <EOL> request = factory . get ( '<STR_LIT>' % <EOL> dhcp_scope , follow = True ) <EOL> hosts = h . read ( request ) <EOL> adapter_list = [ ] <EOL> for host in hosts : <EOL> if '<STR_LIT>' in host : <EOL> the_url = '<STR_LIT>' % ( dhcp_scope , host [ '<STR_LIT>' ] ) <EOL> try : <EOL> request = factory . get ( '<STR_LIT>' <EOL> % ( dhcp_scope , host [ '<STR_LIT>' ] ) ) <EOL> adapter_list . append ( h . read ( request ) ) <EOL> except : <EOL> pass <EOL> d = DHCPInterface ( scope_options , adapter_list ) <EOL> return d . get_hosts ( ) <EOL> def create ( self , request , dhcp_scope = None , dhcp_action = None ) : <EOL> if dhcp_scope and dhcp_action == '<STR_LIT>' : <EOL> try : <EOL> task = ScheduledTask ( type = '<STR_LIT>' , task = dhcp_scope ) <EOL> task . save ( ) <EOL> except Exception , e : <EOL> pass <EOL> return rc . ALL_OK <EOL> else : <EOL> return rc . NOT_FOUND <EOL> def delete ( self , request , dhcp_scope = None , dhcp_action = None ) : <EOL> if dhcp_scope and dhcp_action == '<STR_LIT>' : <EOL> try : <EOL> task = ScheduledTask . objects . get ( type = '<STR_LIT>' , task = dhcp_scope ) <EOL> task . delete ( ) <EOL> except : <EOL> pass <EOL> return rc . ALL_OK </s>
