Exemplo n.º 1
0
def edit_apikey(request):
    if request.method == 'POST':
        form = DirectorApiKeyForm(request.POST)
        if form.is_valid():
            keyID = form.cleaned_data.get('keyID')
            vCode = form.cleaned_data.get('vCode')
            characterID = form.cleaned_data.get('characterID')

            api.set_api(keyID, vCode, characterID)
            tasks_to_execute = ScheduledTask.objects.filter(
                is_active=True).order_by("-priority")
            tasks_to_execute.update(is_scheduled=True)

            process.run_async(*tasks_to_execute)

            return redirect('/scheduler/tasks/')
    else:
        try:
            keyID = Setting.get('common_api_keyID')
            vCode = Setting.get('common_api_vCode')
        except Setting.DoesNotExist:
            keyID = 0
            vCode = ''
        form = DirectorApiKeyForm(initial={
            'keyID': keyID,
            'vCode': vCode,
        })
    return render_to_response('ecm/common/edit_director_api.html',
                              {'form': form}, Ctx(request))
Exemplo n.º 2
0
def edit_apikey(request):
    if request.method == 'POST':
        form = DirectorApiKeyForm(request.POST)
        if form.is_valid():
            keyID = form.cleaned_data.get('keyID')
            vCode = form.cleaned_data.get('vCode')
            characterID = form.cleaned_data.get('characterID')
            
            api.set_api(keyID, vCode, characterID)
            tasks_to_execute = ScheduledTask.objects.filter(is_active=True).order_by("-priority")
            tasks_to_execute.update(is_scheduled=True)
            
            process.run_async(*tasks_to_execute)
            
            return redirect('/scheduler/tasks/')
    else:
        try:
            keyID = Setting.get('common_api_keyID')
            vCode = Setting.get('common_api_vCode')
        except Setting.DoesNotExist:
            keyID = 0
            vCode = ''
        form = DirectorApiKeyForm(initial={
            'keyID': keyID,
            'vCode': vCode,
        })
    return render_to_response('ecm/common/edit_director_api.html',
                              {'form': form}, Ctx(request))
Exemplo n.º 3
0
def get_api():
    keyID = Setting.get(name='common_api_keyID')
    vCode = Setting.get(name='common_api_vCode')
    if not keyID or not vCode:
        raise Setting.DoesNotExist('the settings "common_api_keyID" or "common_api_vCode" are empty')
    else:
        return keyID, vCode
Exemplo n.º 4
0
def get_charID():
    characterID = Setting.get(name='common_api_characterID')
    if not characterID:
        raise Setting.DoesNotExist(
            'the setting "common_api_characterID" is empty')
    else:
        return characterID
Exemplo n.º 5
0
def get_api():
    keyID = Setting.get(name='common_api_keyID')
    vCode = Setting.get(name='common_api_vCode')
    if not keyID or not vCode:
        raise Setting.DoesNotExist(
            'the settings "common_api_keyID" or "common_api_vCode" are empty')
    else:
        return keyID, vCode
Exemplo n.º 6
0
def oper_data(request, pos_id):
    try:
        params = extract_datatable_params(request)
        pos_id = int(pos_id)
    except:
        return HttpResponseBadRequest()
    pos = get_object_or_404(POS, item_id=pos_id)
    oper_table = []
    members = Setting.get('hr_corp_members_group_name')
    for user in pos.operators.all():
        oper_table.append([
            user.username,
            ', '.join(user.characters.all().values_list('name', flat=True)),
            ', '.join(
                user.groups.exclude(name=members).values_list('name',
                                                              flat=True)),
        ])

    operator_count = pos.operators.all().count()
    json_data = {
        "sEcho": params.sEcho,
        "iTotalRecords": operator_count,
        "iTotalDisplayRecords": operator_count,
        "aaData": oper_table,
    }
    return HttpResponse(json.dumps(json_data))
Exemplo n.º 7
0
def public_price(request, item_id):
    """
    Serves URL /industry/catalog/items/<item_id>/public_price/

    update the public price of the item
    return the price formatted as a string
    """
    try:
        error = None
        type_id = int(item_id)
        supply_source_id = Setting.get('industry_default_price_source')
        try:
            buyPrices = evecentral.get_buy_prices([type_id], supply_source_id)
            item = get_object_or_404(CatalogEntry, typeID=type_id)
            if buyPrices[type_id] > 0.0 and item.public_price != buyPrices[type_id]:
                item.public_price = buyPrices[type_id]
                logger.info('New price for "%s" -> %s' % (item.typeName,
                                                      print_float(buyPrices[type_id])))
                item.save()
        except KeyError:
            error = 'Could not find buy-price for item: "%s"' % item
            
        if error:
            return HttpResponseBadRequest(error)
        else:
            return HttpResponse(print_float(item.public_price))
    except ValueError:
        raise Http404()
Exemplo n.º 8
0
def public_price(request, item_id):
    """
    Serves URL /industry/catalog/items/<item_id>/public_price/

    update the public price of the item
    return the price formatted as a string
    """
    try:
        error = None
        type_id = int(item_id)
        supply_source_id = Setting.get('industry_default_price_source')
        try:
            buyPrices = evecentral.get_buy_prices([type_id], supply_source_id)
            item = get_object_or_404(CatalogEntry, typeID=type_id)
            if buyPrices[type_id] > 0.0 and item.public_price != buyPrices[
                    type_id]:
                item.public_price = buyPrices[type_id]
                logger.info('New price for "%s" -> %s' %
                            (item.typeName, print_float(buyPrices[type_id])))
                item.save()
        except KeyError:
            error = 'Could not find buy-price for item: "%s"' % item

        if error:
            return HttpResponseBadRequest(error)
        else:
            return HttpResponse(print_float(item.public_price))
    except ValueError:
        raise Http404()
