Esempio n. 1
0
def delete_scope(character_id, scope):
    """ Remove a scope for a given character_id from the database """
    if is_xhr(request):
        allowed_character_id = [
            alt.character_id for alt in current_user.alts_characters.all()
        ]
        if (character_id == current_user.character_id
                or character_id in allowed_character_id):
            try:
                TokenScope.query.filter(TokenScope.user_id == character_id,
                                        TokenScope.scope == scope).delete()
                db.session.commit()
                return json_response('success', '', 200)

            except:
                logger.exception('Cannot delete scope %s for user_id %s' % (
                    scope,
                    character_id,
                ))
                db.session.rollback()
                return json_response('danger',
                                     'Error while trying to delete scope', 500)
        else:
            return json_response('danger',
                                 'This character does not belong to you', 500)
    else:
        return 'Cannot call this page directly', 403
def is_not_ajax():
    """
    Return True if request is not ajax
    This function is used in @cache annotation
    to not cache direct call (http 403)
    """
    return not is_xhr(request)
Esempio n. 3
0
def item_search(name):
    """
    Return JSON result for a specific search
    name is the request name.
    """
    if is_xhr(request):
        cache_key = 'item:search:%s' % (name.lower().replace(" ", ""), )

        data = CACHE.get(cache_key)
        if data is None:
            items = Item.query.filter(
                Item.name.ilike('%' + name.lower() + '%'), ).order_by(
                    Item.name.asc()).all()

            data = []
            for item in items:
                data.append({
                    'id': item.id,
                    'name': item.name,
                    'icon': item.icon_32(),
                })

            # cache for 7 day as it does not change that often
            CACHE.set(cache_key, json.dumps(data), 24 * 3600 * 7)

        else:
            data = json.loads(data)

        return jsonify(result=data)
    else:
        return 'Cannot call this page directly', 403
Esempio n. 4
0
def solarsystems():
    """
    Return JSON result with system list (ID,Name)
    """
    if is_xhr(request):
        systems = SolarSystem.query.all()
        data = []
        for system in systems:
            data.append(system.name)
        return jsonify(result=data)
    else:
        return 'Cannot call this page directly', 403
Esempio n. 5
0
def delete_characters_skills():
    """ remove all character skills for current user """
    if is_xhr(request):
        try:
            purge_characters_skill(current_user)
            return json_response('success', 'Characters skills deleted.', 200)

        except:
            logger.exception('Cannot delete characters skills [u:%d]' %
                             current_user.character_id)
            db.session.rollback()
            return json_response('danger',
                                 'Error while trying to delete skills', 500)
    else:
        return 'Cannot call this page directly', 403
Esempio n. 6
0
def delete_user_account():
    """ remove all character blueprint for current user """
    if is_xhr(request):
        char_id = current_user.character_id
        try:
            delete_account(current_user)
            flash("Your account have been deleted.", 'info')
            return json_response('success', '', 200)
        except:
            logger.exception('Cannot delete user account [u:%d]' % char_id)
            db.session.rollback()
            return json_response(
                'danger',
                'Error while trying to delete corporation blueprints', 500)
    else:
        return 'Cannot call this page directly', 403
Esempio n. 7
0
def blueprint_search(name):
    """
    Return JSON result for a specific search
    name is the request name.
    """
    if is_xhr(request):
        name_lower = name.lower()

        # prevent only % string, to avoid query the full database at once...
        if name_lower == '%' * len(name_lower):
            return jsonify(result=[])

        blueprints = Item.query.filter(
            Item.name.ilike('%' + name_lower + '%'),
            Item.max_production_limit.isnot(None)).outerjoin(
                ActivityProduct,
                ((Item.id == ActivityProduct.item_id) &
                 ((ActivityProduct.activity == ActivityEnum.INVENTION.aid) |
                  (ActivityProduct.activity == ActivityEnum.REACTIONS.aid))
                 )).options(db.contains_eager(
                     Item.activity_products__eager)).order_by(
                         Item.name.asc()).all()

        data = []
        for bp in blueprints:
            invention = False
            reaction = False

            # we can't have invention AND reaction
            # at the same time as product.
            if bp.activity_products__eager:
                invention = (bp.activity_products__eager[0].activity ==
                             ActivityEnum.INVENTION.aid)
                reaction = (bp.activity_products__eager[0].activity ==
                            ActivityEnum.REACTIONS.aid)

            data.append({
                'id': bp.id,
                'name': bp.name,
                'invention': invention,
                'reaction': reaction,
                'relic': bp.is_ancient_relic(),
            })

        return jsonify(result=data)
    else:
        return 'Cannot call this page directly', 403
Esempio n. 8
0
def delete_corporation_blueprint():
    """ remove all character blueprint for current user """
    if is_xhr(request):
        try:
            purge_corporation_blueprints(current_user)
            return json_response('success', 'Corporations blueprints deleted.',
                                 200)

        except:
            logger.exception('Cannot delete corporation blueprints [u:%d]' %
                             current_user.character_id)
            db.session.rollback()
            return json_response(
                'danger',
                'Error while trying to delete corporation blueprints', 500)
    else:
        return 'Cannot call this page directly', 403
