示例#1
0
    def post(self):
	try:
	    vs = self.body_json()
	    source, bid, listing, rating, text = \
		    vs['source'], vs['bid'], vs['listing'], int(vs['rating']), vs['text'][0:400]
	    user = users.get_current_user()
	    bid = Bid.get(bid)
	    listing = Listing.get(listing)
	    fb = Feedback.get_by_source(bid, listing, user_steam_id(user))
	    if rating > 100 or rating < -100:
		raise TypeError('Invalid feedback rating')
	    if source == 'lister':
		## lister is adding feedback for bidder; target is bid owner
		target = bid.owner
	    elif source == 'bidder':
		## bidder is adding feedback for lister; target is listing owner
		target = listing.owner
	    else:
		raise TypeError('Invalid feedback source')
	    source = user_steam_id(user)
            if fb:
		#raise TypeError('Feedback exists')
                fb.rating = rating
                fb.comment = text
                fb.put()
            else:
                fb = Feedback.build(bid, listing, source, target, rating, text)
	except (Exception, ), exc:
	    self.error(500)
	    raise
	    exc = exc.message if hasattr(exc, 'message') else str(exc)
	    result = {'msg':'error', 'description':exc}
示例#2
0
    def post(self):
        try:
            post = self.body_json()
            listing_id = int(post["id"])
            listing = Listing.get_by_id(listing_id)
            if not listing:
                self.error(404)
                return
            current_user = users.get_current_user()
            if current_user and (listing.owner == user_steam_id(current_user.nickname())):
                db_result = listing.cancel("Cancelled by user.")
            else:
                import logging

                logging.warn(
                    "wtf %s %s %s",
                    user_steam_id(current_user.nickname()),
                    listing.owner,
                    current_user.nickname() == listing.owner,
                )
                self.error(401)
                return
            result = {"msg": "okay", "result": db_result}
        except (Exception,), exc:
            self.error(500)
            exc = exc.message if hasattr(exc, "message") else str(exc)
            result = {"msg": "error", "description": exc}
示例#3
0
    def build(cls, **kwds):
	owner = users.get_current_user()
	if not owner:
	    raise ValueError('No owner specified.')
	kwds['owner'] = user_steam_id(owner)

	if not cls.lazy_verify:
	    kwds['profile'] = profile = PlayerProfile.get_by_user(owner)
	    profile.refresh()
	else:
	    kwds['profile'] = None

	verify_items_inactive(uid for uid, item in kwds['item_ids'])
	listing_id = int(kwds.pop('listing_id'))
	listing = kwds['listing'] = Listing.get_by_id(listing_id)

	## TODO: check for existing bid by owner and disallow creating
	## multiple bids.
	if not listing:
	    raise TypeError('Invalid listing.')
	if listing.status != 'active':
	    raise TypeError('Invalid listing status.')
	key = db.run_in_transaction(cls.build_transaction, **kwds)
    	listing.bid_count += 1
        listing.update_top(put=False)
	listing.put()
	return key
示例#4
0
    def build(cls, source, target, message):
	if cls.is_full(target):
	    raise Exception('Mailbox full')
	source = user_steam_id(source) if hasattr(source, 'nickname') else source
	## verify source + target
	msg = cls(source=source, target=target, message=message[0:400])
	msg.put()
	inc(message_counter_cache_key(msg))
	return msg
示例#5
0
    def build_update(cls, **kwds):
	owner = users.get_current_user()
	if not owner:
	    raise ValueError('No owner specified.')
	listing_id = int(kwds.pop('listing_id'))
	listing = kwds['listing'] = Listing.get_by_id(listing_id)
	if not listing:
	    raise TypeError('Invalid listing.')
	if listing.status != 'active':
	    raise TypeError('Invalid listing status.')
	bid = kwds['bid'] = cls.all().filter('owner =', user_steam_id(owner)).filter('listing =', listing).get()
	if not bid:
	    raise ValueError('No existing bid to update.')
	kwds['owner'] = user_steam_id(owner)
	kwds['profile'] = PlayerProfile.get_by_user(owner)
	verify_items_inactive(uid for uid, item in kwds['item_ids'])
	key = db.run_in_transaction(cls.build_update_transaction, **kwds)
        listing.update_top(put=True)
	return key
示例#6
0
    def build(cls, **kwds):
	## 0.  verify the owner, duration and description before
	## we do anything else (to prevent needless expensive checks).
        ## like the listing item, we need to get the profile before
	## we run the transaction.
	owner = users.get_current_user()
	if not owner:
	    raise ValueError('No owner specified.')
	kwds['owner'] = id64 = user_steam_id(owner)
	if not cls.lazy_verify:
	    kwds['profile'] = profile = PlayerProfile.get_by_user(user_steam_id(owner))
	    profile.refresh()
	else:
	    kwds['profile'] = None
	kwds['is_subscriber'] = PlayerProfile.is_subscriber_id64(id64)

	## this check has to be performed outside of the transaction
	## because its against items outside the new listing ancestry.
	## this whole check should move to a
	## "maybe-cancel-invalid-new-listing" queue:
	verify_items_inactive(uid for uid, item in kwds['item_ids'])
	return db.run_in_transaction(cls.build_transaction, **kwds)
示例#7
0
    def post(self):
	try:
	    key = self.body_json()['key']
	    bid = Bid.get(key)
	    if not bid:
		self.error(404)
	    elif user_steam_id(users.get_current_user()) != bid.owner:
		self.error(401)
	    else:
		bid.cancel()
	except (Exception, ), exc:
	    self.error(500)
            raise
	    error('cancel bid exception: %s', exc)