Exemplo n.º 9
0
def get_sell_prices(item_ids, systemID):

    prices = {}
    for some_ids in tools.sublists(item_ids, 50):

        params = [("typeid", type_id) for type_id in some_ids]
        if systemID != 1:
            params.append(("usesystem", systemID))

        evecentralurl = Setting.get('industry_evecentral_url')

        url = evecentralurl + '?' + urllib.urlencode(params)

        try:
            response = urllib2.urlopen(url)
        except (URLError, HTTPError), err:
            LOG.warning(str(err))
            continue

        element = ElementTree.parse(source=response)
        for typ in element.findall('.//type'):
            typeID = int(typ.attrib['id'])
            buyMax = typ.find('sell/min')
            if buyMax is not None:
                prices[typeID] = round(float(buyMax.text), 2)
Exemplo n.º 10
0
def get_sell_prices(item_ids, systemID):

    prices = {}
    for some_ids in tools.sublists(item_ids, 50):

        params = [ ("typeid", type_id) for type_id in some_ids ]
        if systemID != 1:
            params.append(("usesystem", systemID))
        
        evecentralurl = Setting.get('industry_evecentral_url')
        
        url = evecentralurl + '?' + urllib.urlencode(params)
        
        try:
            response = urllib2.urlopen(url)
        except (URLError, HTTPError), err:
            LOG.warning(str(err))
            continue
        
        element = ElementTree.parse(source=response)
        for typ in element.findall('.//type'):
            typeID = int(typ.attrib['id'])
            buyMax = typ.find('sell/min')
            if buyMax is not None:
                prices[typeID] = round(float(buyMax.text), 2)
Exemplo n.º 11
0
 def resolve_surcharge(item, user, price):
     active_policies = PricingPolicy.objects.filter(is_active=True)
     
     if active_policies.filter(user_group__in=user.groups.all()):
         policies = active_policies.filter(user_group__in=user.groups.all())
     else:
         # if no policy matches the user's groups, we use only non group-based policies
         policies = active_policies.filter(user_group__isnull=True)
     
     if policies.filter(item_group__in=item.item_groups.all()):
         policies = policies.filter(item_group__in=item.item_groups.all())
     else:
         # if no policy matches the item, we use only non item-based policies
         policies = policies.filter(item_group__isnull=True)
     
     if policies:
         policies = policies.extra(
             select={'total_surcharge': '%s * surcharge_relative + surcharge_absolute'},
             select_params=(price,),
         )
         policies = policies.order_by('-priority', 'total_surcharge')
         surcharge = policies[0].total_surcharge # take the smallest surcharge
     else:
         # If no policy is defined for this (item, user) combination,
         # we fallback to the default margin.
         surcharge = Setting.get('industry_default_margin') * price
     
     return surcharge
Exemplo n.º 12
0
 def resolve_surcharge(item, user, price):
     active_policies = PricingPolicy.objects.filter(is_active=True)
     
     if active_policies.filter(user_group__in=user.groups.all()):
         policies = active_policies.filter(user_group__in=user.groups.all())
     else:
         # if no policy matches the user's groups, we use only non group-based policies
         policies = active_policies.filter(user_group__isnull=True)
     
     if policies.filter(item_group__in=item.item_groups.all()):
         policies = policies.filter(item_group__in=item.item_groups.all())
     else:
         # if no policy matches the item, we use only non item-based policies
         policies = policies.filter(item_group__isnull=True)
     
     if policies:
         policies = policies.extra(
             select={'total_surcharge': '%s * surcharge_relative + surcharge_absolute'},
             select_params=(price,),
         )
         policies = policies.order_by('-priority', 'total_surcharge')
         surcharge = policies[0].total_surcharge # take the smallest surcharge
     else:
         # If no policy is defined for this (item, user) combination,
         # we fallback to the default margin.
         surcharge = Setting.get('industry_default_margin') * price
     
     return surcharge
Exemplo n.º 13
0
def get_systems_data(request):

    divisions = extract_divisions(request)
    show_in_space = json.loads(request.GET.get('space', 'true'))
    show_in_stations = json.loads(request.GET.get('stations', 'true'))

    where = []
    if not show_in_space:
        where.append('"stationID" < %d' % constants.MAX_STATION_ID)
    if not show_in_stations:
        where.append('"stationID" > %d' % constants.MAX_STATION_ID)
    if divisions is not None:
        where.append('"hangarID" IN (%s)' % ', '.join(['%s'] * len(divisions)))

    sql = 'SELECT "solarSystemID", COUNT(*) AS "items", SUM("volume") AS "volume" '\
          'FROM "assets_asset" '
    if where: sql += ' WHERE ' + ' AND '.join(where)
    sql += ' GROUP BY "solarSystemID";'
    sql = db.fix_mysql_quotes(sql)

    cursor = connection.cursor() #@UndefinedVariable
    if divisions is None:
        cursor.execute(sql)
    else:
        cursor.execute(sql, divisions)

    exact_volumes = Setting.get('assets_show_exact_volumes')

    jstree_data = []
    for solarSystemID, items, volume in cursor:
        try:
            system = CelestialObject.objects.get(itemID=solarSystemID)
        except CelestialObject.DoesNotExist:
            system = CelestialObject(itemID=solarSystemID, itemName=str(solarSystemID), security=0)
        if system.security > 0.5:
            color = 'hisec'
        elif system.security > 0:
            color = 'lowsec'
        else:
            color = 'nullsec'

        if exact_volumes:
            volume = print_float(volume)
        else:
            volume = round_quantity(volume)

        jstree_data.append({
            'data' : HTML_ITEM_SPAN % (system.itemName, items, pluralize(items), volume),
            'attr' : {
                'id' : '%d_' % solarSystemID,
                'rel' : 'system',
                'sort_key' : system.itemName.lower(),
                'class' : 'system-%s-row' % color
            },
            'state' : 'closed'
        })
    cursor.close()
    return HttpResponse(json.dumps(jstree_data))
