<s> """<STR_LIT>""" <EOL> from json import loads as json_loads <EOL> from django . views . generic import TemplateView , CreateView <EOL> from django . shortcuts import redirect <EOL> from django . core . urlresolvers import reverse <EOL> from django . contrib import messages <EOL> from django . utils . html import strip_tags <EOL> from django . utils . safestring import mark_safe <EOL> from braces . views import LoginRequiredMixin <EOL> from allauth . account . models import EmailAddress <EOL> from rest_framework . views import APIView <EOL> from rest_framework . response import Response <EOL> from rest_framework import status <EOL> from geokey . applications . models import Application <EOL> from geokey . projects . views import ProjectContext <EOL> from geokey . core . decorators import ( <EOL> handle_exceptions_for_ajax , handle_exceptions_for_admin <EOL> ) <EOL> from geokey . projects . models import Project <EOL> from geokey . projects . base import STATUS <EOL> from . models import User <EOL> from . serializers import ( UserSerializer , UserGroupSerializer ) <EOL> from . forms import ( <EOL> UsergroupCreateForm , <EOL> CustomPasswordChangeForm , <EOL> UserRegistrationForm , <EOL> UserForm <EOL> ) <EOL> class Index ( TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not request . user . is_anonymous ( ) : <EOL> return redirect ( '<STR_LIT>' ) <EOL> return super ( Index , self ) . get ( request , * args , ** kwargs ) <EOL> class Dashboard ( LoginRequiredMixin , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get_context_data ( self ) : <EOL> """<STR_LIT>""" <EOL> from geokey . extensions . base import extensions <EOL> ext = [ ] <EOL> for ext_id in sorted ( extensions ) : <EOL> extension = extensions . get ( ext_id ) <EOL> if extension . get ( '<STR_LIT>' ) and ( <EOL> not extension . get ( '<STR_LIT>' ) or <EOL> self . request . user . is_superuser ) : <EOL> ext . append ( extension ) <EOL> return { <EOL> '<STR_LIT>' : Project . objects . get_list ( <EOL> self . request . user ) . filter ( admins = self . request . user ) , <EOL> '<STR_LIT>' : STATUS , <EOL> '<STR_LIT>' : ext <EOL> } <EOL> class UserGroupList ( LoginRequiredMixin , ProjectContext , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> class UserGroupCreate ( LoginRequiredMixin , ProjectContext , CreateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> form_class = UsergroupCreateForm <EOL> @ handle_exceptions_for_admin <EOL> def get_context_data ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> project_id = self . kwargs [ '<STR_LIT>' ] <EOL> return super ( UserGroupCreate , self ) . get_context_data ( project_id ) <EOL> def get_success_url ( self ) : <EOL> """<STR_LIT>""" <EOL> project_id = self . kwargs [ '<STR_LIT>' ] <EOL> return reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : project_id , '<STR_LIT>' : self . object . id } <EOL> ) <EOL> def form_valid ( self , form ) : <EOL> """<STR_LIT>""" <EOL> project_id = self . kwargs [ '<STR_LIT>' ] <EOL> project = Project . objects . as_admin ( self . request . user , project_id ) <EOL> form . instance . project = project <EOL> add_another_url = reverse ( <EOL> '<STR_LIT>' , <EOL> kwargs = { <EOL> '<STR_LIT>' : project_id <EOL> } <EOL> ) <EOL> messages . success ( <EOL> self . request , <EOL> mark_safe ( '<STR_LIT>' <EOL> '<STR_LIT>' % add_another_url ) <EOL> ) <EOL> return super ( UserGroupCreate , self ) . form_valid ( form ) <EOL> def post ( self , request , project_id , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . object = None <EOL> context = self . get_context_data ( * args , ** kwargs ) <EOL> project = context . get ( '<STR_LIT>' ) <EOL> if project is not None : <EOL> cannot_create = '<STR_LIT>' <EOL> if project . islocked : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' % cannot_create <EOL> ) <EOL> else : <EOL> return super ( UserGroupCreate , self ) . post ( <EOL> request , project_id , * args , ** kwargs <EOL> ) <EOL> return self . render_to_response ( context ) <EOL> class AdministratorsOverview ( LoginRequiredMixin , ProjectContext , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> class UserGroupContext ( object ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_admin <EOL> def get_context_data ( self , project_id , usergroup_id , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> project = Project . objects . as_admin ( self . request . user , project_id ) <EOL> usergroup = project . usergroups . get ( pk = usergroup_id ) <EOL> return super ( UserGroupContext , self ) . get_context_data ( <EOL> project = project , <EOL> usergroup = usergroup , <EOL> status_types = STATUS , <EOL> * args , <EOL> ** kwargs <EOL> ) <EOL> class UserGroupOverview ( LoginRequiredMixin , UserGroupContext , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> class UserGroupSettings ( LoginRequiredMixin , UserGroupContext , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def post ( self , request , project_id , usergroup_id ) : <EOL> """<STR_LIT>""" <EOL> data = request . POST <EOL> context = self . get_context_data ( project_id , usergroup_id ) <EOL> usergroup = context . get ( '<STR_LIT>' ) <EOL> if usergroup : <EOL> usergroup . name = strip_tags ( data . get ( '<STR_LIT:name>' ) ) <EOL> usergroup . description = strip_tags ( data . get ( '<STR_LIT:description>' ) ) <EOL> usergroup . save ( ) <EOL> messages . success ( self . request , '<STR_LIT>' ) <EOL> return self . render_to_response ( context ) <EOL> class UserGroupData ( LoginRequiredMixin , UserGroupContext , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def post ( self , request , project_id , usergroup_id ) : <EOL> """<STR_LIT>""" <EOL> data = request . POST <EOL> context = self . get_context_data ( project_id , usergroup_id ) <EOL> usergroup = context . get ( '<STR_LIT>' ) <EOL> if usergroup : <EOL> cannot_modify = '<STR_LIT>' <EOL> if usergroup . project . islocked : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' % cannot_modify <EOL> ) <EOL> elif usergroup . project . categories . count ( ) == <NUM_LIT:0> : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' % cannot_modify <EOL> ) <EOL> else : <EOL> if data [ '<STR_LIT>' ] != '<STR_LIT>' : <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT:all>' : <EOL> usergroup . filters = None <EOL> else : <EOL> usergroup . filters = json_loads ( data [ '<STR_LIT>' ] ) <EOL> usergroup . save ( ) <EOL> messages . success ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> return self . render_to_response ( context ) <EOL> class UserGroupPermissions ( LoginRequiredMixin , UserGroupContext , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def post ( self , request , project_id , usergroup_id ) : <EOL> """<STR_LIT>""" <EOL> data = request . POST <EOL> context = self . get_context_data ( project_id , usergroup_id ) <EOL> usergroup = context . get ( '<STR_LIT>' ) <EOL> if usergroup : <EOL> cannot_modify = '<STR_LIT>' <EOL> if usergroup . project . islocked : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' % cannot_modify <EOL> ) <EOL> else : <EOL> usergroup . can_moderate = False <EOL> usergroup . can_contribute = False <EOL> if data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> usergroup . can_moderate = True <EOL> elif data [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> usergroup . can_contribute = True <EOL> usergroup . save ( ) <EOL> messages . success ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> return self . render_to_response ( context ) <EOL> class UserGroupDelete ( LoginRequiredMixin , UserGroupContext , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def get ( self , request , project_id , usergroup_id ) : <EOL> """<STR_LIT>""" <EOL> context = self . get_context_data ( project_id , usergroup_id ) <EOL> usergroup = context . get ( '<STR_LIT>' ) <EOL> if usergroup : <EOL> if usergroup . project . islocked : <EOL> messages . error ( <EOL> self . request , <EOL> '<STR_LIT>' <EOL> ) <EOL> return redirect ( <EOL> '<STR_LIT>' , <EOL> project_id = project_id , <EOL> usergroup_id = usergroup_id <EOL> ) <EOL> else : <EOL> usergroup . 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 UserProfile ( LoginRequiredMixin , TemplateView ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> user = User . objects . get ( pk = request . user . pk ) <EOL> form = UserForm ( request . POST , instance = user ) <EOL> if form . is_valid ( ) : <EOL> if form . has_changed ( ) : <EOL> user . display_name = form . cleaned_data [ '<STR_LIT>' ] <EOL> user . email = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> user . save ( ) <EOL> if user . email != request . user . email : <EOL> try : <EOL> EmailAddress . objects . get ( user = user ) . change ( <EOL> request , <EOL> user . email , <EOL> confirm = True <EOL> ) <EOL> except EmailAddress . DoesNotExist : <EOL> EmailAddress . objects . create ( <EOL> user = user , <EOL> email = user . email <EOL> ) . send_confirmation ( request ) <EOL> messages . success ( request , '<STR_LIT>' ) <EOL> self . request . user = user <EOL> else : <EOL> messages . info ( request , '<STR_LIT>' ) <EOL> return self . render_to_response ( self . get_context_data ( form = form ) ) <EOL> class QueryUsers ( APIView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request ) : <EOL> """<STR_LIT>""" <EOL> q = request . GET . get ( '<STR_LIT>' ) <EOL> if q is None : <EOL> return Response ( [ ] ) <EOL> else : <EOL> users = User . objects . filter ( <EOL> display_name__icontains = q . lower ( ) <EOL> ) . exclude ( pk = <NUM_LIT:1> ) [ : <NUM_LIT:10> ] <EOL> serializer = UserSerializer ( <EOL> users , many = True , fields = ( '<STR_LIT:id>' , '<STR_LIT>' ) <EOL> ) <EOL> return Response ( serializer . data ) <EOL> class UserGroup ( APIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def put ( self , request , project_id , usergroup_id ) : <EOL> """<STR_LIT>""" <EOL> project = Project . objects . as_admin ( request . user , project_id ) <EOL> if project . islocked : <EOL> return Response ( <EOL> '<STR_LIT>' , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> else : <EOL> group = project . usergroups . get ( pk = usergroup_id ) <EOL> serializer = UserGroupSerializer ( <EOL> group , data = request . data , partial = True ) <EOL> if serializer . is_valid ( ) : <EOL> serializer . save ( ) <EOL> return Response ( serializer . data ) <EOL> return Response ( <EOL> serializer . errors , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> class UserGroupUsers ( APIView ) : <EOL> """<STR_LIT>""" <EOL> @ handle_exceptions_for_ajax <EOL> def post ( self , request , project_id , usergroup_id ) : <EOL> """<STR_LIT>""" <EOL> project = Project . objects . as_admin ( request . user , project_id ) <EOL> if project . islocked : <EOL> return Response ( <EOL> '<STR_LIT>' , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> else : <EOL> user_id = request . data . get ( '<STR_LIT>' ) <EOL> try : <EOL> user = User . objects . get ( pk = user_id ) <EOL> except User . DoesNotExist : <EOL> return Response ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> group = project . usergroups . get ( pk = usergroup_id ) <EOL> group . users . add ( user ) <EOL> serializer = UserGroupSerializer ( group ) <EOL> return Response ( serializer . data , status = status . HTTP_201_CREATED ) <EOL> class CreateUserMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def create_user ( self , data ) : <EOL> """<STR_LIT>""" <EOL> user = User . objects . create_user ( <EOL> data . get ( '<STR_LIT:email>' ) , <EOL> data . get ( '<STR_LIT>' ) , <EOL> password = data . get ( '<STR_LIT>' ) <EOL> ) <EOL> user . save ( ) <EOL> return user <EOL> class UserAPIView ( CreateUserMixin , APIView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request ) : <EOL> """<STR_LIT>""" <EOL> user = request . user <EOL> if not user . is_anonymous ( ) : <EOL> serializer = UserSerializer ( user ) <EOL> return Response ( <EOL> serializer . data , <EOL> status = status . HTTP_200_OK <EOL> ) <EOL> else : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_401_UNAUTHORIZED <EOL> ) <EOL> def patch ( self , request ) : <EOL> """<STR_LIT>""" <EOL> user = request . user <EOL> if user . is_anonymous ( ) : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_401_UNAUTHORIZED <EOL> ) <EOL> data = request . data <EOL> serializer = UserSerializer ( user , data = data , partial = True ) <EOL> if serializer . is_valid ( ) : <EOL> new_mail = data . get ( '<STR_LIT:email>' ) <EOL> if new_mail is not None and user . email != new_mail : <EOL> try : <EOL> email = EmailAddress . objects . get ( <EOL> user = user , email = user . email <EOL> ) <EOL> email . change ( request , new_mail , confirm = True ) <EOL> except EmailAddress . DoesNotExist : <EOL> EmailAddress . objects . create ( user = user , email = new_mail ) <EOL> serializer . save ( ) <EOL> return Response ( <EOL> serializer . data , <EOL> status = status . HTTP_200_OK <EOL> ) <EOL> else : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : serializer . errors } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> data = request . data <EOL> form = UserRegistrationForm ( data ) <EOL> client_id = data . pop ( '<STR_LIT>' , None ) <EOL> try : <EOL> Application . objects . get ( client_id = client_id ) <EOL> if form . is_valid ( ) : <EOL> user = self . create_user ( data ) <EOL> EmailAddress . objects . add_email ( <EOL> request . _request , <EOL> user , <EOL> user . email , <EOL> signup = True , <EOL> confirm = True <EOL> ) <EOL> serializer = UserSerializer ( user ) <EOL> return Response ( <EOL> serializer . data , <EOL> status = status . HTTP_201_CREATED <EOL> ) <EOL> else : <EOL> return Response ( <EOL> { '<STR_LIT>' : form . errors } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> except Application . DoesNotExist : <EOL> return Response ( <EOL> { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> class ChangePasswordView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> def post ( self , request ) : <EOL> """<STR_LIT>""" <EOL> user = request . user <EOL> data = request . data <EOL> if not user . is_anonymous ( ) : <EOL> form = CustomPasswordChangeForm ( user , data ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> else : <EOL> return Response ( <EOL> { '<STR_LIT>' : form . errors } , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> else : <EOL> return Response ( <EOL> { '<STR_LIT:error>' : '<STR_LIT>' } , <EOL> status = status . HTTP_401_UNAUTHORIZED <EOL> ) </s>
