<commit_before># -*- coding: utf-8 -*-
"""Public section, including homepage and signup."""
from flask import (Blueprint, request, render_template, flash, url_for,
                    redirect, session, current_app, jsonify)
from flask_login import login_required, current_user

from dribdat.user.models import User, Event, Project
from dribdat.public.forms import ProjectForm
from dribdat.database import db
from dribdat.aggregation import GetProjectData, ProjectActivity, IsProjectStarred, GetProjectTeam
from dribdat.extensions import cache

blueprint = Blueprint('public', __name__, static_folder="../static")

def current_event():
    return Event.query.filter_by(is_current=True).first()

@blueprint.route("/")
def home():
    event = current_event()
    if event is not None:
        events = Event.query.filter(Event.id != event.id)
    else:
        events = Event.query.all()
    return render_template("public/home.html", events=events, current_event=event)

@blueprint.route("/about/")
def about():
    return render_template("public/about.html", current_event=current_event())

@blueprint.route("/dashboard/")
def dashboard():
    return render_template("public/dashboard.html", current_event=current_event())

@blueprint.route("/event/<int:event_id>")
def event(event_id):
    event = Event.query.filter_by(id=event_id).first_or_404()
    projects = Project.query.filter_by(event_id=event_id, is_hidden=False)
    if request.args.get('embed'):
        return render_template("public/embed.html", current_event=event, projects=projects)
    summaries = [ p.data for p in projects ]
    summaries.sort(key=lambda x: x['score'], reverse=True)
    return render_template("public/event.html",  current_event=event, projects=summaries)

@blueprint.route('/project/<int:project_id>')
def project(project_id):
    return project_action(project_id, None)

@blueprint.route('/project/<int:project_id>/edit', methods=['GET', 'POST'])
@login_required
def project_edit(project_id):
    project = Project.query.filter_by(id=project_id).first_or_404()
    event = project.event
    if project.user_id != current_user.id:
        flash('You do not have access to edit this project.', 'warning')
        return project_action(project_id, None)
    form = ProjectForm(obj=project, next=request.args.get('next'))
    form.category_id.choices = [(c.id, c.name) for c in project.categories_for_event(event.id)]
    form.category_id.choices.insert(0, (-1, ''))
    if form.validate_on_submit():
        form.populate_obj(project)
        project.update()
        db.session.add(project)
        db.session.commit()
        cache.clear()
        flash('Project updated.', 'success')
        return project_action(project_id, 'update')
    return render_template('public/projectedit.html', current_event=event, project=project, form=form)

def project_action(project_id, of_type):
    project = Project.query.filter_by(id=project_id).first_or_404()
    event = project.event
    if of_type is not None:
        ProjectActivity(project, of_type, current_user)
    project_starred = current_user and current_user.is_authenticated and IsProjectStarred(project, current_user)
    project_stars = GetProjectTeam(project)
    return render_template('public/project.html', current_event=event, project=project,
        project_starred=project_starred, project_stars=project_stars)

@blueprint.route('/project/<int:project_id>/star', methods=['GET', 'POST'])
@login_required
def project_star(project_id):
    flash('Thanks for your support!', 'success')
    return project_action(project_id, 'star')

@blueprint.route('/project/<int:project_id>/unstar', methods=['GET', 'POST'])
@login_required
def project_unstar(project_id):
    flash('Project un-starred', 'success')
    return project_action(project_id, 'unstar')

@blueprint.route('/project/new', methods=['GET', 'POST'])
def project_new():
    form = None
    event = current_event()
    if current_user and current_user.is_authenticated:
        project = Project()
        project.user_id = current_user.id
        form = ProjectForm(obj=project, next=request.args.get('next'))
        form.category_id.choices = [(c.id, c.name) for c in project.categories_for_event(event.id)]
        form.category_id.choices.insert(0, (-1, ''))
        if form.validate_on_submit():
            form.populate_obj(project)
            project.event = event
            project.update()
            db.session.add(project)
            db.session.commit()
            flash('Project added.', 'success')
            project_action(project.id, 'create')
            cache.clear()
            return project_action(project.id, 'star')
        del form.logo_icon
        del form.logo_color
    return render_template('public/projectnew.html', current_event=event, form=form)

# API routine used to sync project data
@blueprint.route('/project/autofill', methods=['GET', 'POST'])
@login_required
def project_autofill():
    url = request.args.get('url')
    data = GetProjectData(url)
    return jsonify(data)

@blueprint.route('/project/<int:project_id>/autoupdate')
@login_required<commit_msg>Clear id in form<commit_after># -*- coding: utf-8 -*-
"""Public section, including homepage and signup."""
from flask import (Blueprint, request, render_template, flash, url_for,
                    redirect, session, current_app, jsonify)