Exemplo n.º 14
0
def get_celestial_objects_data(request, solarSystemID):
    solarSystemID = int(solarSystemID)
    divisions = extract_divisions(request)
    show_in_space = json.loads(request.GET.get('space', 'true'))
    show_in_stations = json.loads(request.GET.get('stations', 'true'))

    where = []
    if not show_in_space:
        where.append('"stationID" < %d' % constants.MAX_STATION_ID)
    if not show_in_stations:
        where.append('"stationID" > %d' % constants.MAX_STATION_ID)
    if divisions is not None:
        where.append('"hangarID" IN (%s)' % ', '.join(['%s'] * len(divisions)))

    sql = 'SELECT "closest_object_id", COUNT(*), SUM("volume") '\
          'FROM "assets_asset" '\
          'WHERE "solarSystemID"=%s '
    if where:
        sql += ' AND ' + ' AND '.join(where)
    sql += ' GROUP BY "closest_object_id";'
    sql = db.fix_mysql_quotes(sql)

    cursor = connection.cursor() #@UndefinedVariable
    if divisions is None:
        cursor.execute(sql, [solarSystemID])
    else:
        cursor.execute(sql, [solarSystemID] + list(divisions))

    exact_volumes = Setting.get('assets_show_exact_volumes')

    jstree_data = []
    for closest_object_id, items, volume in cursor:

        if closest_object_id != 0:
            try:
                name = CelestialObject.objects.get(itemID=closest_object_id).itemName
            except CelestialObject.DoesNotExist:
                name = str(closest_object_id)
        else:
            name = 'Stations'

        if exact_volumes:
            volume = print_float(volume)
        else:
            volume = round_quantity(volume)

        jstree_data.append({
            'data' : HTML_ITEM_SPAN % (name, items, pluralize(items), volume),
            'attr' : {
                'id' : '%d_%d_' % (solarSystemID, closest_object_id),
                'sort_key' : closest_object_id,
                'rel' : 'celestial',
                'class' : 'celestial-row',
            },
            'state' : 'closed'
        })
    cursor.close()
    return HttpResponse(json.dumps(jstree_data))
Exemplo n.º 15
0
def get_hangars_data(request, solarSystemID, closest_obj_id, stationID):
    solarSystemID = int(solarSystemID)
    closest_obj_id = int(closest_obj_id)
    stationID = int(stationID)
    divisions = extract_divisions(request)

    where = []
    if divisions is not None:
        where.append('"hangarID" IN (%s)' % ', '.join(['%s'] * len(divisions)))

    sql = 'SELECT "hangarID", COUNT(*) AS "items", SUM("volume") AS "volume" '\
          'FROM "assets_asset" '\
          'WHERE "solarSystemID"=%s AND "closest_object_id"=%s AND "stationID"=%s '
    if where: sql += ' AND ' + ' AND '.join(where)
    sql += ' GROUP BY "hangarID";'
    sql = db.fix_mysql_quotes(sql)

    cursor = connection.cursor()  #@UndefinedVariable
    if divisions is None:
        cursor.execute(sql, [solarSystemID, closest_obj_id, stationID])
    else:
        cursor.execute(sql, [solarSystemID, closest_obj_id, stationID] +
                       list(divisions))

    exact_volumes = Setting.get('assets_show_exact_volumes')

    HANGAR = Hangar.DEFAULT_NAMES.copy()
    for h in CorpHangar.objects.filter(corp=Corporation.objects.mine()):
        HANGAR[h.hangar_id] = h.name

    jstree_data = []
    for hangarID, items, volume in cursor:

        if exact_volumes:
            volume = print_float(volume)
        else:
            volume = round_quantity(volume)

        jstree_data.append({
            'data':
            HTML_ITEM_SPAN %
            (HANGAR[hangarID], items, pluralize(items), volume),
            'attr': {
                'id':
                '%d_%d_%d_%d_' %
                (solarSystemID, closest_obj_id, stationID, hangarID),
                'sort_key':
                hangarID,
                'rel':
                'hangar',
                'class':
                'hangar-row'
            },
            'state':
            'closed'
        })

    return HttpResponse(json.dumps(jstree_data))
Exemplo n.º 16
0
def players(request):
    query = User.objects.select_related(depth=3).filter(is_active=True)
    query = query.annotate(char_count=Count("characters"))
    query = query.filter(char_count__gt=0)
    query = query.exclude(username__in=[Setting.get('common_cron_username'), 
                                        Setting.get('common_admin_username')])
    members = []
    for player in query:
        characters = list(player.characters.values('name',
                                                   'characterID'))
        characters.sort(key=lambda x: x['name'].lower())
        bindings = list(player.bindings.values('external_app__name',
                                               'external_name',
                                               'external_id'))
        members.append({
            'id': player.id,
            'username': player.username,
            'characters': characters,
            'bindings': bindings
        })
    return HttpResponse(json.dumps(members))
Exemplo n.º 17
0
def players(request):
    query = User.objects.select_related(depth=3).filter(is_active=True)
    query = query.annotate(char_count=Count("characters"))
    query = query.filter(char_count__gt=0)
    query = query.exclude(username__in=[
        Setting.get('common_cron_username'),
        Setting.get('common_admin_username')
    ])
    members = []
    for player in query:
        characters = list(player.characters.values('name', 'characterID'))
        characters.sort(key=lambda x: x['name'].lower())
        bindings = list(
            player.bindings.values('external_app__name', 'external_name',
                                   'external_id'))
        members.append({
            'id': player.id,
            'username': player.username,
            'characters': characters,
            'bindings': bindings
        })
    return HttpResponse(json.dumps(members))
