def update_document(doc, sigh):
    prepped = contenttypes.prep_state(doc)

    with transaction.atomic():
        try:
            doc.state.delete()
            doc.raw_state.delete()
        except:
            pass

        raw_state, state = contenttypes.get_state(prepped)

        doc.raw_state = raw_state
        doc.state = state

        raw_state.save()
        state.save()
        doc.save()

        if sigh:
            raw_state, state = contenttypes.get_state(prepped)

            pub = models.Publication()
            pub.raw_state = raw_state
            pub.state = state
            pub.creator = models.NUser.objects.get(pk=1)
            pub.preliminary = True
            pub.create_date = datetime.datetime(2000, 1, 1)
            pub.episode = doc.episode
            pub.save()
    def publications(self, request, pk=None):
        document = get_object_or_404(models.Document, pk=pk)

        if not hasattr(document, 'episode'):
            return Response(status=status.HTTP_400_BAD_REQUEST)

        episode = document.episode

        if request.method == 'POST':
            if not request.user.is_authenticated() or not request.user.has_perm('o_publish_episode', episode):
                return Response(status=status.HTTP_403_FORBIDDEN)

            podcasters = request.DATA['podcasters']
            request.DATA['podcasters'] = []

            cover = None
            if 'cover' in request.DATA:
                cover = request.DATA['cover']
                del request.DATA['cover']

            request.DATA['create_date'] = datetime.now()

            serialized = serializers.PublicationSerializer(data=request.DATA)

            if not serialized.is_valid():
                return Response(serialized.errors, status=status.HTTP_400_BAD_REQUEST)

            pub = serialized.object

            prepped = contenttypes.prep_state(document)

            with transaction.atomic():
                raw_state, state = contenttypes.get_state(prepped)
                state.save()

                if cover is not None:
                    if cover['id'] == 'new':
                        cover = models.Cover.from_url(request.user, cover['file'])
                    else:
                        cover = models.Cover.objects.get(pk=cover['id'])

                    episode.cover = cover
                    episode.save()

                pub.creator = request.user
                pub.state = state
                pub.raw_state = raw_state
                pub.episode = episode

                pub.save()

                pub.shownoters.add(*document.meta.shownoters.all())

                episode.publicationrequests.all().delete()

            return Response(status=status.HTTP_201_CREATED)
        elif request.method == 'GET':
            return Response(episode.publications)
def update_document(doc, sigh):
    # execute parser
    prepped = contenttypes.prep_state(doc)

    #TODO reduce write access on contenttypes.get_state
    # Two states - sight and live
    with transaction.atomic():
        raw_state, state = contenttypes.get_state(prepped)

        raw_state.save()
        state.save()

        _delete_old_states(doc)
        _save_new_state_to_doc(doc, raw_state, state)

        if sigh:
            pub = models.Publication()
            _save_state_to_publication(pub, doc.episode, raw_state, state)
Example #4
0
def update_document(doc, sigh):
    # execute parser
    prepped = contenttypes.prep_state(doc)

    #TODO reduce write access on contenttypes.get_state
    # Two states - sight and live
    with transaction.atomic():
        raw_state, state = contenttypes.get_state(prepped)

        raw_state.save()
        state.save()

        _delete_old_states(doc)
        _save_new_state_to_doc(doc, raw_state, state)

        if sigh:
            pub = models.Publication()
            _save_state_to_publication(pub, doc.episode, raw_state, state)
    def publications(self, request, pk=None):
        document = get_object_or_404(models.Document, pk=pk)

        if not hasattr(document, 'episode'):
            return Response(status=status.HTTP_400_BAD_REQUEST)

        episode = document.episode

        if request.method == 'POST':
            if not request.user.is_authenticated(
            ) or not request.user.has_perm('o_publish_episode', episode):
                return Response(status=status.HTTP_403_FORBIDDEN)

            podcasters = request.DATA['podcasters']
            request.DATA['podcasters'] = []

            cover = None
            if 'cover' in request.DATA:
                cover = request.DATA['cover']
                del request.DATA['cover']

            request.DATA['create_date'] = datetime.now()

            serialized = serializers.PublicationSerializer(data=request.DATA)

            if not serialized.is_valid():
                return Response(serialized.errors,
                                status=status.HTTP_400_BAD_REQUEST)

            pub = serialized.object

            prepped = contenttypes.prep_state(document)

            with transaction.atomic():
                raw_state, state = contenttypes.get_state(prepped)
                state.save()

                if cover is not None:
                    if cover['id'] == 'new':
                        cover = models.Cover.from_url(request.user,
                                                      cover['file'])
                    else:
                        cover = models.Cover.objects.get(pk=cover['id'])

                    episode.cover = cover
                    episode.save()

                pub.creator = request.user
                pub.state = state
                pub.raw_state = raw_state
                pub.episode = episode

                pub.save()

                pub.shownoters.add(*document.meta.shownoters.all())

                episode.publicationrequests.all().delete()

            return Response(status=status.HTTP_201_CREATED)
        elif request.method == 'GET':
            return Response(episode.publications)