<s> """<STR_LIT>""" <EOL> from django . views . generic import CreateView , TemplateView <EOL> from django . shortcuts import redirect <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib import messages <EOL> from django . template . defaultfilters import slugify <EOL> from django . utils . html import strip_tags <EOL> from django . utils . safestring import mark_safe <EOL> from rest_framework import status <EOL> from rest_framework . views import APIView <EOL> from rest_framework . response import Response <EOL> from braces . views import LoginRequiredMixin <EOL> from geokey . projects . models import Project <EOL> from geokey . projects . views import ProjectContext <EOL> from geokey . core . decorators import handle_exceptions_for_ajax <EOL> from . base import STATUS <EOL> from . models import ( <EOL> Category , Field , TextField , NumericField , LookupField , LookupValue , <EOL> MultipleLookupField , MultipleLookupValue <EOL> ) <EOL> from . forms import CategoryCreateForm , FieldCreateForm <EOL> from . serializers import ( <EOL> CategorySerializer , FieldSerializer , LookupFieldSerializer <EOL> ) <EOL> from geokey . categories . mixins import CategoryMixin , FieldMixin <EOL> from geokey . contributions . models import Observation <EOL> class CategoryList ( LoginRequiredMixin , ProjectContext , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> class CategoryCreate ( LoginRequiredMixin , ProjectContext , CreateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> form_class = CategoryCreateForm <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return super ( CategoryCreate , self ) . get_context_data ( <EOL> self . kwargs [ '<STR_LIT>' ] , <EOL> ** kwargs <EOL> ) <EOL> def form_valid ( self , form ) : <EOL> """<STR_LIT>""" <EOL> data = form . cleaned_data <EOL> project = Project . objects . as_admin ( <EOL> self . request . user , <EOL> self . kwargs [ '<STR_LIT>' ] <EOL> ) <EOL> if project : <EOL> if project . islocked : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> return redirect ( <EOL> '<STR_LIT>' , <EOL> project_id = self . kwargs [ '<STR_LIT>' ] <EOL> ) <EOL> else : <EOL> category = Category . objects . create ( <EOL> project = project , <EOL> creator = self . request . user , <EOL> name = strip_tags ( data . get ( '<STR_LIT:name>' ) ) , <EOL> description = strip_tags ( data . get ( '<STR_LIT:description>' ) ) , <EOL> default_status = data . get ( '<STR_LIT>' ) <EOL> ) <EOL> add_another_url = reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : self . kwargs [ '<STR_LIT>' ] } <EOL> ) <EOL> messages . success ( <EOL> self . request , <EOL> mark_safe ( '<STR_LIT>' <EOL> '<STR_LIT>' % add_another_url ) <EOL> ) <EOL> return redirect ( <EOL> '<STR_LIT>' , <EOL> project_id = self . kwargs [ '<STR_LIT>' ] , <EOL> category_id = category . id <EOL> ) <EOL> return self . render_to_response ( self . get_context_data ( form = form ) ) <EOL> class CategoryOverview ( LoginRequiredMixin , CategoryMixin , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> class CategorySettings ( LoginRequiredMixin , CategoryMixin , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self , project_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> context = super ( CategorySettings , self ) . get_context_data ( <EOL> project_id , <EOL> category_id , <EOL> status_types = STATUS , <EOL> ) <EOL> category = context . get ( '<STR_LIT>' ) <EOL> if category : <EOL> context [ '<STR_LIT>' ] = Observation . objects . filter ( <EOL> category = category <EOL> ) . count ( ) <EOL> return context <EOL> def post ( self , request , project_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> data = request . POST <EOL> context = self . get_context_data ( project_id , category_id ) <EOL> category = context . get ( '<STR_LIT>' ) <EOL> if category : <EOL> category . name = strip_tags ( data . get ( '<STR_LIT:name>' ) ) <EOL> category . description = strip_tags ( data . get ( '<STR_LIT:description>' ) ) <EOL> category . default_status = data . get ( '<STR_LIT>' ) <EOL> if category . fields . exists ( ) : <EOL> display_field = category . fields . get ( <EOL> pk = data . get ( '<STR_LIT>' ) <EOL> ) <EOL> if category . display_field != display_field : <EOL> category . display_field = display_field <EOL> for observation in category . observation_set . all ( ) : <EOL> observation . update_display_field ( ) <EOL> observation . save ( ) <EOL> category . save ( ) <EOL> messages . success ( self . request , '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = category <EOL> return self . render_to_response ( context ) <EOL> class CategoryDisplay ( LoginRequiredMixin , CategoryMixin , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def post ( self , request , project_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> data = request . POST <EOL> context = self . get_context_data ( project_id , category_id ) <EOL> category = context . get ( '<STR_LIT>' ) <EOL> if category : <EOL> symbol = request . FILES . get ( '<STR_LIT>' ) <EOL> category . colour = data . get ( '<STR_LIT>' ) <EOL> if symbol is not None : <EOL> category . symbol . delete ( ) <EOL> category . symbol = symbol <EOL> elif data . get ( '<STR_LIT>' ) == '<STR_LIT:true>' : <EOL> category . symbol . delete ( ) <EOL> category . symbol = None <EOL> category . save ( ) <EOL> messages . success ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> context [ '<STR_LIT>' ] = category <EOL> return self . render_to_response ( context ) <EOL> class CategoryDelete ( LoginRequiredMixin , CategoryMixin , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request , project_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> context = self . get_context_data ( project_id , category_id ) <EOL> category = context . get ( '<STR_LIT>' ) <EOL> if category : <EOL> if category . project . islocked : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> return redirect ( <EOL> '<STR_LIT>' , <EOL> project_id = project_id , <EOL> category_id = category_id <EOL> ) <EOL> else : <EOL> category . delete ( ) <EOL> messages . success ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> return redirect ( '<STR_LIT>' , project_id = project_id ) <EOL> return self . render_to_response ( context ) <EOL> class FieldCreate ( LoginRequiredMixin , CategoryMixin , CreateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> form_class = FieldCreateForm <EOL> def get_context_data ( self , data = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = super ( FieldCreate , self ) . get_context_data ( <EOL> self . kwargs [ '<STR_LIT>' ] , <EOL> self . kwargs [ '<STR_LIT>' ] , <EOL> ** kwargs <EOL> ) <EOL> context [ '<STR_LIT>' ] = Field . get_field_types ( ) <EOL> return context <EOL> def form_valid ( self , form ) : <EOL> """<STR_LIT>""" <EOL> request = self . request <EOL> data = form . cleaned_data <EOL> category = Category . objects . as_admin ( <EOL> self . request . user , <EOL> self . kwargs [ '<STR_LIT>' ] , <EOL> self . kwargs [ '<STR_LIT>' ] <EOL> ) <EOL> if category : <EOL> if category . project . islocked : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> return redirect ( <EOL> '<STR_LIT>' , <EOL> project_id = self . kwargs [ '<STR_LIT>' ] , <EOL> category_id = self . kwargs [ '<STR_LIT>' ] <EOL> ) <EOL> else : <EOL> proposed_key = slugify ( strip_tags ( data . get ( '<STR_LIT:name>' ) ) ) <EOL> if len ( proposed_key ) < <NUM_LIT:1> : <EOL> proposed_key = '<STR_LIT:key>' <EOL> suggested_key = proposed_key <EOL> count = <NUM_LIT:1> <EOL> while category . fields . filter ( key = suggested_key ) . exists ( ) : <EOL> suggested_key = '<STR_LIT>' % ( proposed_key , count ) <EOL> count = count + <NUM_LIT:1> <EOL> field = Field . create ( <EOL> strip_tags ( data . get ( '<STR_LIT:name>' ) ) , <EOL> suggested_key , <EOL> strip_tags ( data . get ( '<STR_LIT:description>' ) ) , <EOL> data . get ( '<STR_LIT>' ) , <EOL> category , <EOL> request . POST . get ( '<STR_LIT:type>' ) <EOL> ) <EOL> if isinstance ( field , TextField ) : <EOL> field . textarea = request . POST . get ( '<STR_LIT>' ) or False <EOL> field . maxlength = request . POST . get ( '<STR_LIT>' ) or None <EOL> elif isinstance ( field , NumericField ) : <EOL> field . minval = request . POST . get ( '<STR_LIT>' ) or None <EOL> field . maxval = request . POST . get ( '<STR_LIT>' ) or None <EOL> field . save ( ) <EOL> add_another_url = reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { <EOL> '<STR_LIT>' : self . kwargs [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : self . kwargs [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> messages . success ( <EOL> self . request , <EOL> mark_safe ( '<STR_LIT>' <EOL> '<STR_LIT>' % add_another_url ) <EOL> ) <EOL> return redirect ( <EOL> '<STR_LIT>' , <EOL> project_id = self . kwargs [ '<STR_LIT>' ] , <EOL> category_id = self . kwargs [ '<STR_LIT>' ] , <EOL> field_id = field . id <EOL> ) <EOL> return self . render_to_response ( self . get_context_data ( form = form ) ) <EOL> class FieldSettings ( LoginRequiredMixin , FieldMixin , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self , project_id , category_id , field_id , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = super ( FieldSettings , self ) . get_context_data ( <EOL> project_id , <EOL> category_id , <EOL> field_id <EOL> ) <EOL> if context . get ( '<STR_LIT>' ) : <EOL> context [ '<STR_LIT>' ] = STATUS <EOL> context [ '<STR_LIT>' ] = ( <EOL> context [ '<STR_LIT>' ] == context [ '<STR_LIT>' ] . category . display_field ) <EOL> return context <EOL> def post ( self , request , project_id , category_id , field_id ) : <EOL> """<STR_LIT>""" <EOL> data = request . POST <EOL> context = self . get_context_data ( <EOL> project_id , <EOL> category_id , <EOL> field_id <EOL> ) <EOL> field = context . get ( '<STR_LIT>' ) <EOL> if field : <EOL> field . name = strip_tags ( data . get ( '<STR_LIT:name>' ) ) <EOL> field . description = strip_tags ( data . get ( '<STR_LIT:description>' ) ) <EOL> field . required = data . get ( '<STR_LIT>' ) or False <EOL> if isinstance ( field , TextField ) : <EOL> field . textarea = data . get ( '<STR_LIT>' ) or False <EOL> field . maxlength = data . get ( '<STR_LIT>' ) or None <EOL> elif isinstance ( field , NumericField ) : <EOL> field . minval = data . get ( '<STR_LIT>' ) or None <EOL> field . maxval = data . get ( '<STR_LIT>' ) or None <EOL> field . save ( ) <EOL> messages . success ( self . request , '<STR_LIT>' ) <EOL> context [ '<STR_LIT>' ] = field <EOL> return self . render_to_response ( context ) <EOL> class FieldDelete ( LoginRequiredMixin , FieldMixin , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request , project_id , category_id , field_id ) : <EOL> """<STR_LIT>""" <EOL> context = self . get_context_data ( project_id , category_id , field_id ) <EOL> field = context . get ( '<STR_LIT>' ) <EOL> if field : <EOL> if field . category . project . islocked : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> return redirect ( <EOL> '<STR_LIT>' , <EOL> project_id = project_id , <EOL> category_id = category_id , <EOL> field_id = field_id <EOL> ) <EOL> else : <EOL> field . delete ( ) <EOL> messages . success ( self . request , '<STR_LIT>' ) <EOL> return redirect ( <EOL> '<STR_LIT>' , <EOL> project_id = project_id , <EOL> category_id = category_id <EOL> ) <EOL> return self . render_to_response ( context ) <EOL> class CategoryUpdate ( APIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def get ( self , request , project_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> category = Category . objects . as_admin ( <EOL> request . user , project_id , category_id ) <EOL> serializer = CategorySerializer ( category ) <EOL> return Response ( serializer . data ) <EOL> @ handle_exceptions_for_ajax <EOL> def put ( self , request , project_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> category = Category . objects . as_admin ( <EOL> request . user , project_id , category_id ) <EOL> serializer = CategorySerializer ( <EOL> category , data = request . data , partial = True , <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT:status>' ) ) <EOL> if serializer . is_valid ( ) : <EOL> serializer . save ( ) <EOL> return Response ( serializer . data ) <EOL> return Response ( serializer . errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> class FieldUpdate ( APIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def put ( self , request , project_id , category_id , field_id , <EOL> format = None ) : <EOL> """<STR_LIT>""" <EOL> field = Field . objects . as_admin ( <EOL> request . user , project_id , category_id , field_id ) <EOL> serializer = FieldSerializer ( <EOL> field , data = request . data , partial = True <EOL> ) <EOL> if serializer . is_valid ( ) : <EOL> serializer . save ( ) <EOL> return Response ( serializer . data ) <EOL> return Response ( serializer . errors , status = status . HTTP_400_BAD_REQUEST ) <EOL> class FieldLookups ( APIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def post ( self , request , project_id , category_id , field_id , <EOL> format = None ) : <EOL> """<STR_LIT>""" <EOL> field = Field . objects . as_admin ( <EOL> request . user , project_id , category_id , field_id ) <EOL> name = strip_tags ( request . data . get ( '<STR_LIT:name>' ) ) <EOL> if field . category . project . islocked : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> if isinstance ( field , LookupField ) : <EOL> LookupValue . objects . create ( name = name , field = field ) <EOL> serializer = LookupFieldSerializer ( field ) <EOL> return Response ( serializer . data , status = status . HTTP_201_CREATED ) <EOL> elif isinstance ( field , MultipleLookupField ) : <EOL> MultipleLookupValue . objects . create ( name = name , field = field ) <EOL> serializer = LookupFieldSerializer ( field ) <EOL> return Response ( serializer . data , status = status . HTTP_201_CREATED ) <EOL> else : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_404_NOT_FOUND <EOL> ) <EOL> class FieldLookupsUpdate ( APIView ) : <EOL> """<STR_LIT>""" <EOL> def get_field ( self , user , project_id , category_id , field_id ) : <EOL> field = Field . objects . as_admin ( <EOL> user , project_id , category_id , field_id ) <EOL> if ( isinstance ( field , LookupField ) or <EOL> isinstance ( field , MultipleLookupField ) ) : <EOL> return field <EOL> else : <EOL> return None <EOL> @ handle_exceptions_for_ajax <EOL> def post ( self , request , project_id , category_id , field_id , value_id ) : <EOL> """<STR_LIT>""" <EOL> field = self . get_field ( request . user , project_id , category_id , field_id ) <EOL> if field : <EOL> if field . category . project . islocked : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> else : <EOL> value = field . lookupvalues . get ( pk = value_id ) <EOL> name = request . data . get ( '<STR_LIT:name>' ) <EOL> if name : <EOL> value . name = strip_tags ( name ) <EOL> symbol = request . FILES . get ( '<STR_LIT>' ) <EOL> if symbol : <EOL> value . symbol . delete ( ) <EOL> value . symbol = symbol <EOL> elif request . POST . get ( '<STR_LIT>' ) == '<STR_LIT:true>' : <EOL> value . symbol . delete ( ) <EOL> value . symbol = None <EOL> value . save ( ) <EOL> return Response ( { <EOL> '<STR_LIT:id>' : value . id , <EOL> '<STR_LIT:name>' : value . name , <EOL> '<STR_LIT>' : value . symbol . url if value . symbol else None <EOL> } ) <EOL> else : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_404_NOT_FOUND <EOL> ) <EOL> @ handle_exceptions_for_ajax <EOL> def delete ( self , request , project_id , category_id , field_id , value_id ) : <EOL> """<STR_LIT>""" <EOL> field = self . get_field ( request . user , project_id , category_id , field_id ) <EOL> if field : <EOL> if field . category . project . islocked : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> else : <EOL> field . lookupvalues . get ( pk = value_id ) . delete ( ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> else : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_404_NOT_FOUND <EOL> ) <EOL> class FieldsReorderView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def post ( self , request , project_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> category = Category . objects . as_admin ( <EOL> request . user , <EOL> project_id , <EOL> category_id <EOL> ) <EOL> try : <EOL> category . reorder_fields ( request . data . get ( '<STR_LIT>' ) ) <EOL> serializer = CategorySerializer ( category ) <EOL> return Response ( serializer . data ) <EOL> except Field . DoesNotExist : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> class SingleCategory ( APIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def get ( self , request , project_id , category_id ) : <EOL> """<STR_LIT>""" <EOL> category = Category . objects . get_single ( <EOL> request . user , <EOL> project_id , <EOL> category_id <EOL> ) <EOL> serializer = CategorySerializer ( category ) <EOL> return Response ( serializer . data ) </s>
