Exemple #1
0
def autoupdate(mod_id):
    mod = Mod.query.filter(Mod.id == mod_id).first()
    if not mod:
        abort(404)
    game = Game.query.filter(Game.id == mod.game_id).first()
    session['game'] = game.id;
    session['gamename'] = game.name;
    session['gameshort'] = game.short;
    session['gameid'] = game.id;
    if not mod or not game:
        ga = Game.query.filter(Game.short == 'kerbal-space-program').order_by(desc(Game.id)).first()
        session['game'] = ga.id;
        session['gamename'] = ga.name;
        session['gameshort'] = ga.short;
        session['gameid'] = ga.id;
        abort(404)
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod.user_id:
            editable = True
        if any([u.accepted and u.user == current_user for u in mod.shared_authors]):
            editable = True
    if not editable:
        abort(401)
    default = mod.default_version()
    default.gameversion_id = GameVersion.query.filter(GameVersion.game_id == mod.game_id).order_by(desc(GameVersion.id)).first().id
    send_autoupdate_notification(mod)
    notify_ckan.delay(mod_id, 'version-update')
    return redirect(url_for("mods.mod", id=mod.id, mod_name=mod.name,ga=game))
Exemple #2
0
def delete(mod_id):
    mod = Mod.query.filter(Mod.id == mod_id).first()
    if not mod:
        abort(404)
    game = Game.query.filter(Game.id == mod.game_id).first()
    session['game'] = game.id
    session['gamename'] = game.name
    session['gameshort'] = game.short
    session['gameid'] = game.id
    if not mod or not game:
        ga = Game.query.filter(Game.short == 'kerbal-space-program').order_by(
            desc(Game.id)).first()
        session['game'] = ga.id
        session['gamename'] = ga.name
        session['gameshort'] = ga.short
        session['gameid'] = ga.id
        abort(404)
    else:
        session['game'] = game.id
        session['gamename'] = game.name
        session['gameshort'] = game.short
        session['gameid'] = game.id
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod.user_id:
            editable = True
    if not editable:
        abort(401)
    db.delete(mod)
    for feature in Featured.query.filter(Featured.mod_id == mod.id).all():
        db.delete(feature)
    for media in Media.query.filter(Media.mod_id == mod.id).all():
        db.delete(media)
    for version in ModVersion.query.filter(ModVersion.mod_id == mod.id).all():
        db.delete(version)
    base_path = os.path.join(
        secure_filename(mod.user.username) + '_' + str(mod.user.id),
        secure_filename(mod.name))
    full_path = os.path.join(_cfg('storage'), base_path)
    db.commit()
    notify_ckan.delay(mod_id, 'delete')
    rmtree(full_path)
    return redirect("/profile/" + current_user.username)
Exemple #3
0
def delete(mod_id):
    mod = Mod.query.filter(Mod.id == mod_id).first()
    if not mod:
        abort(404)
    game = Game.query.filter(Game.id == mod.game_id).first()
    session['game'] = game.id;
    session['gamename'] = game.name;
    session['gameshort'] = game.short;
    session['gameid'] = game.id;
    if not mod or not game:
        ga = Game.query.filter(Game.short == 'kerbal-space-program').order_by(desc(Game.id)).first()
        session['game'] = ga.id;
        session['gamename'] = ga.name;
        session['gameshort'] = ga.short;
        session['gameid'] = ga.id;
        abort(404)
    else:
        session['game'] = game.id;
        session['gamename'] = game.name;
        session['gameshort'] = game.short;
        session['gameid'] = game.id;
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod.user_id:
            editable = True
    if not editable:
        abort(401)
    db.delete(mod)
    for feature in Featured.query.filter(Featured.mod_id == mod.id).all():
        db.delete(feature)
    for media in Media.query.filter(Media.mod_id == mod.id).all():
        db.delete(media)
    for version in ModVersion.query.filter(ModVersion.mod_id == mod.id).all():
        db.delete(version)
    base_path = os.path.join(secure_filename(mod.user.username) + '_' + str(mod.user.id), secure_filename(mod.name))
    full_path = os.path.join(_cfg('storage'), base_path)
    db.commit()
    notify_ckan.delay(mod_id, 'delete')
    rmtree(full_path)
    return redirect("/profile/" + current_user.username)