Exemplo n.º 18
0
def get_hangars_data(request, solarSystemID, closest_obj_id, stationID):
    solarSystemID = int(solarSystemID)
    closest_obj_id = int(closest_obj_id)
    stationID = int(stationID)
    divisions = extract_divisions(request)

    where = []
    if divisions is not None:
        where.append('"hangarID" IN (%s)' % ', '.join(['%s'] * len(divisions)))

    sql = 'SELECT "hangarID", COUNT(*) AS "items", SUM("volume") AS "volume" '\
          'FROM "assets_asset" '\
          'WHERE "solarSystemID"=%s AND "closest_object_id"=%s AND "stationID"=%s '
    if where: sql += ' AND ' + ' AND '.join(where)
    sql += ' GROUP BY "hangarID";'
    sql = db.fix_mysql_quotes(sql)

    cursor = connection.cursor() #@UndefinedVariable
    if divisions is None:
        cursor.execute(sql, [solarSystemID, closest_obj_id, stationID])
    else:
        cursor.execute(sql, [solarSystemID, closest_obj_id, stationID] + list(divisions))

    exact_volumes = Setting.get('assets_show_exact_volumes')

    HANGAR = Hangar.DEFAULT_NAMES.copy()
    for h in CorpHangar.objects.filter(corp=Corporation.objects.mine()):
        HANGAR[h.hangar_id] = h.name

    jstree_data = []
    for hangarID, items, volume in cursor:

        if exact_volumes:
            volume = print_float(volume)
        else:
            volume = round_quantity(volume)

        jstree_data.append({
            'data': HTML_ITEM_SPAN % (HANGAR[hangarID], items, pluralize(items), volume),
            'attr' : {
                'id' : '%d_%d_%d_%d_' % (solarSystemID, closest_obj_id, stationID, hangarID),
                'sort_key' : hangarID,
                'rel' : 'hangar',
                'class' : 'hangar-row'
            },
            'state' : 'closed'
        })

    return HttpResponse(json.dumps(jstree_data))
Exemplo n.º 19
0
def corp_standings(request):

    my_corp = Corporation.objects.mine()

    corp_id = request.GET.get('corp')
    if corp_id is not None:
        try:
            corp = Corporation.objects.get(corporationID=int(corp_id))
        except (ValueError, Corporation.DoesNotExist):
            raise Http404()
    else:
        corp = my_corp

    visibility = request.POST.get('standings_visibility')
    if request.user.is_superuser and visibility is not None:
        Setting.objects.filter(name='standings_visibility').update(
            value=repr(visibility))

    standings = Standing.objects.filter(corp=corp)

    alliance_standings = standings.filter(is_corp_contact=False)
    alliance_standings = alliance_standings.order_by('-value', 'contactName')
    corp_standings = standings.filter(is_corp_contact=True)
    corp_standings = corp_standings.order_by('-value', 'contactName')

    corporations = Corporation.objects.others().annotate(
        standing_cnt=Count('standings'))

    data = {
        'standings_visibility':
        Setting.get('standings_visibility'),
        'alliance_standings':
        alliance_standings,
        'corp_standings':
        corp_standings,
        'selected_corp':
        corp,
        'corporations':
        corporations.filter(standing_cnt__gt=0).order_by('corporationName'),
    }

    return render_to_response(
        "ecm/corp/standings.html",
        data,
        RequestContext(request),
    )
Exemplo n.º 20
0
def corp_standings(request):
    
    my_corp = Corporation.objects.mine()
    
    corp_id = request.GET.get('corp')
    if corp_id is not None:
        try:
            corp = Corporation.objects.get(corporationID=int(corp_id))
        except (ValueError, Corporation.DoesNotExist):
            raise Http404()
    else:
        corp = my_corp
      
    visibility = request.POST.get('standings_visibility')
    if request.user.is_superuser and visibility is not None:
        Setting.objects.filter(name='standings_visibility').update(value=repr(visibility))
    
    standings = Standing.objects.filter(corp=corp)
    
    alliance_standings = standings.filter(is_corp_contact=False)
    alliance_standings = alliance_standings.order_by('-value', 'contactName')
    corp_standings = standings.filter(is_corp_contact=True)
    corp_standings = corp_standings.order_by('-value', 'contactName')
    
    corporations = Corporation.objects.others().annotate(standing_cnt=Count('standings'))

    data = {
        'standings_visibility': Setting.get('standings_visibility'),
        'alliance_standings': alliance_standings,
        'corp_standings': corp_standings,
        'selected_corp': corp,
        'corporations': corporations.filter(standing_cnt__gt=0).order_by('corporationName'),
    }
    
    return render_to_response("ecm/corp/standings.html",
                              data,
                              RequestContext(request),
                              )
Exemplo n.º 21
0
def oper_data(request, pos_id):
    try:
        params = extract_datatable_params(request)
        pos_id = int(pos_id)
    except:
        return HttpResponseBadRequest()
    pos = get_object_or_404(POS, item_id=pos_id)
    oper_table = []
    members = Setting.get('hr_corp_members_group_name')
    for user in pos.operators.all():
        oper_table.append([
            user.username,
            ', '.join(user.characters.all().values_list('name', flat=True)),
            ', '.join(user.groups.exclude(name=members).values_list('name', flat=True)),
        ])
        
    operator_count = pos.operators.all().count()
    json_data = {
        "sEcho"                 : params.sEcho,
        "iTotalRecords"         : operator_count,
        "iTotalDisplayRecords"  : operator_count,
        "aaData"                : oper_table,
    }
    return HttpResponse(json.dumps(json_data))
