Ejemplo n.º 1
0
def register_with_oauth_authorized():
    '''
    This endpoint should be called after authorizing with oauth, by the user.
    '''
    email = request.form.get('email')
    username = request.form.get('username')
    provider = request.form.get('provider')
    remote_user = request.form.get('remote_user')

    good = True
    if check_username_for_registration(username):
        good = False
    if check_email_for_registration(email):
        good = False

    if good:
        password = binascii.b2a_hex(os.urandom(99))
        user = User(username, email, password)
        user.confirmation = binascii.b2a_hex(os.urandom(20)).decode("utf-8")
        db.add(user)
        db.flush()  # to get an ID.
        auth = UserAuth(user.id, remote_user, provider)
        db.add(auth)
        db.commit()  # Commit before trying to email

        send_confirmation(user)
        return redirect("/account-pending")

    return render_register_with_oauth(provider, remote_user, username, email)
Ejemplo n.º 2
0
def register_with_oauth_authorized():
    '''
    This endpoint should be called after authorizing with oauth, by the user.
    '''
    email = request.form.get('email')
    username = request.form.get('username')
    provider = request.form.get('provider')
    remote_user = request.form.get('remote_user')

    good = True
    if check_username_for_registration(username):
        good = False
    if check_email_for_registration(email):
        good = False

    if good:
        password = binascii.b2a_hex(os.urandom(99))
        user = User(username, email, password)
        user.confirmation = binascii.b2a_hex(os.urandom(20)).decode("utf-8")
        db.add(user)
        db.flush()  # to get an ID.
        auth = UserAuth(user.id, remote_user, provider)
        db.add(auth)
        db.commit()  # Commit before trying to email

        send_confirmation(user)
        return redirect("/account-pending")

    return render_register_with_oauth(provider, remote_user, username, email)
Ejemplo n.º 3
0
def post_blog():
    title = request.form.get('post-title')
    body = request.form.get('post-body')
    post = BlogPost()
    post.title = title
    post.text = body
    db.add(post)
    db.commit()
    return redirect("/blog/" + str(post.id))
Ejemplo n.º 4
0
 def go(*args, **kw):
     try:
         ret = f(*args, **kw)
         db.commit()
         return ret
     except:
         db.rollback()
         db.close()
         raise
Ejemplo n.º 5
0
def post_blog():
    title = request.form.get('post-title')
    body = request.form.get('post-body')
    post = BlogPost()
    post.title = title
    post.text = body
    db.add(post)
    db.commit()
    return redirect("/blog/" + str(post.id))
Ejemplo n.º 6
0
def create_publisher():
    name = request.form.get("pname")
    if not name:
        return redirect("/asdf")
    if any(Publisher.query.filter(Publisher.name == name)):
        return redirect("/fsda")
    gname = Publisher(name)
    db.add(gname)
    db.commit()
    return redirect("/admin")
Ejemplo n.º 7
0
def create_version():
    friendly = request.form.get("friendly_version")
    gid = request.form.get("ganame")
    if not friendly or not gid:
        return redirect("/asdf")
    if any(GameVersion.query.filter(GameVersion.friendly_version == friendly)):
        return redirect("/fsda")
    version = GameVersion(friendly,gid)
    db.add(version)
    db.commit()
    return redirect("/admin")
Ejemplo n.º 8
0
def create_game():
    name = request.form.get("gname")
    sname = request.form.get("sname")
    pid = request.form.get("pname")
    if not name or not pid or not sname:
        return redirect("/asdf")
    if any(Game.query.filter(Game.name == name)):
        return redirect("/fsda")

    go = Game(name,pid,sname)
    db.add(go)
    db.commit()
    return redirect("/admin")
