<commit_before># -*- coding: utf-8 -*-
'''
Created on Nov 24, 2014

@author: moloch

    Copyright 2014 Root the Box

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.


CRUD for game objects:
    - Corporations
    - Boxes (and IpAddress)
    - Flags (and FlagAttachment)
    - Game Levels
    - Hints

'''

import logging

from handlers.BaseHandlers import BaseHandler
from models.Box import Box
from models.Corporation import Corporation
from models.GameLevel import GameLevel
from models.Hint import Hint
from models.Team import Team
from models.IpAddress import IpAddress
from models.User import ADMIN_PERMISSION
from models.Flag import Flag, FLAG_FILE, FLAG_REGEX, FLAG_STATIC
from libs.ValidationError import ValidationError
from libs.SecurityDecorators import *


class AdminCreateHandler(BaseHandler):

    ''' Handler used to create game objects '''

    @restrict_ip_address
    @authenticated
    @authorized(ADMIN_PERMISSION)
    def get(self, *args, **kwargs):
        ''' Renders Corp/Box/Flag create pages '''
        game_objects = {
            'corporation': 'admin/create/corporation.html',
            'box': 'admin/create/box.html',
            'flag': 'admin/create/flag.html',
            'flag/regex': 'admin/create/flag-regex.html',
            'flag/file': 'admin/create/flag-file.html',
            'flag/static': 'admin/create/flag-static.html',
            'game_level': 'admin/create/game_level.html',
            'hint': 'admin/create/hint.html',
            'team': 'admin/create/team.html',
        }
        if len(args) and args[0] in game_objects:
            self.render(game_objects[args[0]], errors=None)
        else:
            self.render("public/404.html")

    @restrict_ip_address
    @authenticated
    @authorized(ADMIN_PERMISSION)
    def post(self, *args, **kwargs):
        ''' Calls a function based on URL '''
        game_objects = {
            'corporation': self.create_corporation,
            'box': self.create_box,
            'flag/file': self.create_flag_file,
            'flag/regex': self.create_flag_regex,
            'flag/static': self.create_flag_static,
            'game_level': self.create_game_level,
            'hint': self.create_hint,
            'team': self.create_team,
        }
        if len(args) and args[0] in game_objects:
            game_objects[args[0]]()
        else:
            self.render("public/404.html")

    def create_team(self):
        ''' Admins can create teams manually '''
        try:
            name = self.get_argument('team_name', '')
            motto = self.get_argument('motto', '')
            if Team.by_name(name) is not None:
                raise ValidationError("Team already exists")
            team = Team(name=name, motto=motto)
            self.dbsession.add(team)
            self.dbsession.commit()
            self.redirect('/admin/users')
        except ValidationError as error:
            self.render("admin/create/team.html", errors=[str(error), ])

    def create_corporation(self):
        ''' Add a new corporation to the database '''
        try:
            corp_name = self.get_argument('corporation_name', '')
            if Corporation.by_name(corp_name) is not None:<commit_msg>Bug fix #53<commit_after># -*- coding: utf-8 -*-
'''
Created on Nov 24, 2014

@author: moloch

    Copyright 2014 Root the Box

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.


CRUD for game objects:
    - Corporations
    - Boxes (and IpAddress)
    - Flags (and FlagAttachment)
    - Game Levels
    - Hints

'''

import logging

from handlers.BaseHandlers import BaseHandler
from models.Box import Box
from models.Corporation import Corporation
from models.GameLevel import GameLevel
from models.Hint import Hint
from models.Team import Team
from models.IpAddress import IpAddress
from models.User import ADMIN_PERMISSION
from models.Flag import Flag, FLAG_FILE, FLAG_REGEX, FLAG_STATIC
from libs.ValidationError import ValidationError
from libs.SecurityDecorators import *


class AdminCreateHandler(BaseHandler):

    ''' Handler used to create game objects '''

    @restrict_ip_address
    @authenticated
    @authorized(ADMIN_PERMISSION)
    def get(self, *args, **kwargs):
        ''' Renders Corp/Box/Flag create pages '''
        game_objects = {
            'corporation': 'admin/create/corporation.html',
            'box': 'admin/create/box.html',
            'flag': 'admin/create/flag.html',
            'flag/regex': 'admin/create/flag-regex.html',
            'flag/file': 'admin/create/flag-file.html',
            'flag/static': 'admin/create/flag-static.html',
            'game_level': 'admin/create/game_level.html',
            'hint': 'admin/create/hint.html',
            'team': 'admin/create/team.html',
        }
        if len(args) and args[0] in game_objects:
            self.render(game_objects[args[0]], errors=None)
        else:
            self.render("public/404.html")

    @restrict_ip_address
    @authenticated
    @authorized(ADMIN_PERMISSION)
    def post(self, *args, **kwargs):
        ''' Calls a function based on URL '''
        game_objects = {
            'corporation': self.create_corporation,
            'box': self.create_box,
            'flag/file': self.create_flag_file,
            'flag/regex': self.create_flag_regex,
            'flag/static': self.create_flag_static,
            'game_level': self.create_game_level,
            'hint': self.create_hint,
            'team': self.create_team,
        }
        if len(args) and args[0] in game_objects:
            game_objects[args[0]]()
        else:
            self.render("public/404.html")

    def create_team(self):
        ''' Admins can create teams manually '''
        try:
            name = self.get_argument('team_name', '')
            motto = self.get_argument('motto', '')
            if Team.by_name(name) is not None:
                raise ValidationError("Team already exists")
            team = Team()
            team.name = self.get_argument('team_name', '')
            team.motto = self.get_argument('motto', '')
            level_0 = GameLevel.all()[0]
            self.dbsession.add(team)
            self.dbsession.commit()
            self.redirect('/admin/users')
        except ValidationError as error:
            self.render("admin/create/team.html", errors=[str(error), ])

    def create_corporation(self):
        ''' Add a new corporation to the database '''
        try:
            corp_name = self.get_argument('corporation_name', '')
            if Corporation.by_name(corp_name) is not None:
{'fraction_non_alphanumeric': 0.10631578947368421, 'fraction_numerical': 0.007105263157894737, 'mean_word_length': 2.4963201471941123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2760826', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2115, 'n_words': 590}