Exemplo n.º 22
0
def update():
    """
    Retrieve all corp assets and calculate the changes.

    If there's an error, nothing is written in the database
    """
    global IGNORE_CONTAINERS_VOLUMES

    LOG.info("fetching /corp/AssetList.xml.aspx...")
    api_conn = api.connect()
    apiAssets = api_conn.corp.AssetList(characterID=api.get_charID())
    api.check_version(apiAssets._meta.version)

    currentTime = timezone.make_aware(apiAssets._meta.currentTime,
                                      timezone.utc)
    cachedUntil = timezone.make_aware(apiAssets._meta.cachedUntil,
                                      timezone.utc)
    LOG.debug("current time : %s", str(currentTime))
    LOG.debug("cached util : %s", str(cachedUntil))

    LOG.debug("fetching old assets from the database...")
    old_items = {}
    for asset in Asset.objects.all():
        old_items[asset] = asset

    new_items = {}
    LOG.debug("%d assets fetched", len(old_items.keys()))

    IGNORE_CONTAINERS_VOLUMES = Setting.get('assets_ignore_containers_volumes')

    # we store the itemIDs of all the assets we want to locate
    # then query /corp/Locations.xml with the list
    assets_to_locate = []

    fill_cache()

    LOG.debug("parsing api response...")
    for row in apiAssets.assets:
        if row.typeID == cst.BOOKMARK_TYPEID:
            continue  # we don't give a flying @#!$ about the bookmarks...

        if row.locationID >= cst.STATIONS_IDS:
            # this row contains assets in a station
            if row.typeID == cst.OFFICE_TYPEID:
                row_is_office(office=row, items_dic=new_items)
            else:
                row_is_in_hangar(item=row, items_dic=new_items)
        else:
            # this row contains assets in space
            try:
                if cst.HAS_HANGAR_DIVISIONS[row.typeID]:
                    row_is_pos_corp_hangar(corpArray=row, items_dic=new_items)
                else:
                    row_is_pos_array(array=row, items_dic=new_items)
                assets_to_locate.append(row.itemID)
            except KeyError:
                # unhandled typeID, this may be a reactor array or some other crap
                pass

    LOG.info("%d assets parsed", len(new_items))

    clear_cache()

    # I grouped all the DB writes here so that it doesn't make a too long DB transaction.
    # The assets parsing can last more than 10 minutes on slow servers.
    write_results(new_items, old_items, assets_to_locate, currentTime)

    diffs = []
    if old_items:
        LOG.debug("computing diffs since last asset scan...")
        diffs = calc_assets_diff(old_items=old_items,
                                 new_items=new_items,
                                 date=currentTime)

    if diffs:
        write_diff_results(diffs, currentTime)
Exemplo n.º 23
0
def get_charID():
    characterID = Setting.get(name='common_api_characterID')
    if not characterID:
        raise Setting.DoesNotExist('the setting "common_api_characterID" is empty')
    else:
        return characterID
Exemplo n.º 24
0
def get_allies_plus_10_group():
    return get_or_create_group(Setting.get('hr_allies_plus_10_group_name'))
Exemplo n.º 25
0
def get_members_group():
    return get_or_create_group(Setting.get('hr_corp_members_group_name'))
Exemplo n.º 26
0
__date__ = "2011 6 26"
__author__ = "diabeteman"

from django.shortcuts import get_object_or_404
from django.http import HttpResponse
from django.db.models.aggregates import Count
from django.contrib.auth.models import User

from ecm.utils import _json as json
from ecm.views.decorators import basic_auth_required
from ecm.apps.common.models import ExternalApplication, GroupBinding, UserBinding, Setting


#------------------------------------------------------------------------------
@basic_auth_required(username=Setting.get('common_cron_username'))
def players(request):
    query = User.objects.select_related(depth=3).filter(is_active=True)
    query = query.annotate(char_count=Count("characters"))
    query = query.filter(char_count__gt=0)
    query = query.exclude(username__in=[
        Setting.get('common_cron_username'),
        Setting.get('common_admin_username')
    ])
    members = []
    for player in query:
        characters = list(player.characters.values('name', 'characterID'))
        characters.sort(key=lambda x: x['name'].lower())
        bindings = list(
            player.bindings.values('external_app__name', 'external_name',
                                   'external_id'))
Exemplo n.º 27
0
def get_directors_group():
    return get_or_create_group(Setting.get('hr_directors_group_name'))
Exemplo n.º 28
0
def get_members_group():
    return get_or_create_group(Setting.get('hr_corp_members_group_name'))
Exemplo n.º 29
0
# The creation of the declared objects is delayed here.
# If not, it would crash at first try of synchronizing the db
# as tables are not created yet.
for app in apps.LIST:
    create_app_objects(app)
for plugin in plugins.LIST:
    create_app_objects(plugin)

# When the server is shutdown while a scheduled task is running, it gets frozen
# and cannot be triggered anymore.
# The first time this module is imported, we reset all the scheduled tasks to
# is_running = False to avoid this problem.
ScheduledTask.objects.all().update(is_running=False, is_scheduled=False)

#------------------------------------------------------------------------------
admin_username = Setting.get('common_admin_username')
if not User.objects.filter(username=admin_username):
    try:
        logger.info('superuser "%s" does not exists. Creating...' %
                    admin_username)
        User.objects.create_superuser(username=admin_username,
                                      email='',
                                      password='******')
    except:
        logger.exception("")
        raise


#-----------------------------------------------------------------------------
def import_monkey_patches():
    import ecm.lib.templatepatch  #@UnusedImport for multi line template tags