Exemple #4
0
def autoupdate(mod_id):
    mod = Mod.query.filter(Mod.id == mod_id).first()
    if not mod:
        abort(404)
    game = Game.query.filter(Game.id == mod.game_id).first()
    session['game'] = game.id
    session['gamename'] = game.name
    session['gameshort'] = game.short
    session['gameid'] = game.id
    if not mod or not game:
        ga = Game.query.filter(Game.short == 'kerbal-space-program').order_by(
            desc(Game.id)).first()
        session['game'] = ga.id
        session['gamename'] = ga.name
        session['gameshort'] = ga.short
        session['gameid'] = ga.id
        abort(404)
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod.user_id:
            editable = True
        if any([
                u.accepted and u.user == current_user
                for u in mod.shared_authors
        ]):
            editable = True
    if not editable:
        abort(401)
    default = mod.default_version()
    default.gameversion_id = GameVersion.query.filter(
        GameVersion.game_id == mod.game_id).order_by(desc(
            GameVersion.id)).first().id
    send_autoupdate_notification(mod)
    notify_ckan.delay(mod_id, 'version-update')
    return redirect(url_for("mods.mod", id=mod.id, mod_name=mod.name, ga=game))
Exemple #5
0
def update_mod(mod_id):
    if current_user == None:
        return {"error": True, "reason": "You are not logged in."}, 401
    mod = Mod.query.filter(Mod.id == mod_id).first()
    if not mod:
        return {"error": True, "reason": "Mod not found."}, 404
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod.user_id:
            editable = True
        if any([u.accepted and u.user == current_user for u in mod.shared_authors]):
            editable = True
    if not editable:
        return {"error": True, "reason": "Not enought rights."}, 401
    version = request.form.get("version")
    changelog = request.form.get("changelog")
    game_version = request.form.get("game-version")
    notify = request.form.get("notify-followers")
    zipball = request.files.get("zipball")
    if not version or not game_version or not zipball:
        # Client side validation means that they're just being pricks if they
        # get here, so we don't need to show them a pretty error reason
        # SMILIE: this doesn't account for "external" API use --> return a json error
        return {"error": True, "reason": "All fields are required."}, 400
    test_gameversion = (
        GameVersion.query.filter(GameVersion.game_id == Mod.game_id)
        .filter(GameVersion.friendly_version == game_version)
        .first()
    )
    if not test_gameversion:
        return {"error": True, "reason": "Game version does not exist."}, 400
    game_version_id = test_gameversion.id
    if notify == None:
        notify = False
    else:
        notify = notify.lower() == "true" or notify.lower() == "yes"
    filename = secure_filename(mod.name) + "-" + secure_filename(version) + ".zip"
    base_path = os.path.join(
        secure_filename(current_user.username) + "_" + str(current_user.id), secure_filename(mod.name)
    )
    full_path = os.path.join(_cfg("storage"), base_path)
    if not os.path.exists(full_path):
        os.makedirs(full_path)
    path = os.path.join(full_path, filename)
    for v in mod.versions:
        if v.friendly_version == secure_filename(version):
            return {"error": True, "reason": "We already have this version. Did you mistype the version number?"}, 400
    if os.path.isfile(path):
        os.remove(path)
    zipball.save(path)
    if not zipfile.is_zipfile(path):
        os.remove(path)
        return {"error": True, "reason": "This is not a valid zip file."}, 400
    version = ModVersion(secure_filename(version), game_version_id, os.path.join(base_path, filename))
    version.changelog = changelog
    # Assign a sort index
    if len(mod.versions) == 0:
        version.sort_index = 0
    else:
        version.sort_index = max([v.sort_index for v in mod.versions]) + 1
    mod.versions.append(version)
    mod.updated = datetime.now()
    if notify:
        send_update_notification(mod, version, current_user)
    db.add(version)
    db.commit()
    mod.default_version_id = version.id
    db.commit()
    notify_ckan.delay(mod_id, "update")
    return {"url": url_for("mods.mod", id=mod.id, mod_name=mod.name), "id": version.id}