Esempio n. 9
0
def update_user_industry_preference():
    """ Update the user preferences for industry """
    if is_xhr(request):
        preferences = request.get_json()

        if 'production' in preferences:
            return update_production_preference(preferences['production'])

        if 'research' in preferences:
            return update_research_preference(preferences['research'])

        if 'invention' in preferences:
            return update_invention_preference(preferences['invention'])

        if 'reaction' in preferences:
            return update_reaction_preference(preferences['reaction'])
    else:
        return 'Cannot call this page directly', 403
Esempio n. 10
0
def get_price(item_list):
    """
    Get prices for all items we need !
    """
    if is_xhr(request):

        item_list = item_list.split(',')

        # get all items price
        item_prices = ItemPrice.query.filter(
            ItemPrice.item_id.in_(item_list)
        )

        item_price_list = {}
        for price in item_prices:
            if price.region_id not in item_price_list:
                item_price_list[price.region_id] = {}

            item_price_list[price.region_id][price.item_id] = {
                'sell': price.sell_price,
                'buy': price.buy_price,
                'updated_at': humanize.naturaltime(price.get_delta_update()),
            }

        # get all items adjusted price
        item_adjusted = ItemAdjustedPrice.query.filter(
            ItemAdjustedPrice.item_id.in_(item_list)
        )

        item_adjusted_list = {}
        for item in item_adjusted:
            item_adjusted_list[item.item_id] = item.price

        return jsonify(
            {'prices': item_price_list, 'adjusted': item_adjusted_list}
        )
    else:
        return 'Cannot call this page directly', 403
Esempio n. 11
0
    def check_and_update_user():
        """ check for invalid token and print message and update last seen """
        if flask_login.current_user.is_authenticated and not is_xhr(request):
            char_id = flask_login.current_user.character_id
            current_user = flask_login.current_user
            count_error = TokenScope.query.filter_by(
                valid=False).join(User).filter((
                    (User.main_character_id.is_(None))
                    & (User.character_id == char_id)
                ) | (User.main_character_id == char_id)).filter(
                    ((TokenScope.last_update.is_(None)) &
                     (TokenScope.updated_at >= current_user.current_login_at))
                    | (TokenScope.last_update >= current_user.current_login_at)
                ).count()

            if count_error > 0:
                flash(
                    'You have at least one scope that have been invalidate.'
                    ' Please take a moment to check and update it, '
                    ' or remove it.', 'danger')

            flask_login.current_user.current_login_at = utcnow()
            db.session.commit()
Esempio n. 12
0
def blueprint_bom(blueprint_id):
    """
    Return JSON with the list of all bill of material for
    each material in the blueprint given in argument

    As reaction and manufacturing cannot happen on the same blueprint at once
    we can safely ask for both at the same time (to be used in prod/reactions)
    """
    if is_xhr(request):
        blueprints = ActivityMaterial.query.filter(
            ActivityMaterial.item_id == blueprint_id,
            ((ActivityMaterial.activity == ActivityEnum.MANUFACTURING.aid) |
             (ActivityMaterial.activity == ActivityEnum.REACTIONS.aid))).all()

        data = OrderedDict()
        for bp in blueprints:
            # As some item cannot be manufactured, catch the exception
            try:
                product = bp.material.product_for_activities
                product = product.filter((
                    ActivityProduct.activity == ActivityEnum.MANUFACTURING.aid)
                                         | (ActivityProduct.activity ==
                                            ActivityEnum.REACTIONS.aid)).one()
                bp_final = product.blueprint
            except NoResultFound:
                continue

            activity = bp_final.activities.filter(
                (Activity.activity == ActivityEnum.MANUFACTURING.aid)
                | (Activity.activity == ActivityEnum.REACTIONS.aid)).one()

            mats = bp_final.activity_materials.filter(
                (ActivityMaterial.activity == ActivityEnum.MANUFACTURING.aid)
                | (ActivityMaterial.activity == ActivityEnum.REACTIONS.aid)
            ).all()

            if bp_final.id not in data:
                data[bp_final.id] = {
                    'id': bp_final.id,
                    'icon': bp_final.icon_32(),
                    'name': bp_final.name,
                    'volume': bp.material.volume,
                    'materials': [],
                    'time': activity.time,
                    'product_id': bp.material.id,
                    'product_name': bp.material.name,
                    'product_qty_per_run': product.quantity,
                    'max_run_per_bp': bp_final.max_production_limit,
                }

            for mat in mats:
                pref = current_user.pref

                if mat.material.is_moon_goo():
                    price_type = pref.prod_price_type_moongoo
                    price_region = pref.prod_price_region_moongoo
                elif mat.material.is_pi():
                    price_type = pref.prod_price_type_pi
                    price_region = pref.prod_price_region_pi
                elif mat.material.is_mineral_salvage():
                    price_type = pref.prod_price_type_minerals
                    price_region = pref.prod_price_region_minerals
                else:
                    price_type = pref.prod_price_type_others
                    price_region = pref.prod_price_region_others

                data[bp_final.id]['materials'].append({
                    'id':
                    mat.material.id,
                    'name':
                    mat.material.name,
                    'quantity':
                    mat.quantity,
                    'volume':
                    mat.material.volume,
                    'icon':
                    mat.material.icon_32(),
                    'price_type':
                    price_type,
                    'price_region':
                    price_region,
                })

        return jsonify(result=list(data.values()))

    else:
        return 'Cannot call this page directly', 403