<commit_before>from flask.blueprints import Blueprint
import logging
from waitlist.data.perm import perm_management, perm_dev
from waitlist.utility import fleet as fleetUtils
from werkzeug.utils import redirect
from flask_login import login_required, current_user
from waitlist.base import db
from waitlist.storage.database import CrestFleet, WaitlistGroup
from waitlist.utility.config import crest_client_id, crest_client_secret
from pycrest.eve import AuthedConnectionB
from flask.helpers import url_for
from flask.templating import render_template
from flask.globals import request
import re
import flask
from waitlist.utility.fleet import get_wings
from waitlist.utility.crest import create_token_cb

bp = Blueprint('fleet', __name__)
logger = logging.getLogger(__name__)

'''
Setps:
    url == process the url and setup quads/motd if wanted
    selection == select which squads to invite too
'''
@bp.route("/setup/<string:step>", methods=['POST'])
@login_required
@perm_management.require(http_exception=401)
def setup_steps(step):
    if step == 'url':
        return setup_step_url()
    elif step == "select":
        return setup_step_select()
    pass

def setup_step_url():
    skip_setup = request.form.get('skip-setup')
    fleet_link = request.form.get('fleet-link')
    fleet_type = request.form.get('fleet-type')
    if skip_setup == "no-setup":
        skip_setup = True
    else:
        skip_setup = False
    
    fleet_id_search = re.search('https://crest-tq.eveonline.com/fleets/(\d+)/', fleet_link, re.IGNORECASE)
    fleet_id = None
    if fleet_id_search:
        fleet_id = int(fleet_id_search.group(1))
    
    if fleet_id is None:
        flask.abort(400)
    
    if not skip_setup:
        fleetUtils.setup(fleet_id, fleet_type)
    
    return get_select_form(fleet_id)

def get_select_form(fleet_id):
    wings = get_wings(fleet_id)
    active_groups = db.session.query(WaitlistGroup).filter(WaitlistGroup.enabled == True)
    auto_assign = {}
    

    for wing in wings:
        for squad in wing.squadsList:
            lname = squad.name.lower()
            if "logi" in lname:
                auto_assign['logi'] = squad
            elif "sniper" in lname:
                auto_assign['sniper'] = squad
            elif "dps" in lname and not "more" in lname:
                auto_assign['dps'] = squad
            elif ("dps" in lname and "more" in lname) or "other" in lname:
                auto_assign['overflow'] = squad
    return render_template("/fleet/setup/select.html", wings=wings, fleet_id=fleet_id, groups=active_groups, assign=auto_assign)

def setup_step_select():
    logi_s = request.form.get('wl-logi')
    sniper_s = request.form.get('wl-sniper')
    dps_s = request.form.get('wl-dps')
    overflow_s = request.form.get('wl-overflow')
    fleet_id = int(request.form.get('fleet-id'))
    groupID = int(request.form.get('fleet-group'))
    # create [wingID, squadID] lists
    logi = [int(x) for x in logi_s.split(';')]
    sniper = [int(x) for x in sniper_s.split(';')]
    dps = [int(x) for x in dps_s.split(';')]
    overflow = [int(x) for x in overflow_s.split(';')]
    
    fleet = db.session.query(CrestFleet).get(fleet_id)
    if fleet is None:
        fleet = CrestFleet()
        fleet.fleetID = fleet_id
        fleet.logiWingID = logi[0]
        fleet.logiSquadID = logi[1]
        fleet.sniperWingID = sniper[0]
        fleet.sniperSquadID = sniper[1]
        fleet.dpsWingID = dps[0]
        fleet.dpsSquadID = dps[1]
        fleet.otherWingID = overflow[0]
        fleet.otherSquadID = overflow[1]
        fleet.groupID = groupID
        fleet.compID = current_user.id
        oldfleet = db.session.query(CrestFleet).filter((CrestFleet.compID == current_user.id)).first()
        if oldfleet != None:
            oldfleet.compID = None
        db.session.add(fleet)
    else:
        fleet.logiWingID = logi[0]
        fleet.logiSquadID = logi[1]<commit_msg>fixed 2 internal server errors on fleetsetup form submission<commit_after>from flask.blueprints import Blueprint