Exemple #6
0
def create_mod():
    if not current_user:
        return {"error": True, "reason": "You are not logged in."}, 401
    if not current_user.public:
        return {"error": True, "reason": "Only users with public profiles may create mods."}, 403
    name = request.form.get("name")
    game = request.form.get("game")
    short_description = request.form.get("short-description")
    version = request.form.get("version")
    game_version = request.form.get("game-version")
    license = request.form.get("license")
    ckan = request.form.get("ckan")
    zipball = request.files.get("zipball")
    # Validate
    if not name or not short_description or not version or not game or not game_version or not license or not zipball:
        return {"error": True, "reason": "All fields are required."}, 400
    # Validation, continued
    if len(name) > 100 or len(short_description) > 1000 or len(license) > 128:
        return {"error": True, "reason": "Fields exceed maximum permissible length."}, 400
    if ckan == None:
        ckan = False
    else:
        ckan = ckan.lower() == "true" or ckan.lower() == "yes" or ckan.lower() == "on"
    test_game = Game.query.filter(Game.id == game).first()
    if not test_game:
        return {"error": True, "reason": "Game does not exist."}, 400
    test_gameversion = (
        GameVersion.query.filter(GameVersion.game_id == test_game.id)
        .filter(GameVersion.friendly_version == game_version)
        .first()
    )
    if not test_gameversion:
        return {"error": True, "reason": "Game version does not exist."}, 400
    game_version_id = test_gameversion.id
    mod = Mod()
    mod.user = current_user
    mod.name = name
    mod.game_id = game
    mod.short_description = short_description
    mod.description = default_description
    mod.ckan = ckan
    mod.license = license
    # Save zipball
    filename = secure_filename(name) + "-" + secure_filename(version) + ".zip"
    base_path = os.path.join(secure_filename(current_user.username) + "_" + str(current_user.id), secure_filename(name))
    full_path = os.path.join(_cfg("storage"), base_path)
    if not os.path.exists(full_path):
        os.makedirs(full_path)
    path = os.path.join(full_path, filename)
    if os.path.isfile(path):
        # We already have this version
        # We'll remove it because the only reason it could be here on creation is an error
        os.remove(path)
    zipball.save(path)
    if not zipfile.is_zipfile(path):
        os.remove(path)
        return {"error": True, "reason": "This is not a valid zip file."}, 400
    version = ModVersion(secure_filename(version), game_version_id, os.path.join(base_path, filename))
    mod.versions.append(version)
    db.add(version)
    # Save database entry
    db.add(mod)
    db.commit()
    mod.default_version_id = version.id
    db.commit()
    ga = Game.query.filter(Game.id == game).first()
    session["game"] = ga.id
    session["gamename"] = ga.name
    session["gameshort"] = ga.short
    session["gameid"] = ga.id
    notify_ckan.delay(mod.id, "create")
    return {"url": url_for("mods.mod", id=mod.id, mod_name=mod.name), "id": mod.id, "name": mod.name}
Exemple #7
0
def update_mod(mod_id):
    if current_user == None:
        return {'error': True, 'reason': 'You are not logged in.'}, 401
    mod = Mod.query.filter(Mod.id == mod_id).first()
    if not mod:
        return {'error': True, 'reason': 'Mod not found.'}, 404
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod.user_id:
            editable = True
        if any([
                u.accepted and u.user == current_user
                for u in mod.shared_authors
        ]):
            editable = True
    if not editable:
        return {'error': True, 'reason': 'Not enought rights.'}, 401
    version = request.form.get('version')
    changelog = request.form.get('changelog')
    game_version = request.form.get('game-version')
    notify = request.form.get('notify-followers')
    zipball = request.files.get('zipball')
    if not version \
        or not game_version \
        or not zipball:
        # Client side validation means that they're just being pricks if they
        # get here, so we don't need to show them a pretty error reason
        # SMILIE: this doesn't account for "external" API use --> return a json error
        return {'error': True, 'reason': 'All fields are required.'}, 400
    test_gameversion = GameVersion.query.filter(
        GameVersion.game_id == Mod.game_id).filter(
            GameVersion.friendly_version == game_version).first()
    if not test_gameversion:
        return {'error': True, 'reason': 'Game version does not exist.'}, 400
    game_version_id = test_gameversion.id
    if notify == None:
        notify = False
    else:
        notify = (notify.lower() == "true" or notify.lower() == "yes")
    filename = secure_filename(
        mod.name) + '-' + secure_filename(version) + '.zip'
    base_path = os.path.join(
        secure_filename(current_user.username) + '_' + str(current_user.id),
        secure_filename(mod.name))
    full_path = os.path.join(_cfg('storage'), base_path)
    if not os.path.exists(full_path):
        os.makedirs(full_path)
    path = os.path.join(full_path, filename)
    for v in mod.versions:
        if v.friendly_version == secure_filename(version):
            return {
                'error':
                True,
                'reason':
                'We already have this version. Did you mistype the version number?'
            }, 400
    if os.path.isfile(path):
        os.remove(path)
    zipball.save(path)
    if not zipfile.is_zipfile(path):
        os.remove(path)
        return {'error': True, 'reason': 'This is not a valid zip file.'}, 400
    version = ModVersion(secure_filename(version), game_version_id,
                         os.path.join(base_path, filename))
    version.changelog = changelog
    # Assign a sort index
    if len(mod.versions) == 0:
        version.sort_index = 0
    else:
        version.sort_index = max([v.sort_index for v in mod.versions]) + 1
    mod.versions.append(version)
    mod.updated = datetime.now()
    if notify:
        send_update_notification(mod, version, current_user)
    db.add(version)
    db.commit()
    mod.default_version_id = version.id
    db.commit()
    notify_ckan.delay(mod_id, 'update')
    return {
        'url': url_for("mods.mod", id=mod.id, mod_name=mod.name),
        "id": version.id
    }
