示例#1
0
def CreateCampaign():
    if 'file' not in request.files:
        return Response('no file found', status=400)
    file = request.files['file']
    if not allowed_file(file.filename):
        return Response('invalid file type', status=400)
    try:
        newCampaign = Campaign(
            request.form['name'] or None,
            f'/mediafiles/campaigns/{secure_filename(file.filename)}')
        if request.form['is_default'] == 'true':
            old_default = Campaign.query.filter_by(is_default=True).all()
            for camp in old_default:
                camp.is_default = False
            newCampaign.is_default = True
        db.session.add(newCampaign)
        db.session.commit()
        os.makedirs(
            f'/usr/src/app/mediafiles/campaigns/{newCampaign.name.replace(" ", "_")}',
            exist_ok=True)
        file.save(f'/usr/src/app{newCampaign.image}')
        data = jsonify(newCampaign.to_dict())
        data.status_code = 201
        return data
    except IntegrityError as error:
        return Response(error.args[0], status=400)
示例#2
0
    def setUp(self):
        app = create_app(test_config)

        with app.app_context():
            db.drop_all()
            db.create_all()

        self.app = app
        self.test_client = app.test_client()
        self.app_context = app.app_context()
        self.app_context.push()

        self.socketio = SocketIO(app)

        self.socketio.on_event('connect', on_connect)
        self.socketio.on_event('campaign-update', handle_campaign_update)

        guild = Guild('Flames of Exile')
        db.session.add(guild)
        db.session.commit()
        foe_guild = db.session.query(Guild).filter_by(
            name='Flames of Exile').first()

        admin = User('DiscordBot', sha256_crypt.encrypt('admin'), foe_guild.id,
                     User.Role.ADMIN)
        admin.discord_confirmed = True
        db.session.add(admin)
        db.session.commit()

        campaign = Campaign('campaign_name', '/mediafiles/campaign.png', True)
        db.session.add(campaign)
        db.session.commit()

        self.campaign = campaign.to_dict()

        self.maxDiff = None

        self.assertEqual(app.debug, False)