Example #1
0
def edit():
    pcfg = {"title": gettext("Edit my profile")}

    user = User.query.filter(User.id == current_user.id).first()
    if not user:
        flash(gettext("User not found"), "error")
        return redirect(url_for("bp_main.home"))

    form = UserProfileForm(request.form, obj=user)
    form.timezone.choices = [[str(i), str(i)] for i in pytz.all_timezones]

    if form.validate_on_submit():
        user.timezone = form.timezone.data
        user.locale = form.locale.data
        user.display_name = form.display_name.data

        db.session.commit()

        # log
        add_user_log(user.id, user.id, "user", "info", "Edited user profile")

        flash(gettext("Profile updated"), "success")

        return redirect(url_for("bp_users.profile", name=user.name))

    return render_template("users/edit.jinja2",
                           pcfg=pcfg,
                           form=form,
                           user=user)
Example #2
0
def delete(username, soundslug):
    sound = Sound.query.filter(Sound.user_id == current_user.id,
                               Sound.slug == soundslug).first()
    if not sound:
        flash(gettext("Sound not found"), "error")
        return redirect(url_for("bp_users.profile", name=username))

    if sound.user.id != current_user.id:
        flash(gettext("Forbidden"), "error")
        return redirect(url_for("bp_users.profile", name=username))

    # Federate Delete
    from tasks import federate_delete_sound

    federate_delete_sound(sound)

    db.session.delete(sound)
    db.session.commit()

    # log
    add_user_log(sound.id, sound.user.id, "sounds", "info",
                 "Deleted {0} -- {1}".format(sound.id, sound.title))

    return redirect(url_for("bp_users.profile", name=username))
Example #3
0
def work_transcode(sound_id):
    sound = Sound.query.get(sound_id)
    if not sound:
        print("- Cant find sound ID {id} in database".format(id=sound_id))
        return
    if not sound.transcode_needed:
        print("- Sound ID {id} doesn't need transcoding".format(id=sound_id))
        sound.transcode_state = Sound.TRANSCODE_DONE
        db.session.commit()
        add_user_log(
            sound.id,
            sound.user.id,
            "sounds",
            "info",
            "Transcoding not needed for: {0} -- {1}".format(
                sound.id, sound.title),
        )
        return
    if not sound.transcode_state == Sound.TRANSCODE_WAITING:
        print("- Sound ID {id} transcoding != TRANSCODE_WAITING".format(
            id=sound_id))
        return

    print("File: {0}: {1}".format(sound.id, sound.title))
    add_user_log(
        sound.id, sound.user.id, "sounds", "info",
        "Transcoding started for: {0} -- {1}".format(sound.id, sound.title))

    fname = os.path.join(current_app.config["UPLOADED_SOUNDS_DEST"],
                         sound.user.slug, sound.filename)
    _file, _ext = splitext(fname)

    _start = time.time()

    a = AudioSegment.from_file(fname)
    a.export("{0}.mp3".format(_file), format="mp3", bitrate="196k")

    print("From: {0}".format(fname))
    print("Transcoded: {0}.mp3".format(_file))
    elapsed = time.time() - _start
    print("Transcoding done: ({0}) {1}".format(elapsed,
                                               duration_song_human(elapsed)))

    sound.transcode_state = Sound.TRANSCODE_DONE

    info = sound.sound_infos.first()
    info.done_waveform = False

    _a, _b = splitext(sound.filename)
    sound.filename_transcoded = "{0}.mp3".format(_a)
    db.session.commit()

    add_user_log(
        sound.id, sound.user.id, "sounds", "info",
        "Transcoding finished for: {0} -- {1}".format(sound.id, sound.title))
Example #4
0
def upload():
    pcfg = {"title": gettext("New upload")}
    user = User.query.filter(User.id == current_user.id).one()

    form = SoundUploadForm()

    if request.method == "POST" and "sound" in request.files:
        if form.validate_on_submit():
            filename_orig = request.files["sound"].filename
            filename_hashed = get_hashed_filename(filename_orig)

            sounds.save(request.files["sound"],
                        folder=user.slug,
                        name=filename_hashed)

            rec = Sound()
            rec.filename = filename_hashed
            rec.filename_orig = filename_orig
            rec.licence = form.licence.data
            if form.album.data:
                rec.album_id = form.album.data.id
                if not form.album.data.sounds:
                    rec.album_order = 0
                else:
                    rec.album_order = form.album.data.sounds.count() + 1

            rec.user_id = current_user.id
            if not form.title.data:
                rec.title = filename_orig
            else:
                rec.title = form.title.data
            rec.description = form.description.data
            rec.private = form.private.data

            if "flac" in request.files[
                    "sound"].mimetype or "ogg" in request.files[
                        "sound"].mimetype:
                rec.transcode_state = Sound.TRANSCODE_WAITING
                rec.transcode_needed = True

            db.session.add(rec)
            db.session.commit()

            # push the job in queue
            from tasks import upload_workflow

            upload_workflow.delay(rec.id)

            # log
            add_user_log(rec.id, user.id, "sounds", "info",
                         "Uploaded {0} -- {1}".format(rec.id, rec.title))

            flash(gettext("Uploaded ! Processing will now follow."), "success")
        else:
            return render_template("sound/upload.jinja2",
                                   pcfg=pcfg,
                                   form=form,
                                   flash="Error with the file")
        return redirect(
            url_for("bp_sound.show",
                    username=current_user.name,
                    soundslug=rec.slug))

    # GET
    return render_template("sound/upload.jinja2", pcfg=pcfg, form=form)
