def create(self, request, *args, **kwargs): model_name = self.queryset.model._meta.model_name app_name = self.queryset.model._meta.app_label perm = '{}.add_{}'.format(app_name, model_name) with transaction.atomic(): response = super(PublicProjectViewSet, self).create(request, *args, **kwargs) user = request.user obj = self.queryset.model.objects.get(pk=response.data['id']) project = get_project_for_object(Project, obj) # Delete the object if the user doesn't have the right permissions # to create this. The object may get created without checking for # permissions, since the viewset returns True if the user just has # the required role in any organisation. If the newly created # object is not a project, and the user doesn't have permissions to # create it, we delete the object. if obj != project and not (user.has_perm( 'rsr.view_project', project) and user.has_perm(perm, obj)): obj.delete() if obj.pk is None: raise exceptions.PermissionDenied elif project is not None: log_project_changes(request.user, project, obj, {}, 'added') delete_project_from_project_directory_cache(project.pk) project.schedule_iati_checks() return response
def update(self, request, *args, **kwargs): response = super(PublicProjectViewSet, self).update(request, *args, **kwargs) obj = self.get_object() project = get_project_for_object(Project, obj) if project is not None: log_project_changes(request.user, project, obj, request.data, 'changed') delete_project_from_project_directory_cache(project.pk) project.schedule_iati_checks() return response
def destroy(self, request, *args, **kwargs): obj = self.get_object() project = get_project_for_object(Project, obj) try: response = super(PublicProjectViewSet, self).destroy(request, *args, **kwargs) except ProtectedError: msg = _("{}s with updates cannot be deleted".format( self.queryset.model.__name__)) return Response(msg, status=status.HTTP_405_METHOD_NOT_ALLOWED) if project is not None: log_project_changes(request.user, project, obj, {}, 'deleted') delete_project_from_project_directory_cache(project.pk) project.schedule_iati_checks() return response
def handle(self, *args, **options): projects = Project.objects.published().values_list('pk', flat=True) start = time() print( f"Populating project directory cache with {projects.count()} projects" ) for project_id in projects: print(f"project {project_id}", end=' ', flush=True) project_start = time() delete_project_from_project_directory_cache(project_id) print(f"{time() - project_start}", end=' ', flush=True) serialized_project(project_id) print(f"{time() - project_start}") print(f"total: {time() - start}")
def handle(self, *args, **options): projects = Project.objects.published().values_list('pk', flat=True) for project_id in projects: delete_project_from_project_directory_cache(project_id) serialized_project(project_id)