Python django.db.transaction.atomic() Examples

The following are 30 code examples of django.db.transaction.atomic(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.db.transaction , or try the search function .
Example #1
Source File: evidence.py    From open-synthesis with GNU General Public License v3.0 8 votes vote down vote up
def toggle_source_tag(request, evidence_id, source_id):
    """Toggle source tag for the given source and redirect to the evidence detail page for the associated evidence."""
    # May want to put in a sanity check here that source_id actually corresponds to evidence_id
    # Inefficient to have to do the DB lookup before making a modification. May want to have the client pass in
    # whether or not they're adding/removing the tag
    if request.method == 'POST':
        with transaction.atomic():
            source = get_object_or_404(EvidenceSource, pk=source_id)
            tag = EvidenceSourceTag.objects.get(tag_name=request.POST['tag'])
            user_tag = AnalystSourceTag.objects.filter(source=source, tagger=request.user, tag=tag)
            if user_tag.count() > 0:
                user_tag.delete()
                messages.success(request, _('Removed "{name}" tag from source.').format(name=tag.tag_name))
            else:
                AnalystSourceTag.objects.create(source=source, tagger=request.user, tag=tag)
                messages.success(request, _('Added "{name}" tag to source.').format(name=tag.tag_name))
            return HttpResponseRedirect(reverse('openach:evidence_detail', args=(evidence_id,)))
    else:
        # Redirect to the form where the user can toggle a source tag
        return HttpResponseRedirect(reverse('openach:evidence_detail', args=(evidence_id,))) 
Example #2
Source File: cleanup.py    From resolwe with Apache License 2.0 6 votes vote down vote up
def process(self, file_storage_id: Optional[int] = None):
        """Process objects to clean.

        When file_storage is not None process only that object.
        """
        logger.debug("Starting cleanup manager run")
        qset = FileStorage.objects.all()
        if file_storage_id is not None:
            qset = qset.filter(pk=file_storage_id)
        for file_storage in qset.filter(data__isnull=True).iterator():
            # Set applicable storage locations to deleting.
            StorageLocation.all_objects.unreferenced_locations().filter(
                file_storage=file_storage
            ).update(status=StorageLocation.STATUS_DELETING)
            with transaction.atomic():
                q_set = FileStorage.objects.filter(
                    id=file_storage.id
                ).select_for_update(skip_locked=True)
                # The FileStorage object is locked or deleted, skip processing.
                if not q_set.exists():
                    continue
                self._process_file_storage(q_set.first())

        logger.debug("Finished cleanup manager run") 
Example #3
Source File: teams.py    From open-synthesis with GNU General Public License v3.0 6 votes vote down vote up
def create_team(request):
    """Return a team creation view, or handle the form submission."""
    if request.method == 'POST':
        form = TeamCreateForm(request.POST)
        if form.is_valid():
            with transaction.atomic():
                team = form.save(commit=False)
                team.owner = request.user
                team.creator = request.user
                team.save()
                team.members.add(request.user)
                team.save()
            return HttpResponseRedirect(reverse('openach:view_team', args=(team.id,)))
    else:
        form = TeamCreateForm()
    return render(request, 'teams/create_team.html', {'form': form}) 
Example #4
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def create_reminder_on(self, date, start_date, end_date):
        if start_date > date or date > end_date:
            # not timely, so ignore
            return

        if self.reminder_type == 'ROLE':
            roles = Role.objects_fresh.filter(unit=self.unit, role=self.role).select_related('person')
            recipients = [r.person for r in roles]
        elif self.reminder_type in ['PERS', 'INST']:
            recipients = [self.person]
        else:
            raise ValueError()

        for recip in recipients:
            ident = '%s_%s_%s' % (self.slug, recip.userid_or_emplid(), date.isoformat())
            # ident length: slug (50) + userid/emplid (9) + ISO date (10) + _ (2) <= 71
            rm = ReminderMessage(reminder=self, sent=False, date=date, person=recip, ident=ident)
            with transaction.atomic():
                try:
                    rm.save()
                except IntegrityError:
                    # already been created because we got IntegrityError on rm.ident
                    pass 
Example #5
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def deduplicate(cls, start_date=None, end_date=None, dry_run=False):
        """
        Remove any EnrolmentHistory objects that aren't adding any new information.
        """
        all_ehs = EnrolmentHistory.objects.order_by('offering', 'date')
        if start_date:
            all_ehs = all_ehs.filter(date__gte=start_date)
        if end_date:
            all_ehs = all_ehs.filter(date__lte=end_date)

        for off_id, ehs in itertools.groupby(all_ehs, key=lambda eh: eh.offering_id):
            # iterate through EnrolmentHistory for this offering and purge any "same as yesterday" entries
            with transaction.atomic():
                current = next(ehs)
                for eh in ehs:
                    if current.is_dup(eh):
                        if not dry_run:
                            eh.delete()
                        else:
                            print('delete', eh)
                    else:
                        current = eh 
Example #6
Source File: models.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def safely_delete(self):
        """
        Do the actions to safely "delete" the activity.
        """
        with transaction.atomic():
            # mangle name and short-name so instructors can delete and replace
            i = 1
            while True:
                suffix = "__%04i" % (i)
                existing = Activity.objects.filter(offering=self.offering, name=self.name+suffix).count() \
                        + Activity.objects.filter(offering=self.offering, short_name=self.short_name+suffix).count()
                if existing == 0:
                    break
                i += 1

            # update the activity
            self.deleted = True
            # Truncate the names if we need to since we are adding a 6 character suffix 
            self.name = self.name[:24] + suffix
            self.short_name = self.short_name[:9] + suffix
            self.slug = None
            self.save() 
Example #7
Source File: graph.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def post(self, request, cardid=None):
        data = JSONDeserializer().deserialize(request.body)
        if self.action == "update_card":
            if data:
                card = Card(data)
                card.save()
                return JSONResponse(card)

        if self.action == "reorder_cards":
            if "cards" in data and len(data["cards"]) > 0:
                with transaction.atomic():
                    for card_data in data["cards"]:
                        card = models.CardModel.objects.get(pk=card_data["id"])
                        card.sortorder = card_data["sortorder"]
                        card.save()
                return JSONResponse(data["cards"])

        return HttpResponseNotFound() 
Example #8
Source File: graph.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def post(self, request, graphid):
        data = JSONDeserializer().deserialize(request.body)
        self.graph = Graph.objects.get(graphid=graphid)
        with transaction.atomic():
            for item in data:
                functionXgraph, created = models.FunctionXGraph.objects.update_or_create(
                    pk=item["id"], defaults={"function_id": item["function_id"], "graph_id": graphid, "config": item["config"]}
                )
                item["id"] = functionXgraph.pk

                # run post function save hook
                func = functionXgraph.function.get_class_module()()
                try:
                    func.after_function_save(functionXgraph, request)
                except NotImplementedError:
                    pass

        return JSONResponse(data) 
Example #9
Source File: graph.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def post(self, request):
        nodegroupid = None
        try:
            nodegroupid = uuid.UUID(str(request.POST.get("nodegroupid")))
        except Exception as e:
            print(e)
        if self.action == "exportable" and nodegroupid is not None:
            exportable = json.loads(request.POST.get("exportable"))

            nodegroup = models.NodeGroup.objects.select_for_update().filter(nodegroupid=nodegroupid)
            with transaction.atomic():
                for ng in nodegroup:
                    ng.exportable = exportable
                    ng.save()

            return JSONResponse({"nodegroup": nodegroupid, "status": "success"})

        return HttpResponseNotFound() 
Example #10
Source File: mobile_survey.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def delete(self, request, surveyid):
        try:
            connection_error = False
            with transaction.atomic():
                if surveyid is not None:
                    ret = MobileSurvey.objects.get(pk=surveyid)
                    ret.delete()
                    return JSONResponse({"success": True})
        except Exception as e:
            if connection_error is False:
                error_title = _("Unable to delete survey")
                if "strerror" in e and e.strerror == "Connection refused" or "Connection refused" in e:
                    error_message = _("Unable to connect to CouchDB. Please confirm that CouchDB is running")
                else:
                    error_message = e.message
                connection_error = JSONErrorResponse(error_title, error_message)
            return connection_error

        return HttpResponseNotFound() 
Example #11
Source File: concept.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def concept_value(request):
    if request.method == "DELETE":
        data = JSONDeserializer().deserialize(request.body)

        if data:
            with transaction.atomic():
                value = ConceptValue(data)
                value.delete_index()
                value.delete()
                return JSONResponse(value)
    if request.method == "GET":
        valueid = request.GET.get("valueid")
        value = models.Value.objects.get(pk=valueid)
        return JSONResponse(value)

    return HttpResponseNotFound 
Example #12
Source File: packages.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def add_mapbox_layer(
        self, layer_name=False, mapbox_json_path=False, layer_icon="fa fa-globe", is_basemap=False,
    ):
        if layer_name is not False and mapbox_json_path is not False:
            with open(mapbox_json_path) as data_file:
                data = json.load(data_file)
                with transaction.atomic():
                    for layer in data["layers"]:
                        if "source" in layer:
                            layer["source"] = layer["source"] + "-" + layer_name
                    for source_name, source_dict in data["sources"].items():
                        map_source = models.MapSource.objects.get_or_create(name=source_name + "-" + layer_name, source=source_dict)
                    map_layer = models.MapLayer(
                        name=layer_name, layerdefinitions=data["layers"], isoverlay=(not is_basemap), icon=layer_icon
                    )
                    try:
                        map_layer.save()
                    except IntegrityError as e:
                        print("Cannot save layer: {0} already exists".format(layer_name)) 
Example #13
Source File: models.py    From heltour with MIT License 6 votes vote down vote up
def perform_withdrawal(self):
        with transaction.atomic():
            # Set the SeasonPlayer as inactive
            sp, _ = SeasonPlayer.objects.get_or_create(season=self.round.season, player=self.player)
            sp.is_active = False
            sp.save()

            # Delete pairings and give opponents byes
            for pairing in self.round.loneplayerpairing_set.filter(white=self.player):
                PlayerBye.objects.create(round=self.round, player=pairing.black,
                                         type='full-point-pairing-bye')
                pairing.delete()
            for pairing in self.round.loneplayerpairing_set.filter(black=self.player):
                PlayerBye.objects.create(round=self.round, player=pairing.white,
                                         type='full-point-pairing-bye')
                pairing.delete() 
Example #14
Source File: 0028_add_data_location.py    From resolwe with Apache License 2.0 6 votes vote down vote up
def set_data_location(apps, schema_editor):
    """Create DataLocation for each Data."""
    Data = apps.get_model("flow", "Data")
    DataLocation = apps.get_model("flow", "DataLocation")

    for data in Data.objects.all():
        if os.path.isdir(
            os.path.join(settings.FLOW_EXECUTOR["DATA_DIR"], str(data.id))
        ):
            with transaction.atomic():
                # Manually set DataLocation id to preserve data directory.
                data_location = DataLocation.objects.create(
                    id=data.id, subpath=str(data.id)
                )
                data_location.data.add(data)

    # Increment DataLocation id's sequence
    if DataLocation.objects.exists():
        max_id = DataLocation.objects.order_by("id").last().id
        with connection.cursor() as cursor:
            cursor.execute(
                "ALTER SEQUENCE flow_datalocation_id_seq RESTART WITH {};".format(
                    max_id + 1
                )
            ) 
Example #15
Source File: base.py    From resolwe with Apache License 2.0 6 votes vote down vote up
def delete_chunked(queryset, chunk_size=500):
    """Chunked delete, which should be used if deleting many objects.

    The reason why this method is needed is that deleting a lot of Data objects
    requires Django to fetch all of them into memory (fast path is not used) and
    this causes huge memory usage (and possibly OOM).

    :param chunk_size: Optional chunk size
    """
    while True:
        # Discover primary key to limit the current chunk. This is required because delete
        # cannot be called on a sliced queryset due to ordering requirement.
        with transaction.atomic():
            # Get offset of last item (needed because it may be less than the chunk size).
            offset = queryset.order_by("pk")[:chunk_size].count()
            if not offset:
                break

            # Fetch primary key of last item and use it to delete the chunk.
            last_instance = queryset.order_by("pk")[offset - 1]
            queryset.filter(pk__lte=last_instance.pk).delete() 
Example #16
Source File: base.py    From resolwe with Apache License 2.0 6 votes vote down vote up
def save(self, *args, **kwargs):
        """Save the model."""
        name_max_len = self._meta.get_field("name").max_length
        if len(self.name) > name_max_len:
            self.name = self.name[: (name_max_len - 3)] + "..."

        for _ in range(MAX_SLUG_RETRIES):
            try:
                # Attempt to save the model. It may fail due to slug conflict.
                with transaction.atomic():
                    super().save(*args, **kwargs)
                    break
            except IntegrityError as error:
                # Retry in case of slug conflicts.
                if "{}_slug".format(self._meta.db_table) in error.args[0]:
                    self.slug = None
                    continue

                raise
        else:
            raise IntegrityError(
                "Maximum number of retries exceeded during slug generation"
            ) 
Example #17
Source File: mobile_survey.py    From arches with GNU Affero General Public License v3.0 6 votes vote down vote up
def delete(self, request):
        mobile_survey_id = None
        try:
            mobile_survey_id = JSONDeserializer().deserialize(request.body)["id"]
        except Exception as e:
            logger.exception(e)

        try:
            connection_error = False
            with transaction.atomic():
                if mobile_survey_id is not None:
                    ret = MobileSurvey.objects.get(pk=mobile_survey_id)
                    ret.delete()
                    return JSONResponse({"success": True})
        except Exception as e:
            if connection_error is False:
                error_title = _("Unable to delete collector project")
                if "strerror" in e and e.strerror == "Connection refused" or "Connection refused" in e:
                    error_message = _("Unable to connect to CouchDB")
                else:
                    error_message = e.message
                connection_error = JSONResponse({"success": False, "message": error_message, "title": error_title}, status=500)
            return connection_error

        return HttpResponseNotFound() 
Example #18
Source File: base.py    From django-anonymizer with MIT License 6 votes vote down vote up
def _run(anonymizer, objs):
    values = {}
    replacer_attr = tuple(r[0] for r in anonymizer.replacers)
    for obj in objs.iterator():
        retval = anonymizer.alter_object(obj)
        if retval is False:
            continue

        values[obj.pk] = {attname: getattr(obj, attname) for attname in replacer_attr}

    query = anonymizer.create_query(replacer_attr)
    query_args = anonymizer.create_query_args(values, replacer_attr)

    with transaction.atomic():
        with connection.cursor() as cursor:
            if connection.vendor == 'postgresql':
                cursor.execute('SET CONSTRAINTS ALL DEFERRED')
            cursor.executemany(query, query_args) 
Example #19
Source File: concept.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def manage_parents(request, conceptid):
    if request.method == "POST":
        json = request.body
        if json is not None:
            data = JSONDeserializer().deserialize(json)

            with transaction.atomic():
                if len(data["deleted"]) > 0:
                    concept = Concept().get(id=conceptid, include=None)
                    for deleted in data["deleted"]:
                        concept.addparent(deleted)

                    concept.delete()
                    concept.bulk_index()

                if len(data["added"]) > 0:
                    concept = Concept().get(id=conceptid)
                    for added in data["added"]:
                        concept.addparent(added)

                    concept.save()
                    concept.bulk_index()

            return JSONResponse(data)

    else:
        return HttpResponseNotAllowed(["POST"])

    return HttpResponseNotFound() 
Example #20
Source File: models.py    From heltour with MIT License 5 votes vote down vote up
def perform_registration(self):
        with transaction.atomic():
            # Set the SeasonPlayer as active
            sp, _ = SeasonPlayer.objects.get_or_create(season=self.round.season, player=self.player)
            sp.is_active = True
            if sp.seed_rating is None:
                sp.seed_rating = self.player.rating_for(self.round.season.league)
            sp.save()

            # Create any retroactive byes (but don't overwrite existing byes/pairings)
            rounds = self.round.season.round_set.all()
            for i in range(self.retroactive_byes):
                round_number = self.round.number - i - 1
                if round_number < 1:
                    # Too many byes specified, we can just skip them
                    break
                round_ = find(rounds, number=round_number)
                pairings = round_.loneplayerpairing_set.filter(
                    white=self.player) | round_.loneplayerpairing_set.filter(black=self.player)
                byes = round_.playerbye_set.filter(player=self.player)
                if pairings.count() == 0 and byes.count() == 0:
                    PlayerBye.objects.create(round=round_, player=self.player,
                                             type='half-point-bye')

            # Set the late-join points
            score = sp.get_loneplayerscore()
            score.late_join_points = max(score.late_join_points, self.late_join_points)
            score.save() 
Example #21
Source File: api.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def put(self, request, resourceid, slug=None, graphid=None):
        try:
            indent = int(request.PUT.get("indent", None))
        except Exception:
            indent = None

        if not user_can_edit_resource(user=request.user, resourceid=resourceid):
            return JSONResponse(status=403)
        else:
            with transaction.atomic():
                try:
                    # DELETE
                    resource_instance = Resource.objects.get(pk=resourceid)
                    resource_instance.delete()
                except models.ResourceInstance.DoesNotExist:
                    pass

                try:
                    # POST
                    data = JSONDeserializer().deserialize(request.body)
                    reader = JsonLdReader()
                    if slug is not None:
                        graphid = models.GraphModel.objects.get(slug=slug).pk
                    reader.read_resource(data, resourceid=resourceid, graphid=graphid)
                    if reader.errors:
                        response = []
                        for value in reader.errors.values():
                            response.append(value.message)
                        return JSONResponse({"error": response}, indent=indent, status=400)
                    else:
                        response = []
                        for resource in reader.resources:
                            with transaction.atomic():
                                resource.save(request=request)
                            response.append(JSONDeserializer().deserialize(self.get(request, resource.resourceinstanceid).content))
                        return JSONResponse(response, indent=indent, status=201)
                except models.ResourceInstance.DoesNotExist:
                    return JSONResponse(status=404)
                except Exception as e:
                    return JSONResponse({"error": "resource data could not be saved"}, status=500, reason=e) 
Example #22
Source File: pad.py    From raveberry with GNU Lesser General Public License v3.0 5 votes vote down vote up
def submit(self, request: WSGIRequest) -> HttpResponse:
        """Stores a new version of the pad content.
        Makes sure that no conflicts occur."""
        version = request.POST.get("version")
        content = request.POST.get("content")
        if version is None or version == "" or content is None:
            return HttpResponseBadRequest("No version or content supplied")
        try:
            iversion = int(version)
        except ValueError:
            return HttpResponseBadRequest("version is not a number")

        with transaction.atomic():
            pad = models.Pad.objects.get(id=1)
            current_version = pad.version
            if current_version == iversion:
                version_valid = True
                pad.version += 1
                pad.content = content
                pad.save()
            else:
                version_valid = False

        if not version_valid:
            return HttpResponseBadRequest(
                "The content was changed in the meantime, please reload"
            )
        self.update_state()
        return HttpResponse("Updated Pad") 
Example #23
Source File: api.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def post(self, request, resourceid=None, slug=None, graphid=None):
        try:
            indent = int(request.POST.get("indent", None))
        except Exception:
            indent = None

        try:
            if user_can_edit_resource(user=request.user, resourceid=resourceid):
                data = JSONDeserializer().deserialize(request.body)
                reader = JsonLdReader()
                if slug is not None:
                    graphid = models.GraphModel.objects.get(slug=slug).pk
                reader.read_resource(data, graphid=graphid)
                if reader.errors:
                    response = []
                    for value in reader.errors.values():
                        response.append(value.message)
                    return JSONResponse({"error": response}, indent=indent, status=400)
                else:
                    response = []
                    for resource in reader.resources:
                        with transaction.atomic():
                            resource.save(request=request)
                        response.append(JSONDeserializer().deserialize(self.get(request, resource.resourceinstanceid).content))
                    return JSONResponse(response, indent=indent, status=201)
            else:
                return JSONResponse(status=403)
        except Exception as e:
            if settings.DEBUG is True:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                formatted = traceback.format_exception(exc_type, exc_value, exc_traceback)
                if len(formatted):
                    for message in formatted:
                        print(message)
            return JSONResponse({"error": "resource data could not be saved: %s" % e}, status=500, reason=e) 
Example #24
Source File: forms.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def save(self):
        user = User.objects.get(username=self.instance.username)
        with transaction.atomic():
            user.first_name = self.cleaned_data["first_name"]
            user.last_name = self.cleaned_data["last_name"]
            user.email = self.cleaned_data["email"]
            if models.UserProfile.objects.filter(user=user).count() == 0:
                models.UserProfile.objects.create(user=user)
            user.userprofile.phone = self.cleaned_data["phone"]
            user.userprofile.save()
            user.save()
        return user 
Example #25
Source File: graph.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def delete(self):
        if self.is_editable() is True:
            with transaction.atomic():
                for nodegroup in self.get_nodegroups():
                    nodegroup.delete()

                for edge in self.edges.values():
                    edge.delete()

                for node in self.nodes.values():
                    node.delete()

                for card in self.cards.values():
                    card.delete()

                for widget in self.widgets.values():
                    widget.delete()

                super(Graph, self).delete()
        else:
            raise GraphValidationError(
                _(
                    "Your resource model: {0}, already has instances saved. You cannot delete a Resource Model with instances.".format(
                        self.name
                    )
                )
            ) 
Example #26
Source File: concept.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def make_collection(self):
        if len(self.values) == 0:
            raise Exception("Need to include values when creating a collection")
        values = JSONSerializer().serializeToPython(self.values)
        for value in values:
            value["id"] = ""
        collection_concept = Concept({"nodetype": "Collection", "values": values})

        def create_collection(conceptfrom):
            for relation in models.Relation.objects.filter(
                Q(conceptfrom_id=conceptfrom.id),
                Q(relationtype__category="Semantic Relations") | Q(relationtype__category="Properties"),
                ~Q(relationtype="related"),
            ):
                conceptto = Concept(relation.conceptto)
                if conceptfrom == self:
                    collection_concept.add_relation(conceptto, "member")
                else:
                    conceptfrom.add_relation(conceptto, "member")
                create_collection(conceptto)

        with transaction.atomic():
            collection_concept.save()
            create_collection(self)

        return collection_concept 
Example #27
Source File: resource.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def copy(self):
        """
        Returns a copy of this resource instance includeing a copy of all tiles associated with this resource instance

        """
        # need this here to prevent a circular import error
        from arches.app.models.tile import Tile

        id_map = {}
        new_resource = Resource()
        new_resource.graph = self.graph

        if len(self.tiles) == 0:
            self.tiles = Tile.objects.filter(resourceinstance=self)

        for tile in self.tiles:
            new_tile = Tile()
            new_tile.data = tile.data
            new_tile.nodegroup = tile.nodegroup
            new_tile.parenttile = tile.parenttile
            new_tile.resourceinstance = new_resource
            new_tile.sortorder = tile.sortorder

            new_resource.tiles.append(new_tile)
            id_map[tile.pk] = new_tile

        for tile in new_resource.tiles:
            if tile.parenttile:
                tile.parenttile = id_map[tile.parenttile_id]

        with transaction.atomic():
            new_resource.save()

        return new_resource 
Example #28
Source File: controller.py    From raveberry with GNU Lesser General Public License v3.0 5 votes vote down vote up
def remove_all(self, request: WSGIRequest) -> HttpResponse:
        """Empties the queue. Only admin is permitted to do this."""
        if not self.musiq.base.user_manager.is_admin(request.user):
            return HttpResponseForbidden()
        with self.playback.mopidy_command() as allowed:
            if allowed:
                with transaction.atomic():
                    count = self.playback.queue.count()
                    self.playback.queue.all().delete()
                for _ in range(count):
                    self.playback.queue_semaphore.acquire(blocking=False)
        return HttpResponse() 
Example #29
Source File: packages.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def delete_mapbox_layer(self, layer_name=False):
        if layer_name is not False:
            try:
                mapbox_layer = models.MapLayer.objects.get(name=layer_name)
            except ObjectDoesNotExist:
                print('error: no mapbox layer named "{}"'.format(layer_name))
                return
            all_sources = [i.get("source") for i in mapbox_layer.layerdefinitions]
            # remove duplicates and None
            sources = {i for i in all_sources if i}
            with transaction.atomic():
                for source in sources:
                    src = models.MapSource.objects.get(name=source)
                    src.delete()
                mapbox_layer.delete() 
Example #30
Source File: resource.py    From arches with GNU Affero General Public License v3.0 5 votes vote down vote up
def apply_permissions(self, data, user, revert=False):
        with transaction.atomic():
            for instance in data["selectedInstances"]:
                resource_instance = models.ResourceInstance.objects.get(pk=instance["resourceinstanceid"])
                for identity in data["selectedIdentities"]:
                    if identity["type"] == "group":
                        identityModel = Group.objects.get(pk=identity["id"])
                    else:
                        identityModel = User.objects.get(pk=identity["id"])

                    instance_creator = get_instance_creator(resource_instance, user)
                    creator = instance_creator["creatorid"]
                    user_can_modify_permissions = instance_creator["user_can_edit_instance_permissions"]

                    if user_can_modify_permissions:
                        # first remove all the current permissions
                        for perm in get_perms(identityModel, resource_instance):
                            remove_perm(perm, identityModel, resource_instance)

                        if not revert:
                            # then add the new permissions
                            no_access = any(perm["codename"] == "no_access_to_resourceinstance" for perm in identity["selectedPermissions"])
                            if no_access:
                                assign_perm("no_access_to_resourceinstance", identityModel, resource_instance)
                            else:
                                for perm in identity["selectedPermissions"]:
                                    assign_perm(perm["codename"], identityModel, resource_instance)

                resource = Resource(str(resource_instance.resourceinstanceid))
                resource.graph_id = resource_instance.graph_id
                resource.index()