Example #1
0
def marketorders_view(request):
    lu = LastUpdate.objects.get(name="marketorders").timestamp
    order_set = MarketOrder.objects.all()
    wanted_set = WantedMarketOrder.objects.all()
    pilot_name = None
    if request.GET.get("all") is None:
        pilot_name = request.user.profile.name
        order_set = order_set.filter(characterid=request.user.profile.characterid)
        wanted_set = wanted_set.filter(characterid=request.user.profile.characterid)
    order_dict = {}
    for order in order_set:
        order.station = get_itemname(order.stationid)
        order.typename = get_typename(order.typeid)
        ordertype = order.ordertype
        order_dict.setdefault(order.station, {})
        order_dict[order.station].setdefault(ordertype, [])
        order_dict[order.station][ordertype].append(order)
        if pilot_name is None:
            order.pilotname = get_membername(order.characterid)
        if request.GET.get("all") is None:
            equal = MarketOrder.objects.filter(
                stationid=order.stationid, typeid=order.typeid, ordertype=order.ordertype
            )
            equal = equal.exclude(characterid=request.user.profile.characterid)
            order.othercorp = [get_membername(other.characterid) for other in equal]
    for wanted in wanted_set:
        station = get_itemname(wanted.stationid)
        typename = get_typename(wanted.typeid)
        ordertype = wanted.ordertype
        order_dict.setdefault(station, {})
        order_dict[station].setdefault(ordertype, [])
        if wanted.typeid not in [order.typeid for order in order_dict[station][ordertype]]:
            try:
                pl = PriceList.objects.get(typeid=wanted.typeid)
            except PriceList.DoesNotExist:
                productioncost = 0.0
            else:
                productioncost = pl.productioncost * pl.safetymargin

            price = last_price(ordertype, wanted.stationid, wanted.typeid)
            if ordertype == "sell":
                if price == 0:
                    profitmargin = 0
                else:
                    profitmargin = 1 - (productioncost / price)
                profitperitem = price - productioncost
            else:
                if price == 0:
                    profitmargin = 0
                else:
                    profitmargin = (productioncost - price) / price
                profitperitem = productioncost - price
            order = KeyValue(
                stationid=wanted.stationid,
                stationname=station,
                typeid=wanted.typeid,
                typename=typename,
                ordertype=ordertype,
                expires=datetime.datetime(2000, 1, 1),
                volremaining=0,
                price=price,
                productioncost=productioncost,
                salesperday=0.0,
                trend=0.0,
                expiredays=0,
                profitmargin=profitmargin,
                profitperitem=profitperitem,
                daysremaining=0,
                profitperday=0,
            )
            order_dict[station][ordertype].append(order)
            if request.GET.get("all") is None:
                equal = MarketOrder.objects.filter(
                    stationid=order.stationid, typeid=order.typeid, ordertype=order.ordertype
                )
                equal = equal.exclude(characterid=request.user.profile.characterid)
                order.othercorp = [get_membername(other.characterid) for other in equal]

    order_list = []
    for station, otypes in order_dict.items():
        newotypes = {}
        for ordertype, orders in otypes.items():
            newotypes[ordertype] = sorted(orders, key=lambda o: o.typename)
        order_list.append((station, newotypes))
    return direct_to_template(
        request,
        "industry/marketorders.html",
        extra_context={"pilot_name": pilot_name, "order_list": order_list, "lastupdate": lu},
    )
Example #2
0
def add_transaction_info(grd):
    api = apiroot()
    standingmap = dict((row.contactID, row.standing)
                       for row in grd.ContactList().allianceContactList)
    for entry in Transaction.objects.filter(info=None):
        info = TransactionInfo()
        info.transaction = entry
        info.account = Account.objects.get(accountkey=entry.accountkey)
        info.typename = get_typename(entry.typeid)
        if info.typename is None:
            continue
        try:
            pl = PriceList.objects.get(typeid=entry.typeid)
            info.cost = pl.productioncost
            info.safetymargin = pl.safetymargin
        except PriceList.DoesNotExist:
            try:
                index = Index.objects.filter(typeid=entry.typeid)[0:1].get()
                info.cost = index.republic
                info.safetymargin = 1.0
            except Index.DoesNotExist:
                info.cost = 0.0
                info.safetymargin = 1.1
        info.stationname = get_itemname(entry.stationid)
        if entry.characterid is not None:
            info.charactername = get_membername(entry.characterid)
        try:
            charinfo = api.eve.CharacterInfo(characterID=entry.clientid)
        except:
            pass
        else:
            info.clientname = charinfo.characterName
            info.clientstanding = standingmap.get(charinfo.characterID, None)
            info.clientcorp = charinfo.corporation
            info.clientcorpid = charinfo.corporationID
            info.clientcorpstanding = standingmap.get(charinfo.corporationID,
                                                      None)
            if hasattr(charinfo, 'allianceID'):
                info.clientalliance = charinfo.alliance
                info.clientallianceid = charinfo.allianceID
                info.clientalliancestanding = standingmap.get(
                    charinfo.allianceID,
                    None)
            info.save()
            transaction.commit()
            continue
        # It's a CorporationID!
        info.clientname = None
        info.clientstanding = None
        info.clientcorpid = entry.clientid
        name = get_itemname(entry.clientid)
        if name is not None: # NPC corp
            info.clientcorp = name
            info.clientcorpstanding = standingmap.get(entry.clientid, None)
            info.save()
            transaction.commit()
            continue
        # Player corp
        try:
            corpinfo = api.eve.CorporationSheet(corporationID=entry.clientid)
        except: # Something bad happened, ignore this
            transaction.rollback()
            continue
        info.clientcorp = corpinfo.corporationName
        info.clientcorpstanding = standingmap.get(corpinfo.corporationID,
                                                  None)
        if hasattr(corpinfo, 'allianceID'):
            info.clientalliance = corpinfo.allianceName
            info.clientallianceid = corpinfo.allianceID
            info.clientalliancestanding = standingmap.get(corpinfo.allianceID,
                                                          None)
        info.save()
        transaction.commit()