def cancel_purposes(self, request, pk=None, *args, **kwargs): try: purpose_ids_list = request.data.get('purpose_ids_list', None) # if not type(purpose_ids_list) == list: # raise serializers.ValidationError( # 'Purpose IDs must be a list') if not request.user.has_perm('wildlifecompliance.issuing_officer'): raise serializers.ValidationError( 'You are not authorised to cancel licenced activities') # if LicencePurpose.objects.filter(id__in=purpose_ids_list).\ # values_list('licence_activity_id',flat=True).\ # distinct().count() != 1: # raise serializers.ValidationError( # 'Selected purposes must all be of the same licence activity') if not purpose_ids_list and pk: raise serializers.ValidationError( 'Licence ID and Purpose IDs list must be specified') instance = self.get_object() LicenceService.request_cancel_licence(instance, request) serializer = DTExternalWildlifeLicenceSerializer( instance, context={'request': request}) return Response(serializer.data) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def reinstate_purposes(self, request, pk=None, *args, **kwargs): MSG_NOAUTH = 'You are not authorised to reinstate licenced activities' # MSG_NOSAME = 'Purposes must all be of the same licence activity' try: purpose_ids_list = request.data.get('purpose_ids_list', None) if not request.user.has_perm('wildlifecompliance.issuing_officer'): raise serializers.ValidationError(MSG_NOAUTH) if not purpose_ids_list and pk: raise serializers.ValidationError( 'Licence ID and Purpose IDs list must be specified') instance = self.get_object() LicenceService.request_reinstate_licence(instance, request) serializer = DTExternalWildlifeLicenceSerializer( instance, context={'request': request}) return Response(serializer.data) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def verify_licence_renewal(self, request, queryset): from wildlifecompliance.components.licences.services import ( LicenceService, ) for selected in queryset: LicenceService.verify_licence_renewal_for(selected.id, request) self.message_user(request, 'Selected licence renewals have been verified.')
def verify_expired_licence(self, request, queryset): from wildlifecompliance.components.licences.services import ( LicenceService, ) with transaction.atomic(): for selected in queryset: LicenceService.verify_expired_licence_for(selected.id, request) self.message_user(request, 'Selected licence expired have been verified.')
def get_latest_purposes_for_licence_activity_and_action( self, request, *args, **kwargs): try: instance = self.get_object() activity_id = request.GET.get('licence_activity_id', None) action = request.GET.get('action', None) if not activity_id or not action: raise serializers.ValidationError( 'A licence activity ID and action must be specified') queryset = LicenceService.request_licence_purpose_list( instance, request) serializer = ProposedPurposeSerializer(queryset, many=True) return Response(serializer.data) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: if hasattr(e, 'error_dict'): raise serializers.ValidationError(repr(e.error_dict)) else: # raise serializers.ValidationError(repr(e[0].encode('utf-8'))) raise serializers.ValidationError(repr(e[0])) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def surrender_licence(self, request, pk=None, *args, **kwargs): try: if pk: instance = self.get_object() LicenceService.request_surrender_licence(instance, request) serializer = DTExternalWildlifeLicenceSerializer( instance, context={'request': request}) return Response(serializer.data) else: raise serializers.ValidationError( 'Licence ID must be specified') except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def process_action( self, request, licence_purpose_id, action_form, action_title ): from wildlifecompliance.components.licences.services import ( LicenceService, ) licence_purpose = self.get_object(request, licence_purpose_id) schm = LicenceService.generate_licence_schema(licence_purpose, request) if request.method != 'POST': form = action_form() else: form = action_form(request.POST) if form.is_valid(): try: # form.save(proposal_type) licence_purpose.schema = schm licence_purpose.save() except Exception: # If save() raised, the form will a have a non # field error containing an informative message. pass else: self.message_user(request, 'Success') url = reverse( 'admin:wildlifecompliance_licencepurpose_change', args=[licence_purpose.pk], current_app=self.admin_site.name, ) return HttpResponseRedirect(url) context = self.admin_site.each_context(request) context['opts'] = self.model._meta context['form'] = form context['licence_purpose'] = licence_purpose context['title'] = action_title context['new_schema'] = schm return TemplateResponse( request, 'wildlifecompliance/admin/licencepurpose_action.html', context, )
def reinstate_licence(self, request, pk=None, *args, **kwargs): try: if not request.user.has_perm('wildlifecompliance.issuing_officer'): raise serializers.ValidationError( 'You are not authorised to reinstate licences') if pk: instance = self.get_object() LicenceService.request_reinstate_licence(instance, request) serializer = DTExternalWildlifeLicenceSerializer( instance, context={'request': request}) return Response(serializer.data) else: raise serializers.ValidationError( 'Licence ID must be specified') except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def handle(self, *args, **options): try: logger.info('Running command {}'.format(__name__)) total = LicenceService.verify_expired_licences() email = CommandsVerifyNotificationEmail() email.set_verified_total(total) email.set_subject('verify_expired_licences was executed.') email.out() logger.info('Command {} finished'.format(__name__)) except Exception as e: logger.error('Error command {0} : {1}'.format(__name__, e))
def version_selected_Licence_purposes(self, request, queryset): from wildlifecompliance.components.licences.services import ( LicenceService, ) warn = False with transaction.atomic(): for selected in queryset: ok = LicenceService.version_licence_purpose( selected.id, request) warn = warn if ok else True if warn: messages.warning( request, 'Not all Licence Purpose(s) have been versioned.') else: self.message_user( request, 'Selected Licence Purpose(s) have been versioned.')
def handle(self, *args, **options): errors = [] updates = [] try: logger.info('Running command {}'.format(__name__)) updates = LicenceService.verify_expired_licences() logger.info('Command {} finished'.format(__name__)) except Exception as e: logger.error('Error command {0} : {1}'.format( __name__, e)) errors.append(e) cmd_name = __name__.split('.')[-1].replace('_', ' ').upper() err_str = '<strong style="color: red;">Errors: {}</strong>'.format( len(errors) ) if len(errors)>0 else '<strong style="color: green;">Errors: 0</strong>' msg = '<p>{} completed. {}. IDs updated: {}.</p>'.format( cmd_name, err_str, updates ) logger.info(msg) print(msg) # will redirect to cron_tasks.log file, by the parent script
def get_latest_activities_merged(self, obj): from wildlifecompliance.components.licences.services import ( LicenceService, ) return LicenceService.get_activities_list_for(obj)