示例#8
0
    def build(cls, owner, id64=None):
	""" Returns the PlayerProfile for the given user, creating it if necessary. """
	if id64 is None:
	    id64 = user_steam_id(owner)
	if not id64:
	    return
	profile = cls.all().filter('owner =', id64).get()
	if profile is None:
	    profile = cls(owner=id64)
	    profile.put()
	    taskqueue.add(
		url='/api/v1/admin/queue/bang-counters',
		queue_name='counters',
		params={'players':1})
	return profile
示例#9
0
    def reverify(self, cls, key):
	""" Verify item ownership against the owner's backpack.

	This method will return the object if ownership is correct,
	otherwise it will cancel the listing or bid.
	"""
	kind = cls.__name__.lower()
	obj = cls.get(key)
	info('re-verify items in %s: %s', kind, key)
	items = obj.items()
	profile = PlayerProfile.get_by_user(user_steam_id(obj.owner))
	profile.refresh()
	#warn('re-verify items: %s', [long(i.uniqueid) for i in items])
	if profile.owns_all([long(i.uniqueid) for i in items]):
	    return obj
	else:
	    obj.cancel('Item ownership changed (%s)' % (kind, ))
示例#10
0
    def encode_builtin(self, listing=True, private=False):
	lfb = Feedback.get_by_source(self, self.listing, self.listing.owner)
	if not private:
	    user = users.get_current_user()
	    if user and user_steam_id(user) == self.owner:
		private = True
	return {
	    'owner' : self.owner_profile().encode_builtin(),
	    'created' : js_datetime(self.created),
	    'message_public' : self.message_public,
	    'message_private' : self.message_private if private else None,
	    'status' : self.status,
	    'items' : [i.encode_builtin() for i in self.items()],
	    'listing' : self.listing.encode_builtin(bids=False, items=False) if listing else None,
	    'key' : str(self.key()),
	    'feedback': lfb.encode_builtin() if lfb else None,
            'currency_val' : self.currency_val
	    }
示例#11
0
    def post(self):
	try:
	    post = self.body_json()
	    listing_id = int(post['id'])
	    listing = Listing.get_by_id(listing_id)
	    if not listing:
		self.error(404)
		return
	    bid = post['bid']
	    current_user = users.get_current_user()
	    if current_user and (listing.owner == user_steam_id(current_user.nickname())):
		db_result = listing.winner(bid)
	    else:
		self.error(401)
		return
	    result = {'msg':'okay', 'result':db_result}
	except (Exception, ), exc:
	    self.error(500)
	    exc = exc.message if hasattr(exc, 'message') else str(exc)
	    result = {'msg':'error', 'description':exc}
示例#12
0
    def encode_builtin(self, bids=False, items=True, feedback=True, currency_type_map=dict(currency_types())):
	""" Encode this instance using only built-in types.

	"""
	key = self.key()
	bids = self.bids() if bids else ()
	wins = [b for b in bids if b.status == 'awarded']
        bfb = Feedback.get_by_listing(self) if feedback else ()
	user = users.get_current_user()
	private = False
	if bids and user and user_steam_id(user) == self.owner:
	    private = True
        try:
            currency_type = currency_type_map[self.bid_currency_type]
        except (KeyError, ):
            currency_type = None
	return {
	    'id' : key.id(),
	    'key': str(key),
	    'owner' : PlayerProfile.get_by_user(self.owner).encode_builtin(subscription=False),
	    'created' : js_datetime(self.created),
	    'expires' : js_datetime(self.expires),
	    'description' : self.description,
	    'bid_count' : self.bid_count,
	    'min_bid' : self.min_bid,
	    'items' : [i.encode_builtin() for i in self.items()] if items else (),
	    'status' : self.status,
	    'status_reason' : self.status_reason,
	    'bids' : [b.encode_builtin(listing=False, private=private) for b in bids],
	    'feedback' : [fb.encode_builtin() for fb in bfb],
	    'featured' : self.featured,
	    'bid_currency_use' : self.bid_currency_use,
	    'bid_currency_start' : self.bid_currency_start,
	    'bid_currency_type' : currency_type,
            'bid_currency_top' : self.bid_currency_top,
	}
示例#13
0
 def build(cls, owner):
     key = user_steam_id(owner) if hasattr(owner, "nickname") else owner
     obj = cls.get_or_insert(key)
     if not obj.is_saved():
         obj.put()
     return obj
示例#14
0
    def is_full(cls, target, limit=100):
	target = user_steam_id(target) if hasattr(target, 'nickname') else target
	count = cls.all(keys_only=True).filter('target =', target).count()
	return count >= limit
示例#15
0
    def get_for_user(cls, user, limit=100):
	user = user_steam_id(user) if hasattr(user, 'nickname') else user
	return cls.all().filter('target =', user).order('-created').fetch(limit)
示例#16
0
    def remove(cls, key, target):
	key = db.Key(key)
	target = user_steam_id(target) if hasattr(target, 'nickname') else target
	msg = PlayerMessage.all().filter('__key__ =', key).filter('target =', target).get()
	msg.delete()
	dec(message_counter_cache_key(msg))
示例#17
0
    def get_by_user(cls, user):
	""" Returns the model instance for the given user. """
	id64 = user_steam_id(user) if hasattr(user, 'nickname') else user
	return cls.get_by_id64(id64)
示例#18
0
    def get_by_user(cls, user):
	""" Returns the PlayerProfile for the given user. """
	return cls.get_by_id64(user_steam_id(user) if hasattr(user, 'nickname') else user)