import logging
from waitlist.data.perm import perm_management, perm_dev
from waitlist.utility import fleet as fleetUtils
from werkzeug.utils import redirect
from flask_login import login_required, current_user
from waitlist.base import db
from waitlist.storage.database import CrestFleet, WaitlistGroup
from waitlist.utility.config import crest_client_id, crest_client_secret
from pycrest.eve import AuthedConnectionB
from flask.helpers import url_for
from flask.templating import render_template
from flask.globals import request
import re
import flask
from waitlist.utility.fleet import get_wings
from waitlist.utility.crest import create_token_cb

bp = Blueprint('fleet', __name__)
logger = logging.getLogger(__name__)

'''
Setps:
    url == process the url and setup quads/motd if wanted
    selection == select which squads to invite too
'''
@bp.route("/setup/<string:step>", methods=['POST'])
@login_required
@perm_management.require(http_exception=401)
def setup_steps(step):
    if step == 'url':
        return setup_step_url()
    elif step == "select":
        return setup_step_select()
    pass

def setup_step_url():
    skip_setup = request.form.get('skip-setup')
    fleet_link = request.form.get('fleet-link')
    fleet_type = request.form.get('fleet-type')
    if skip_setup == "no-setup":
        skip_setup = True
    else:
        skip_setup = False
    
    fleet_id_search = re.search('https://crest-tq.eveonline.com/fleets/(\d+)/', fleet_link, re.IGNORECASE)
    fleet_id = None
    if fleet_id_search:
        fleet_id = int(fleet_id_search.group(1))
    
    if fleet_id is None:
        flask.abort(400)
    
    if not skip_setup:
        fleetUtils.setup(fleet_id, fleet_type)
    
    return get_select_form(fleet_id)

def get_select_form(fleet_id):
    wings = get_wings(fleet_id)
    active_groups = db.session.query(WaitlistGroup).filter(WaitlistGroup.enabled == True)
    auto_assign = {}
    

    for wing in wings:
        for squad in wing.squadsList:
            lname = squad.name.lower()
            if "logi" in lname:
                auto_assign['logi'] = squad
            elif "sniper" in lname:
                auto_assign['sniper'] = squad
            elif "dps" in lname and not "more" in lname:
                auto_assign['dps'] = squad
            elif ("dps" in lname and "more" in lname) or "other" in lname:
                auto_assign['overflow'] = squad
    return render_template("/fleet/setup/select.html", wings=wings, fleet_id=fleet_id, groups=active_groups, assign=auto_assign)

def setup_step_select():
    logi_s = request.form.get('wl-logi')
    sniper_s = request.form.get('wl-sniper')
    dps_s = request.form.get('wl-dps')
    overflow_s = request.form.get('wl-overflow')
    try:
        fleet_id = int(request.form.get('fleet-id'))
    except ValueError:
        flask.abort(400, "No valid fleet-id given!")
    try:
        groupID = int(request.form.get('fleet-group'))
    except:
        flask.abort(400, "No valid fleet-group-id given!")
    # create [wingID, squadID] lists
    logi = [int(x) for x in logi_s.split(';')]
    sniper = [int(x) for x in sniper_s.split(';')]
    dps = [int(x) for x in dps_s.split(';')]
    overflow = [int(x) for x in overflow_s.split(';')]
    
    fleet = db.session.query(CrestFleet).get(fleet_id)
    if fleet is None:
        fleet = CrestFleet()
        fleet.fleetID = fleet_id
        fleet.logiWingID = logi[0]
        fleet.logiSquadID = logi[1]
        fleet.sniperWingID = sniper[0]
        fleet.sniperSquadID = sniper[1]
        fleet.dpsWingID = dps[0]
        fleet.dpsSquadID = dps[1]
        fleet.otherWingID = overflow[0]
        fleet.otherSquadID = overflow[1]
        fleet.groupID = groupID
        fleet.compID = current_user.id
        oldfleet = db.session.query(CrestFleet).filter((CrestFleet.compID == current_user.id)).first()
        if oldfleet != None:
            oldfleet.compID = None
        db.session.add(fleet)
    else:
        fleet.logiWingID = logi[0]
        fleet.logiSquadID = logi[1]
{'fraction_non_alphanumeric': 0.11076732673267327, 'fraction_numerical': 0.005074257425742574, 'mean_word_length': 3.034448327508737, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '2107812', 'n_tokens_mistral': 2793, 'n_tokens_neox': 2766, 'n_words': 606}