Exemple #8
0
def create_mod():
    if not current_user:
        return {'error': True, 'reason': 'You are not logged in.'}, 401
    if not current_user.public:
        return {
            'error': True,
            'reason': 'Only users with public profiles may create mods.'
        }, 403
    name = request.form.get('name')
    game = request.form.get('game')
    short_description = request.form.get('short-description')
    version = request.form.get('version')
    game_version = request.form.get('game-version')
    license = request.form.get('license')
    ckan = request.form.get('ckan')
    zipball = request.files.get('zipball')
    # Validate
    if not name \
        or not short_description \
        or not version \
        or not game \
        or not game_version \
        or not license \
        or not zipball:
        return {'error': True, 'reason': 'All fields are required.'}, 400
    # Validation, continued
    if len(name) > 100 \
        or len(short_description) > 1000 \
        or len(license) > 128:
        return {
            'error': True,
            'reason': 'Fields exceed maximum permissible length.'
        }, 400
    if ckan == None:
        ckan = False
    else:
        ckan = (ckan.lower() == "true" or ckan.lower() == "yes"
                or ckan.lower() == "on")
    test_game = Game.query.filter(Game.id == game).first()
    if not test_game:
        return {'error': True, 'reason': 'Game does not exist.'}, 400
    test_gameversion = GameVersion.query.filter(
        GameVersion.game_id == test_game.id).filter(
            GameVersion.friendly_version == game_version).first()
    if not test_gameversion:
        return {'error': True, 'reason': 'Game version does not exist.'}, 400
    game_version_id = test_gameversion.id
    mod = Mod()
    mod.user = current_user
    mod.name = name
    mod.game_id = game
    mod.short_description = short_description
    mod.description = default_description
    mod.ckan = ckan
    mod.license = license
    # Save zipball
    filename = secure_filename(name) + '-' + secure_filename(version) + '.zip'
    base_path = os.path.join(
        secure_filename(current_user.username) + '_' + str(current_user.id),
        secure_filename(name))
    full_path = os.path.join(_cfg('storage'), base_path)
    if not os.path.exists(full_path):
        os.makedirs(full_path)
    path = os.path.join(full_path, filename)
    if os.path.isfile(path):
        # We already have this version
        # We'll remove it because the only reason it could be here on creation is an error
        os.remove(path)
    zipball.save(path)
    if not zipfile.is_zipfile(path):
        os.remove(path)
        return {'error': True, 'reason': 'This is not a valid zip file.'}, 400
    version = ModVersion(secure_filename(version), game_version_id,
                         os.path.join(base_path, filename))
    mod.versions.append(version)
    db.add(version)
    # Save database entry
    db.add(mod)
    db.commit()
    mod.default_version_id = version.id
    db.commit()
    ga = Game.query.filter(Game.id == game).first()
    session['game'] = ga.id
    session['gamename'] = ga.name
    session['gameshort'] = ga.short
    session['gameid'] = ga.id
    notify_ckan.delay(mod.id, 'create')
    return {
        'url': url_for("mods.mod", id=mod.id, mod_name=mod.name),
        "id": mod.id,
        "name": mod.name
    }