Exemplo n.º 30
0
def get_stations_data(request, solarSystemID, closest_obj_id):
    solarSystemID = int(solarSystemID)
    closest_obj_id = int(closest_obj_id)
    divisions = extract_divisions(request)
    show_in_space = json.loads(request.GET.get('space', 'true'))
    show_in_stations = json.loads(request.GET.get('stations', 'true'))

    where = []
    if not show_in_space:
        where.append('"stationID" < %d' % constants.MAX_STATION_ID)
    if not show_in_stations:
        where.append('"stationID" > %d' % constants.MAX_STATION_ID)
    if divisions is not None:
        where.append('"hangarID" IN (%s)' % ', '.join(['%s'] * len(divisions)))

    sql = 'SELECT "stationID", MAX("name"), MAX("flag"), COUNT(*), SUM("volume") '\
          'FROM "assets_asset" '\
          'WHERE "solarSystemID"=%s AND "closest_object_id"=%s '
    if where: sql += ' AND ' + ' AND '.join(where)
    sql += ' GROUP BY "stationID";'
    sql = db.fix_mysql_quotes(sql)

    cursor = connection.cursor()  #@UndefinedVariable
    if divisions is None:
        cursor.execute(sql, [solarSystemID, closest_obj_id])
    else:
        cursor.execute(sql, [solarSystemID, closest_obj_id] + list(divisions))

    exact_volumes = Setting.get('assets_show_exact_volumes')

    jstree_data = []
    for stationID, item_name, flag, items, volume in cursor:
        if stationID < constants.MAX_STATION_ID:
            # it's a real station
            try:
                name = CelestialObject.objects.get(itemID=stationID).itemName
            except CelestialObject.DoesNotExist:
                name = str(stationID)
            icon = 'station'
        else:
            # it is an inspace anchorable array
            type_name = Type.objects.get(typeID=flag).typeName

            name = type_name
            if item_name and type_name != item_name:
                name += ' "%s"' % item_name

            if constants.CONTROL_TOWERS.has_key(flag):
                icon = 'pos'
            else:
                icon = 'array'

        if exact_volumes:
            volume = print_float(volume)
        else:
            volume = round_quantity(volume)

        jstree_data.append({
            'data':
            HTML_ITEM_SPAN % (name, items, pluralize(items), volume),
            'attr': {
                'id': '%d_%d_%d_' % (solarSystemID, closest_obj_id, stationID),
                'sort_key': stationID,
                'rel': icon,
                'class': '%s-row' % icon
            },
            'state':
            'closed'
        })
    cursor.close()
    return HttpResponse(json.dumps(jstree_data))
Exemplo n.º 31
0
def get_celestial_objects_data(request, solarSystemID):
    solarSystemID = int(solarSystemID)
    divisions = extract_divisions(request)
    show_in_space = json.loads(request.GET.get('space', 'true'))
    show_in_stations = json.loads(request.GET.get('stations', 'true'))

    where = []
    if not show_in_space:
        where.append('"stationID" < %d' % constants.MAX_STATION_ID)
    if not show_in_stations:
        where.append('"stationID" > %d' % constants.MAX_STATION_ID)
    if divisions is not None:
        where.append('"hangarID" IN (%s)' % ', '.join(['%s'] * len(divisions)))

    sql = 'SELECT "closest_object_id", COUNT(*), SUM("volume") '\
          'FROM "assets_asset" '\
          'WHERE "solarSystemID"=%s '
    if where:
        sql += ' AND ' + ' AND '.join(where)
    sql += ' GROUP BY "closest_object_id";'
    sql = db.fix_mysql_quotes(sql)

    cursor = connection.cursor()  #@UndefinedVariable
    if divisions is None:
        cursor.execute(sql, [solarSystemID])
    else:
        cursor.execute(sql, [solarSystemID] + list(divisions))

    exact_volumes = Setting.get('assets_show_exact_volumes')

    jstree_data = []
    for closest_object_id, items, volume in cursor:

        if closest_object_id != 0:
            try:
                name = CelestialObject.objects.get(
                    itemID=closest_object_id).itemName
            except CelestialObject.DoesNotExist:
                name = str(closest_object_id)
        else:
            name = 'Stations'

        if exact_volumes:
            volume = print_float(volume)
        else:
            volume = round_quantity(volume)

        jstree_data.append({
            'data':
            HTML_ITEM_SPAN % (name, items, pluralize(items), volume),
            'attr': {
                'id': '%d_%d_' % (solarSystemID, closest_object_id),
                'sort_key': closest_object_id,
                'rel': 'celestial',
                'class': 'celestial-row',
            },
            'state':
            'closed'
        })
    cursor.close()
    return HttpResponse(json.dumps(jstree_data))
Exemplo n.º 32
0
def update():
    """
    Retrieve all corp assets and calculate the changes.

    If there's an error, nothing is written in the database
    """
    global IGNORE_CONTAINERS_VOLUMES

    LOG.info("fetching /corp/AssetList.xml.aspx...")
    api_conn = api.connect()
    apiAssets = api_conn.corp.AssetList(characterID=api.get_charID())
    api.check_version(apiAssets._meta.version)

    currentTime = timezone.make_aware(apiAssets._meta.currentTime, timezone.utc)
    cachedUntil = timezone.make_aware(apiAssets._meta.cachedUntil, timezone.utc)
    LOG.debug("current time : %s", str(currentTime))
    LOG.debug("cached util : %s", str(cachedUntil))

    LOG.debug("fetching old assets from the database...")
    old_items = {}
    for asset in Asset.objects.all():
        old_items[asset] = asset

    new_items = {}
    LOG.debug("%d assets fetched", len(old_items.keys()))

    IGNORE_CONTAINERS_VOLUMES = Setting.get('assets_ignore_containers_volumes')

    # we store the itemIDs of all the assets we want to locate
    # then query /corp/Locations.xml with the list
    assets_to_locate = []

    fill_cache()

    LOG.debug("parsing api response...")
    for row in apiAssets.assets:
        if row.typeID == cst.BOOKMARK_TYPEID:
            continue # we don't give a flying @#!$ about the bookmarks...

        if row.locationID >= cst.STATIONS_IDS:
            # this row contains assets in a station
            if row.typeID == cst.OFFICE_TYPEID:
                row_is_office(office=row, items_dic=new_items)
            else:
                row_is_in_hangar(item=row, items_dic=new_items)
        else:
            # this row contains assets in space
            try:
                if cst.HAS_HANGAR_DIVISIONS[row.typeID]:
                    row_is_pos_corp_hangar(corpArray=row, items_dic=new_items)
                else:
                    row_is_pos_array(array=row, items_dic=new_items)
                assets_to_locate.append(row.itemID)
            except KeyError:
                # unhandled typeID, this may be a reactor array or some other crap
                pass

    LOG.info("%d assets parsed", len(new_items))

    clear_cache()

    # I grouped all the DB writes here so that it doesn't make a too long DB transaction.
    # The assets parsing can last more than 10 minutes on slow servers.
    write_results(new_items, old_items, assets_to_locate, currentTime)

    diffs = []
    if old_items:
        LOG.debug("computing diffs since last asset scan...")
        diffs = calc_assets_diff(old_items=old_items, new_items=new_items, date=currentTime)

    if diffs:
        write_diff_results(diffs, currentTime)
