Beispiel #1
0
    def save(self):
        """ Acts as a proxy to the actual save method in the parent class. The
        save method will be called in a ``greenlet`` so ``gevent`` must be
        installed.

        Since the origional request will close the file object we write the
        file to a temporary location on disk and create a new
        :class:`werkzeug.datastructures.FileStorage` instance with the stram
        being the temporary file.
        """

        fp = self.fp
        temp = TemporaryStore(fp)
        path = temp.save()
        filename = self.safe_filename(fp.filename)

        @copy_current_request_context
        def _save():
            self.fp = FileStorage(stream=open(path, 'rb'),
                                  filename=filename,
                                  name=fp.name,
                                  content_type=fp.content_type,
                                  content_length=fp.content_length,
                                  headers=fp.headers)

            super(S3GeventProvider, self).save()

            # Cleanup - Delete the temp file
            os.unlink(path)

        gevent.spawn(_save)

        self.filename = filename
Beispiel #2
0
    def save(self):
        """ Acts as a proxy to the actual save method in the parent class. The
        save method will be called in a ``greenlet`` so ``gevent`` must be
        installed.

        Since the origional request will close the file object we write the
        file to a temporary location on disk and create a new
        :class:`werkzeug.datastructures.FileStorage` instance with the stram
        being the temporary file.
        """

        fp = self.fp
        temp = TemporaryStore(fp)
        path = temp.save()
        filename = self.safe_filename(fp.filename)

        @copy_current_request_context
        def _save():
            self.fp = FileStorage(
                stream=open(path, 'rb'),
                filename=filename,
                name=fp.name,
                content_type=fp.content_type,
                content_length=fp.content_length,
                headers=fp.headers)

            super(S3GeventProvider, self).save()

            # Cleanup - Delete the temp file
            os.unlink(path)

        gevent.spawn(_save)

        self.filename = filename
Beispiel #3
0
def upload_recording(campaign_id):
    campaign = Campaign.query.filter_by(id=campaign_id).first_or_404()
    form = AudioRecordingForm()

    if form.validate_on_submit():
        message_key = form.data.get('key')

        # get highest version for this key to date
        last_version = db.session.query(db.func.max(AudioRecording.version)) \
            .filter_by(key=message_key) \
            .scalar()

        recording = AudioRecording()
        form.populate_obj(recording)
        recording.hidden = False
        recording.version = int(last_version or 0) + 1

        # save uploaded file to storage
        file_storage = request.files.get('file_storage')
        if file_storage:
            file_storage.filename = "campaign_{}_{}_{}.mp3".format(campaign.id, message_key, recording.version)
            recording.file_storage = file_storage
        else:
            # dummy file storage
            recording.file_storage = TemporaryStore('')
            # save text-to-speech instead
            recording.text_to_speech = form.data.get('text_to_speech')

        db.session.add(recording)

        # unset selected for all other versions
        # disable autoflush to avoid errors with empty recording file_storage
        with db.session.no_autoflush:
            other_versions = CampaignAudioRecording.query.filter(
                CampaignAudioRecording.campaign_id == campaign_id,
                CampaignAudioRecording.recording.has(key=message_key)).all()
        for v in other_versions:
            # reset empty storages
            if ((not hasattr(v, 'file_storage')) or
                 (v.file_storage is None) or
                 (type(v.file_storage) is TemporaryStore)):
                # create new dummy store
                v.file_storage = TemporaryStore('')
            v.selected = False
            db.session.add(v)
        db.session.commit()

        # link this recording to campaign through m2m, and set selected flag
        campaignRecording = CampaignAudioRecording(campaign_id=campaign.id, recording=recording)
        campaignRecording.selected = True

        db.session.add(campaignRecording)
        db.session.commit()

        message = "Audio recording uploaded"
        return jsonify({'success': True, 'message': message,
                        'key': message_key, 'version': recording.version})
    else:
        return jsonify({'success': False, 'errors': form.errors})