<s> from django . core . exceptions import ValidationError <EOL> from django . core . paginator import Paginator , InvalidPage , EmptyPage <EOL> from django . db . models import Q <EOL> from django . http import HttpResponse , HttpResponseRedirect <EOL> from django . shortcuts import render_to_response , redirect , get_object_or_404 <EOL> from django . template import RequestContext <EOL> from django . template . loader import render_to_string <EOL> try : <EOL> import json <EOL> except : <EOL> from django . utils import simplejson as json <EOL> import _mysql_exceptions <EOL> import models <EOL> import forms <EOL> from truth . models import KeyValue as TruthKeyValue , Truth <EOL> from systems . models import NetworkAdapter <EOL> from middleware . restrict_to_remote import allow_anyone <EOL> from DHCP import DHCP <EOL> from django . test . client import RequestFactory <EOL> from django . template . defaulttags import URLNode <EOL> from django . conf import settings <EOL> from jinja2 . filters import contextfilter <EOL> from django . utils import translation <EOL> from libs . jinja import jinja_render_to_response <EOL> from api_v2 . keyvalue_handler import KeyValueHandler <EOL> factory = RequestFactory ( ) <EOL> def showall ( request ) : <EOL> dhcp_scopes = models . DHCP . objects . all ( ) <EOL> h = KeyValueHandler ( ) <EOL> request = factory . get ( '<STR_LIT>' , follow = True ) <EOL> obj = h . read ( request ) <EOL> dhcp_scopes = [ ] <EOL> for key in obj . iterkeys ( ) : <EOL> dhcp_scopes . append ( key . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] ) <EOL> return jinja_render_to_response ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : dhcp_scopes , <EOL> } ) <EOL> def new ( request ) : <EOL> error_message = '<STR_LIT>' <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . AddDHCPScopeForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> truth_exists = False <EOL> try : <EOL> tmp = Truth . objects . get ( name = form . cleaned_data [ '<STR_LIT>' ] ) <EOL> truth_exists = True <EOL> except : <EOL> pass <EOL> if truth_exists is False : <EOL> t = Truth ( name = form . cleaned_data [ '<STR_LIT>' ] , description = form . cleaned_data [ '<STR_LIT>' ] ) <EOL> t . save ( ) <EOL> k = TruthKeyValue ( truth = t , key = '<STR_LIT>' , value = '<STR_LIT:True>' ) <EOL> k . save ( ) <EOL> return redirect ( '<STR_LIT>' % t . name ) <EOL> else : <EOL> error_message = '<STR_LIT>' <EOL> else : <EOL> form = forms . AddDHCPScopeForm ( ) <EOL> return jinja_render_to_response ( '<STR_LIT>' , { <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : error_message <EOL> } ) <EOL> def override_file ( request , dhcp_scope ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . DHCPScopeOverrideForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> do = models . DHCPOverride . objects . get ( dhcp_scope = dhcp_scope ) <EOL> do . override_text = form . cleaned_data [ '<STR_LIT>' ] <EOL> do . save ( ) <EOL> else : <EOL> try : <EOL> do = models . DHCPOverride . objects . get ( dhcp_scope = dhcp_scope ) <EOL> except : <EOL> do = models . DHCPOverride ( dhcp_scope = dhcp_scope ) <EOL> do . save ( ) <EOL> form = forms . DHCPScopeOverrideForm ( initial = { '<STR_LIT>' : dhcp_scope , '<STR_LIT>' : do . override_text } ) <EOL> return jinja_render_to_response ( '<STR_LIT>' , { <EOL> "<STR_LIT>" : form , <EOL> '<STR_LIT>' : dhcp_scope <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def showfile ( request , dhcp_scope ) : <EOL> try : <EOL> d = models . DHCPFile . objects . get ( dhcp_scope = dhcp_scope ) <EOL> content = d . file_text <EOL> except Exception , e : <EOL> content = """<STR_LIT>""" <EOL> output = content . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> "<STR_LIT>" : output <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def create ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . AddDHCPScopeForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> pass <EOL> else : <EOL> form = forms . AddDHCPScopeForm ( ) <EOL> return render_to_response ( '<STR_LIT>' , { <EOL> "<STR_LIT>" : form <EOL> } , <EOL> RequestContext ( request ) ) <EOL> def edit ( request , dhcp_scope ) : <EOL> h = KeyValueHandler ( ) <EOL> trequest = factory . get ( '<STR_LIT>' % dhcp_scope , follow = True ) <EOL> instance = h . read ( trequest ) <EOL> initial = { } <EOL> initial [ '<STR_LIT>' ] = dhcp_scope <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> try : <EOL> initial [ '<STR_LIT>' ] = instance [ '<STR_LIT>' ] <EOL> except : <EOL> treqeust = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : '<STR_LIT>' , '<STR_LIT>' : dhcp_scope } , follow = True ) <EOL> h . create ( trequest ) <EOL> initial [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = forms . EditDHCPScopeForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> trequest = factory . post ( '<STR_LIT>' % dhcp_scope , { '<STR_LIT>' : dhcp_scope , '<STR_LIT:key>' : '<STR_LIT>' , '<STR_LIT:value>' : form . cleaned_data [ '<STR_LIT>' ] } , follow = True ) <EOL> h . update ( trequest , dhcp_scope ) <EOL> else : <EOL> form = forms . EditDHCPScopeForm ( initial = initial ) <EOL> return jinja_render_to_response ( '<STR_LIT>' , { <EOL> "<STR_LIT>" : form , <EOL> '<STR_LIT>' : dhcp_scope <EOL> } ) <EOL> def delete ( request , dhcp_scope ) : <EOL> try : <EOL> scope = Truth . objects . get ( name = dhcp_scope ) <EOL> TruthKeyValue . objects . filter ( truth = scope ) . delete ( ) <EOL> scope . delete ( ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> except : <EOL> return redirect ( '<STR_LIT>' ) </s>