Exemplo n.º 33
0
def get_stations_data(request, solarSystemID, closest_obj_id):
    solarSystemID = int(solarSystemID)
    closest_obj_id = int(closest_obj_id)
    divisions = extract_divisions(request)
    show_in_space = json.loads(request.GET.get('space', 'true'))
    show_in_stations = json.loads(request.GET.get('stations', 'true'))

    where = []
    if not show_in_space:
        where.append('"stationID" < %d' % constants.MAX_STATION_ID)
    if not show_in_stations:
        where.append('"stationID" > %d' % constants.MAX_STATION_ID)
    if divisions is not None:
        where.append('"hangarID" IN (%s)' % ', '.join(['%s'] * len(divisions)))

    sql = 'SELECT "stationID", MAX("name"), MAX("flag"), COUNT(*), SUM("volume") '\
          'FROM "assets_asset" '\
          'WHERE "solarSystemID"=%s AND "closest_object_id"=%s '
    if where: sql += ' AND ' + ' AND '.join(where)
    sql += ' GROUP BY "stationID";'
    sql = db.fix_mysql_quotes(sql)

    cursor = connection.cursor() #@UndefinedVariable
    if divisions is None:
        cursor.execute(sql, [solarSystemID, closest_obj_id])
    else:
        cursor.execute(sql, [solarSystemID, closest_obj_id] + list(divisions))

    exact_volumes = Setting.get('assets_show_exact_volumes')

    jstree_data = []
    for stationID, item_name, flag, items, volume in cursor:
        if stationID < constants.MAX_STATION_ID:
            # it's a real station
            try:
                name = CelestialObject.objects.get(itemID=stationID).itemName
            except CelestialObject.DoesNotExist:
                name = str(stationID)
            icon = 'station'
        else:
            # it is an inspace anchorable array
            type_name = Type.objects.get(typeID=flag).typeName

            name = type_name
            if item_name and type_name != item_name:
                name += ' "%s"' % item_name

            if constants.CONTROL_TOWERS.has_key(flag):
                icon = 'pos'
            else:
                icon = 'array'

        if exact_volumes:
            volume = print_float(volume)
        else:
            volume = round_quantity(volume)

        jstree_data.append({
            'data' : HTML_ITEM_SPAN % (name, items, pluralize(items), volume),
            'attr' : {
                'id' : '%d_%d_%d_' % (solarSystemID, closest_obj_id, stationID),
                'sort_key' : stationID,
                'rel' : icon,
                'class' : '%s-row' % icon
            },
            'state' : 'closed'
        })
    cursor.close()
    return HttpResponse(json.dumps(jstree_data))
Exemplo n.º 34
0
def get_systems_data(request):

    divisions = extract_divisions(request)
    show_in_space = json.loads(request.GET.get('space', 'true'))
    show_in_stations = json.loads(request.GET.get('stations', 'true'))

    where = []
    if not show_in_space:
        where.append('"stationID" < %d' % constants.MAX_STATION_ID)
    if not show_in_stations:
        where.append('"stationID" > %d' % constants.MAX_STATION_ID)
    if divisions is not None:
        where.append('"hangarID" IN (%s)' % ', '.join(['%s'] * len(divisions)))

    sql = 'SELECT "solarSystemID", COUNT(*) AS "items", SUM("volume") AS "volume" '\
          'FROM "assets_asset" '
    if where: sql += ' WHERE ' + ' AND '.join(where)
    sql += ' GROUP BY "solarSystemID";'
    sql = db.fix_mysql_quotes(sql)

    cursor = connection.cursor()  #@UndefinedVariable
    if divisions is None:
        cursor.execute(sql)
    else:
        cursor.execute(sql, divisions)

    exact_volumes = Setting.get('assets_show_exact_volumes')

    jstree_data = []
    for solarSystemID, items, volume in cursor:
        try:
            system = CelestialObject.objects.get(itemID=solarSystemID)
        except CelestialObject.DoesNotExist:
            system = CelestialObject(itemID=solarSystemID,
                                     itemName=str(solarSystemID),
                                     security=0)
        if system.security > 0.5:
            color = 'hisec'
        elif system.security > 0:
            color = 'lowsec'
        else:
            color = 'nullsec'

        if exact_volumes:
            volume = print_float(volume)
        else:
            volume = round_quantity(volume)

        jstree_data.append({
            'data':
            HTML_ITEM_SPAN %
            (system.itemName, items, pluralize(items), volume),
            'attr': {
                'id': '%d_' % solarSystemID,
                'rel': 'system',
                'sort_key': system.itemName.lower(),
                'class': 'system-%s-row' % color
            },
            'state':
            'closed'
        })
    cursor.close()
    return HttpResponse(json.dumps(jstree_data))