Ejemplo n.º 9
0
def edit_list(list_id, list_name):
    mod_list = ModList.query.filter(ModList.id == list_id).first()
    ga = Game.query.filter(Game.id == mod_list.game_id).first()
    if not mod_list:
        abort(404)
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod_list.user_id:
            editable = True
    if not editable:
        abort(401)
    if request.method == 'GET':
        return render_template("mods/edit_list.html",
            **{
                'mod_list': mod_list,
                'mod_ids': [m.mod.id for m in mod_list.mods],
                'ga': ga
            })
    else:
        description = request.form.get('description')
        background = request.form.get('background')
        bgOffsetY = request.form.get('bg-offset-y')
        mods = json.loads(request.form.get('mods'))
        mod_list.description = description
        if background and background != '':
            mod_list.background = background
        try:
            mod_list.bgOffsetY = int(bgOffsetY)
        except:
            pass
        # Remove mods
        removed_mods = [m for m in mod_list.mods if not m.mod_id in mods]
        for mod in removed_mods:
            mod_list.mods.remove(mod)

        # Add mods
        added_mods = [m for m in mods if not m in [mod.mod.id for mod in mod_list.mods]]
        for m in added_mods:
            mod = Mod.query.filter(Mod.id == m).first()
            mli = ModListItem()
            mli.mod_id = mod.id
            mli.mod_list = mod_list
            mod_list.mods.append(mli)
            db.add(mli)
            db.commit()
        for mod in mod_list.mods:
            mod.sort_index = mods.index(mod.mod.id)
        return redirect(url_for("lists.view_list", list_id=mod_list.id, list_name=mod_list.name))
Ejemplo n.º 10
0
def delete(list_id):
    mod_list = ModList.query.filter(ModList.id == list_id).first()
    if not mod_list:
        abort(404)
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod_list.user_id:
            editable = True
    if not editable:
        abort(401)
    db.delete(mod_list)
    db.commit()
    return redirect("/profile/" + current_user.username)