from flask_login import login_required, current_user

from dribdat.user.models import User, Event, Project
from dribdat.public.forms import ProjectForm
from dribdat.database import db
from dribdat.aggregation import GetProjectData, ProjectActivity, IsProjectStarred, GetProjectTeam
from dribdat.extensions import cache

blueprint = Blueprint('public', __name__, static_folder="../static")

def current_event():
    return Event.query.filter_by(is_current=True).first()

@blueprint.route("/")
def home():
    event = current_event()
    if event is not None:
        events = Event.query.filter(Event.id != event.id)
    else:
        events = Event.query.all()
    return render_template("public/home.html", events=events, current_event=event)

@blueprint.route("/about/")
def about():
    return render_template("public/about.html", current_event=current_event())

@blueprint.route("/dashboard/")
def dashboard():
    return render_template("public/dashboard.html", current_event=current_event())

@blueprint.route("/event/<int:event_id>")
def event(event_id):
    event = Event.query.filter_by(id=event_id).first_or_404()
    projects = Project.query.filter_by(event_id=event_id, is_hidden=False)
    if request.args.get('embed'):
        return render_template("public/embed.html", current_event=event, projects=projects)
    summaries = [ p.data for p in projects ]
    summaries.sort(key=lambda x: x['score'], reverse=True)
    return render_template("public/event.html",  current_event=event, projects=summaries)

@blueprint.route('/project/<int:project_id>')
def project(project_id):
    return project_action(project_id, None)

@blueprint.route('/project/<int:project_id>/edit', methods=['GET', 'POST'])
@login_required
def project_edit(project_id):
    project = Project.query.filter_by(id=project_id).first_or_404()
    event = project.event
    if project.user_id != current_user.id:
        flash('You do not have access to edit this project.', 'warning')
        return project_action(project_id, None)
    form = ProjectForm(obj=project, next=request.args.get('next'))
    form.category_id.choices = [(c.id, c.name) for c in project.categories_for_event(event.id)]
    form.category_id.choices.insert(0, (-1, ''))
    if form.validate_on_submit():
        del form.id
        form.populate_obj(project)
        project.update()
        db.session.add(project)
        db.session.commit()
        cache.clear()
        flash('Project updated.', 'success')
        return project_action(project_id, 'update')
    return render_template('public/projectedit.html', current_event=event, project=project, form=form)

def project_action(project_id, of_type):
    project = Project.query.filter_by(id=project_id).first_or_404()
    event = project.event
    if of_type is not None:
        ProjectActivity(project, of_type, current_user)
    project_starred = current_user and current_user.is_authenticated and IsProjectStarred(project, current_user)
    project_stars = GetProjectTeam(project)
    return render_template('public/project.html', current_event=event, project=project,
        project_starred=project_starred, project_stars=project_stars)

@blueprint.route('/project/<int:project_id>/star', methods=['GET', 'POST'])
@login_required
def project_star(project_id):
    flash('Thanks for your support!', 'success')
    return project_action(project_id, 'star')

@blueprint.route('/project/<int:project_id>/unstar', methods=['GET', 'POST'])
@login_required
def project_unstar(project_id):
    flash('Project un-starred', 'success')
    return project_action(project_id, 'unstar')

@blueprint.route('/project/new', methods=['GET', 'POST'])
def project_new():
    form = None
    event = current_event()
    if current_user and current_user.is_authenticated:
        project = Project()
        project.user_id = current_user.id
        form = ProjectForm(obj=project, next=request.args.get('next'))
        form.category_id.choices = [(c.id, c.name) for c in project.categories_for_event(event.id)]
        form.category_id.choices.insert(0, (-1, ''))
        if form.validate_on_submit():
            del form.id
            form.populate_obj(project)
            project.event = event
            project.update()
            db.session.add(project)
            db.session.commit()
            flash('Project added.', 'success')
            project_action(project.id, 'create')
            cache.clear()
            return project_action(project.id, 'star')
        del form.logo_icon
        del form.logo_color
    return render_template('public/projectnew.html', current_event=event, form=form)

# API routine used to sync project data
@blueprint.route('/project/autofill', methods=['GET', 'POST'])
@login_required
def project_autofill():
    url = request.args.get('url')
    data = GetProjectData(url)
    return jsonify(data)

@blueprint.route('/project/<int:project_id>/autoupdate')
@login_required
{'fraction_non_alphanumeric': 0.12944038929440388, 'fraction_numerical': 0.0027250608272506084, 'mean_word_length': 4.210953346855984, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '885639', 'n_tokens_mistral': 3120, 'n_tokens_neox': 3095, 'n_words': 630}