Exemplo n.º 35
0
            SharedData.objects.filter(url=url).update(handler=handler)

# The creation of the declared objects is delayed here.
# If not, it would crash at first try of synchronizing the db
# as tables are not created yet.
for app in apps.LIST:
    create_app_objects(app)
for plugin in plugins.LIST:
    create_app_objects(plugin)

# When the server is shutdown while a scheduled task is running, it gets frozen
# and cannot be triggered anymore.
# The first time this module is imported, we reset all the scheduled tasks to
# is_running = False to avoid this problem.
ScheduledTask.objects.all().update(is_running=False, is_scheduled=False)

#------------------------------------------------------------------------------
admin_username = Setting.get('common_admin_username')
if not User.objects.filter(username=admin_username):
    try:
        logger.info('superuser "%s" does not exists. Creating...' % admin_username)
        User.objects.create_superuser(username=admin_username, email='', password='******')
    except:
        logger.exception("")
        raise
#-----------------------------------------------------------------------------
def import_monkey_patches():
    import ecm.lib.templatepatch #@UnusedImport for multi line template tags
    from . import template_filters #@UnusedImport to register template tags/filters
import_monkey_patches()
Exemplo n.º 36
0
def get_recruiters_group():
    return get_or_create_group(Setting.get('hr_recruiters_group_name'))
Exemplo n.º 37
0
def get_directors_group():
    return get_or_create_group(Setting.get('hr_directors_group_name'))
Exemplo n.º 38
0
def get_allies_plus_10_group():
    return get_or_create_group(Setting.get('hr_allies_plus_10_group_name'))
Exemplo n.º 39
0
def get_recruiters_group():
    return get_or_create_group(Setting.get('hr_recruiters_group_name'))
Exemplo n.º 40
0
def details(request, characterID):
    avg_session = {
      'sessionlength': 0,
      '30days': 0,
      '7days': 0,
    }
    now = timezone.now()
    try:
        member = Member.objects.get(characterID=int(characterID))
        try:
            member.base = CelestialObject.objects.get(itemID=member.baseID).itemName
        except CelestialObject.DoesNotExist:
            member.base = str(member.baseID)

        member.color = ColorThreshold.get_access_color(member.accessLvl)
        member.roles_no_director = member.roles.exclude(roleID=1) # exclude 'director'

        query = MemberSession.objects.filter(character_id=member.characterID).order_by('session_begin')
        query_30 = query.filter(session_begin__gt=now - timedelta(30))
        query_7 = query.filter(session_begin__gt=now - timedelta(7))

        session_len = query.aggregate(len=Avg('session_seconds'))['len'] or 0
        session_len_30 = query_30.aggregate(len=Avg('session_seconds'))['len'] or 0
        session_len_7 = query_7.aggregate(len=Avg('session_seconds'))['len'] or 0

        # Totals
        total = query.aggregate(len=Sum('session_seconds'))['len'] or 0
        lastWeek = query_7.aggregate(len=Sum('session_seconds'))['len'] or 0
        lastMonth = query_30.aggregate(len=Sum('session_seconds'))['len'] or 0


        loginhistory = query.order_by('-session_begin')[:10]

        avg_session['sessionlength'] = timedelta(seconds=session_len)
        avg_session['30days'] = timedelta(seconds=session_len_30)
        avg_session['7days'] = timedelta(seconds=session_len_7)

        if member.corp_id == Corporation.objects.mine().corporationID:
            member.date = UpdateDate.get_latest(Member)
        else:
            try:
                d = MemberDiff.objects.filter(member=member, new=False).order_by("-id")[0]
                member.date = d.date
            except IndexError:
                member.date = 0
        skills, skill_count, skillpoint_count = get_skills(member)
    except Member.DoesNotExist:
        member = Member(characterID=int(characterID), name="???")
    
    try:
        killboardUrl = Setting.get('corp_killboard_url')
    except Setting.DoesNotExist:
        killboardUrl = None
    
    data = {
        'member'             : member,
        'killboardUrl'       : killboardUrl,
        'sessiondata'        : avg_session,
        'lastWeek'           : lastWeek,
        'lastMonth'          : lastMonth,
        'total'              : total,
        'logins'             : loginhistory,
        'skills_tree'        : json.dumps(skills),
        'skill_count'        : skill_count,
        'skillpoint_count'   : print_integer(skillpoint_count),
        'datatables_defaults': DATATABLES_DEFAULTS,
        'access_columns'     : ACCESS_CHANGES_COLUMNS,
        'sorting'            : [[2, 'desc']],
    }
    return render_to_response("ecm/hr/members/details.html", data, Ctx(request))
Exemplo n.º 41
0
from django.http import HttpResponse, HttpResponseBadRequest, Http404
from django.utils import timezone

from ecm import apps, plugins
from ecm.apps.scheduler import process
from ecm.apps.common.models import Setting
from ecm.utils.format import print_time_min
from ecm.views import extract_datatable_params, datatable_ajax_data
from ecm.views.decorators import check_user_access, basic_auth_required
from ecm.apps.scheduler.models import ScheduledTask


LOG = logging.getLogger(__name__)

#------------------------------------------------------------------------------
@basic_auth_required(username=Setting.get('common_cron_username'))
def trigger_scheduler(request):
    now = timezone.now()
    tasks_to_execute = ScheduledTask.objects.filter(is_active=True,
                                                    is_running=False,
                                                    is_scheduled=False,
                                                    next_execution__lt=now).order_by("-priority")
    if tasks_to_execute:
        tasks_list = list(tasks_to_execute)
        tasks_to_execute.update(is_scheduled=True)
        
        process.run_async(*tasks_list)
        
        return HttpResponse(status=http.ACCEPTED)
    else:
        return HttpResponse(status=http.NOT_MODIFIED)