Ejemplo n.º 11
0
def follow(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;
    if any(m.id == mod.id for m in current_user.following):
        abort(418)
    event = FollowEvent.query\
            .filter(FollowEvent.mod_id == mod.id)\
            .order_by(desc(FollowEvent.created))\
            .first()
    # Events are aggregated hourly
    if not event or ((datetime.now() - event.created).seconds / 60 / 60) >= 1:
        event = FollowEvent()
        event.mod = mod
        event.delta = 1
        event.events = 1
        db.add(event)
        db.flush()
        db.commit()
        mod.follow_events.append(event)
    else:
        event.delta += 1
        event.events += 1
    mod.follower_count += 1
    current_user.following.append(mod)
    return { "success": True }
Ejemplo n.º 12
0
def delete_version(mod_id, version_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 any([u.accepted and u.user == current_user for u in mod.shared_authors]):
            editable = True
    if not editable:
        abort(401)
    version = [v for v in mod.versions if v.id == int(version_id)]
    if len(mod.versions) == 1:
        abort(400)
    if len(version) == 0:
        abort(404)
    if version[0].id == mod.default_version_id:
        abort(400)
    db.delete(version[0])
    mod.versions = [v for v in mod.versions if v.id != int(version_id)]
    db.commit()
    return redirect(url_for("mods.mod", id=mod.id, mod_name=mod.name,ga=game))
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
def mod(id, mod_name):
    mod = Mod.query.filter(Mod.id == id).first()
    ga = mod.game
    session['game'] = ga.id;
    session['gamename'] = ga.name;
    session['gameshort'] = ga.short;
    session['gameid'] = ga.id;
    if not mod or not ga:
        abort(404)
    editable = False
    if current_user:
        if current_user.admin:
            editable = True
        if current_user.id == mod.user_id:
            editable = True
    if not mod.published and not editable:
        abort(401)
    latest = mod.default_version()
    referral = request.referrer
    if referral:
        host = urllib.parse.urlparse(referral).hostname
        event = ReferralEvent.query\
                .filter(ReferralEvent.mod_id == mod.id)\
                .filter(ReferralEvent.host == host)\
                .first()
        if not event:
            event = ReferralEvent()
            event.mod = mod
            event.events = 1
            event.host = host
            db.add(event)
            db.flush()
            db.commit()
            mod.referrals.append(event)
        else:
            event.events += 1
    download_stats = None
    follower_stats = None
    referrals = None
    json_versions = None
    thirty_days_ago = datetime.now() - timedelta(days=30)
    referrals = list()
    for r in ReferralEvent.query\
        .filter(ReferralEvent.mod_id == mod.id)\
        .order_by(desc(ReferralEvent.events)):
        referrals.append( { 'host': r.host, 'count': r.events } )
    download_stats = list()
    for d in DownloadEvent.query\
        .filter(DownloadEvent.mod_id == mod.id)\
        .filter(DownloadEvent.created > thirty_days_ago)\
        .order_by(DownloadEvent.created):
        download_stats.append(dumb_object(d))
    follower_stats = list()
    for f in FollowEvent.query\
        .filter(FollowEvent.mod_id == mod.id)\
        .filter(FollowEvent.created > thirty_days_ago)\
        .order_by(FollowEvent.created):
        follower_stats.append(dumb_object(f))
    json_versions = list()
    for v in mod.versions:
        json_versions.append({ 'name': v.friendly_version, 'id': v.id })
    if request.args.get('noedit') != None:
        editable = False
    forumThread = False
    if mod.external_link != None:
        try:
            u = urlparse(mod.external_link)
            if u.netloc == 'forum.kerbalspaceprogram.com':
                forumThread = True
        except e:
            print(e)
            pass
    total_authors = 1
    pending_invite = False
    owner = editable
    for a in mod.shared_authors:
        if a.accepted:
            total_authors += 1
        if current_user:
            if current_user.id == a.user_id and not a.accepted:
                pending_invite = True
            if current_user.id == a.user_id and a.accepted:
                editable = True
    games = Game.query.filter(Game.active == True).order_by(desc(Game.id)).all()

    game_versions = GameVersion.query.filter(GameVersion.game_id == mod.game_id).order_by(desc(GameVersion.id)).all()

    outdated = False
    if latest:
        outdated = latest.gameversion.id != game_versions[0].id and latest.gameversion.friendly_version != '1.0.5'
    return render_template("detail.html",ptype='mod',stype='view',
        **{
            'mod': mod,
            'latest': latest,
            'safe_name': secure_filename(mod.name)[:64],
            'featured': any(Featured.query.filter(Featured.mod_id == mod.id).all()),
            'editable': editable,
            'owner': owner,
            'pending_invite': pending_invite,
            'download_stats': download_stats,
            'follower_stats': follower_stats,
            'referrals': referrals,
            'json_versions': json_versions,
            'thirty_days_ago': thirty_days_ago,
            'share_link': urllib.parse.quote_plus(_cfg("protocol") + "://" + _cfg("domain") + "/mod/" + str(mod.id)),
            'game_versions': game_versions,
            'games':  games,
            'outdated': outdated,
            'forum_thread': forumThread,
            'new': request.args.get('new') != None,
            'stupid_user': request.args.get('stupid_user') != None,
            'total_authors': total_authors,
			"site_name": _cfg('site-name'), 
			"support_mail": _cfg('support-mail'),
            'ga': ga
        })
Ejemplo n.º 15
0
def download(mod_id, mod_name, version):
    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;
    if not mod.published and (not current_user or current_user.id != mod.user_id):
        abort(401)
    version = ModVersion.query.filter(ModVersion.mod_id == mod_id, \
            ModVersion.friendly_version == version).first()
    if not version:
        abort(404)
    download = DownloadEvent.query\
            .filter(DownloadEvent.mod_id == mod.id and DownloadEvent.version_id == version.id)\
            .order_by(desc(DownloadEvent.created))\
            .first()
    if not os.path.isfile(os.path.join(_cfg('storage'), version.download_path)):
        abort(404)
    
    if not 'Range' in request.headers:
        # Events are aggregated hourly
        if not download or ((datetime.now() - download.created).seconds / 60 / 60) >= 1:
            download = DownloadEvent()
            download.mod = mod
            download.version = version
            download.downloads = 1
            db.add(download)
            db.flush()
            db.commit()
            mod.downloads.append(download)
        else:
            download.downloads += 1
        mod.download_count += 1
    
    if _cfg("cdn-domain"):
        return redirect("http://" + _cfg("cdn-domain") + '/' + version.download_path, code=302)
    
    response = None
    if _cfg("use-x-accel") == 'nginx':
        response = make_response("")
        response.headers['Content-Type'] = 'application/zip'
        response.headers['Content-Disposition'] = 'attachment; filename=' + os.path.basename(version.download_path)
        response.headers['X-Accel-Redirect'] = '/internal/' + version.download_path
    if _cfg("use-x-accel") == 'apache':
        response = make_response("")
        response.headers['Content-Type'] = 'application/zip'
        response.headers['Content-Disposition'] = 'attachment; filename=' + os.path.basename(version.download_path)
        response.headers['X-Sendfile'] = os.path.join(_cfg('storage'), version.download_path)
    if response is None:
        response = make_response(send_file(os.path.join(_cfg('storage'), version.download_path), as_attachment = True))
    return response