Example #5
0
def edit(username, soundslug):
    sound = Sound.query.filter(Sound.user_id == current_user.id,
                               Sound.slug == soundslug).first()
    if not sound:
        flash(gettext("Sound not found"), "error")
        return redirect(url_for("bp_users.profile", name=username))

    if sound.user.id != current_user.id:
        flash(gettext("Forbidden"), "error")
        return redirect(url_for("bp_users.profile", name=username))

    pcfg = {"title": gettext("Edit %(title)s", title=sound.title)}

    form = SoundEditForm(request.form, obj=sound)

    federate_new = False

    if form.validate_on_submit():
        if sound.private and not form.private.data:
            # Switched to public
            federate_new = True
            sound.private = form.private.data

        if not sound.private and form.private.data:
            # Can't switch back to private
            sound.private = False

        sound.title = form.title.data
        sound.description = form.description.data
        sound.licence = form.licence.data
        if form.album.data:
            sound.album_id = form.album.data.id
            if not sound.album_order:
                if not form.album.data.sounds:
                    sound.album_order = 0
                else:
                    sound.album_order = form.album.data.sounds.count() + 1

        db.session.commit()
        # log
        add_user_log(sound.id, sound.user.id, "sounds", "info",
                     "Edited {0} -- {1}".format(sound.id, sound.title))

        if federate_new:
            # Switched from private to public: initial federation

            from tasks import federate_new_sound

            sound.activity_id = federate_new_sound(sound)
            db.session.commit()

        else:
            # it's an update
            from tasks import send_update_sound

            send_update_sound(sound)

        return redirect(
            url_for("bp_sound.show", username=username, soundslug=sound.slug))
    else:
        form.private.data = sound.private

    if not sound.private:
        del form.private

    return render_template("sound/edit.jinja2",
                           pcfg=pcfg,
                           form=form,
                           sound=sound)
Example #6
0
 def log_reset_password_instr(sender, user, token):
     if not user:
         return
     add_user_log(user.id, user.id, "user", "info",
                  "Password reset instructions sent.")
Example #7
0
 def log_password_reset(sender, user):
     if not user:
         return
     add_user_log(user.id, user.id, "user", "info",
                  "Your password has been changed !")
Example #8
0
def work_metadatas(sound_id, force=False):
    sound = Sound.query.get(sound_id)
    if not sound:
        print("- Cant find sound ID %(id)s in database".format(id=sound_id))
        return

    add_user_log(
        sound.id,
        sound.user.id,
        "sounds",
        "info",
        "Metadatas gathering started for: {0} -- {1}".format(
            sound.id, sound.title),
    )

    _infos = sound.sound_infos.first()

    if not _infos:
        _infos = SoundInfo()
        _infos.sound_id = sound.id

    # Generate Basic infos

    fname = os.path.join(current_app.config["UPLOADED_SOUNDS_DEST"],
                         sound.user.slug, sound.filename)

    if not _infos.done_basic or force:
        print("- WORKING BASIC on {0}, {1}".format(sound.id, sound.filename))
        basic_infos = get_basic_infos(fname)
        print("- Our file got basic infos: {0}".format(basic_infos))
        _infos.duration = basic_infos["duration"]
        _infos.channels = basic_infos["channels"]
        _infos.rate = basic_infos["rate"]
        _infos.codec = basic_infos["codec"]
        _infos.format = basic_infos["format"]
        _infos.bitrate = basic_infos["bitrate"]
        _infos.bitrate_mode = basic_infos["bitrate_mode"]
        _infos.done_basic = True
        _infos.type = basic_infos["type"]
        _infos.type_human = basic_infos["type_human"]

    if not _infos.done_waveform or force:
        if sound.transcode_state == Sound.TRANSCODE_DONE:
            _f, _e = splitext(fname)
            fname_t = "{0}.mp3".format(_f)
            print("- WORKING ON TRANSCODED FOR WAVEFORM")
        else:
            fname_t = fname

        print("- GENERATING AUDIO DAT FILE")
        dat_file_name = generate_audio_dat_file(fname_t)

        print("- WORKING WAVEFORM on {0}, {1}".format(sound.id,
                                                      sound.filename))
        waveform_infos = get_waveform_infos(dat_file_name)
        print("- Our file got waveform infos: {0}".format(waveform_infos))
        _infos.waveform = waveform_infos
        if not waveform_infos:
            _infos.waveform_error = True
            add_user_log(
                sound.id,
                sound.user.id,
                "sounds",
                "info",
                "Got an error when generating waveform"
                " for: {0} -- {1}".format(sound.id, sound.title),
            )
        else:
            fdir_wf = os.path.join(current_app.config["UPLOADS_DEFAULT_DEST"],
                                   "waveforms", sound.user.slug)
            fname_wf = os.path.join(fdir_wf, sound.filename)

            if not os.path.isdir(fdir_wf):
                os.makedirs(fdir_wf)

            create_png_waveform(dat_file_name, fname_wf)

        # Delete the temporary dat file
        os.unlink(dat_file_name)

        _infos.done_waveform = True

    db.session.add(_infos)
    db.session.commit()

    add_user_log(
        sound.id,
        sound.user.id,
        "sounds",
        "info",
        "Metadatas gathering finished for: {0} -- {1}".format(
            sound.id, sound.title),
    )