def new_listing_image(listing_id): # user_id = request.form['user_id'] # listing_id = request.form['listing_id'] userfile = request.files['userfile'] filename = userfile.filename # Check if listing exists l = Listing.get_by_id(listing_id) if l is None: raise InvalidUsage('Listing does not exist!', status_code=400) # Create client for interfacing with Cloud Storage API client = storage.Client() bucket = client.get_bucket(global_vars.LISTING_IMG_BUCKET) # Calculating size this way is not very efficient. Is there another way? userfile.seek(0, 2) size = userfile.tell() userfile.seek(0) # upload the item image path = str(listing_id) + '/' + filename image = bucket.blob(blob_name=path) image.upload_from_file(file_obj=userfile, size=size, content_type='image/jpeg') # Hacky way of making the image public.. image.acl.all().grant_read() image.acl.save() resp = jsonify({'image_path': path, 'image_media_link': image.media_link}) resp.status_code = 201 return resp
def import_listings(): listings = pd.read_csv('../../data/listings/nyc_listings.csv') failed_import = [] for idx, listing in listings.iterrows(): db_item = Listing(airBnbId = listing['id'], url = listing['listing_url'], name = listing['name'] , picture_url = listing['picture_url'], host_url = listing['host_url'], host_name = listing['host_name'], latitude = listing['latitude'], longitude = listing['longitude'], room_type = listing['room_type'], price = listing['price'][1:], minimum_nights = listing['minimum_nights'], number_of_reviews = listing['number_of_reviews'], review_scores_rating = listing['review_scores_rating']) try: db_item.save() except: failed_import.append(listing['id']) with open('../../data/listings/failed_import.json', 'w') as outfile: json.dump(failed_import, outfile)
def __init__(self, db): self._db = db self.occupancy = {} self.counter = 0 listings = Listing.get_all(self._db) for l in listings: self.occupancy[l.id] = {}
def ParseAirplaneMartSummary(url): logger.info('Scraping Airplane Mart Summary: {}'.format(url)) load_time = time.time() request = urllib.request.Request(url, headers=_HEADERS) soup = bs4.BeautifulSoup(urllib.request.urlopen(request), 'lxml') links = soup.find_all( 'a', href=re.compile(r'/aircraft-for-sale/Single-Engine-Piston/')) logger.info('Found {:d} Airplane Mart listings.'.format(len(links))) new_listings = [] for link in links: while time.time() < load_time + _REQ_DELAY: time.sleep(0.1) listing_url = urllib.parse.urljoin(url, link['href']) try: Listing.get(Listing.url == listing_url) logger.info('Skipping {}.'.format(link.find('b').string.strip())) continue except peewee.DoesNotExist: logger.info('Opening {}.'.format(link.find('b').string.strip())) load_time = time.time() listing = ParseAirplaneMartListing(listing_url) if not listing: continue if listing.registration: try: Listing.get(Listing.registration == listing.registration) logger.info('Duplicate Registration {}: {}.'.format( listing.registration, link.find('b').string.strip())) continue except peewee.DoesNotExist: pass listing.save() new_listings.append(listing) return new_listings
def ParseTradeAPlaneSummary(url): logger.info('Scraping Trade-A-Plane summary: {}'.format(url)) load_time = time.time() request = urllib.request.Request(url, headers=_HEADERS) soup = bs4.BeautifulSoup(urllib.request.urlopen(request), 'lxml') result_divs = soup.find_all('div', class_='result') logger.info('Found {:d} Trade-A-Plane listings.'.format(len(result_divs))) new_listings = [] for div in result_divs: while time.time() < load_time + _REQ_DELAY: time.sleep(0.1) listing_url = urllib.parse.urljoin(url, div.a['href']) try: Listing.get(Listing.url == listing_url) logger.info('Skipping {}.'.format(div.a.next_element.strip())) continue except peewee.DoesNotExist: logger.info('Opening {}.'.format(div.a.next_element.strip())) load_time = time.time() listing = ParseTradeAPlaneListing(listing_url) if not listing: continue if listing.registration: try: Listing.get(Listing.registration == listing.registration) logger.info('Duplicate Registration {}: {}.'.format( listing.registration, div.a.next_element.strip())) continue except peewee.DoesNotExist: pass listing.save() new_listings.append(listing) return new_listings
def ParseAsoSummary(url): logger.info('Scraping ASO Summary: {}'.format(url)) load_time = time.time() request = urllib.request.Request(url, headers=_HEADERS) soup = bs4.BeautifulSoup(urllib.request.urlopen(request), 'lxml') links = soup.find_all('a', class_='photoListingsDescription') links = [x for x in links if not x.find('img')] logger.info('Found {:d} ASO listings.'.format(len(links))) new_listings = [] for link in links: while time.time() < load_time + _REQ_DELAY: time.sleep(0.1) listing_url = urllib.parse.urljoin(url, link['href']) try: Listing.get(Listing.url == listing_url) logger.info('Skipping {}.'.format(link.string.strip())) continue except peewee.DoesNotExist: logger.info('Opening {}.'.format(link.string.strip())) load_time = time.time() listing = ParseAsoListing(listing_url) if not listing: continue if listing.registration: try: Listing.get(Listing.registration == listing.registration) logger.info('Duplicate Registration {}: {}.'.format( listing.registration, link.string.strip())) continue except peewee.DoesNotExist: pass listing.save() new_listings.append(listing) return new_listings
def by_comp(comp_to_pull=None): ## display only selected company dates = Listing.objects().fields(date_str=1).distinct('date_str') return render_template( 'index-by-comp.html', Listing=Listing, comp_to_pull=comp_to_pull, dates=reversed(sorted(list(dates))) #latest date on top )
def by_location(loc_to_pull=None): ## display only selected location dates = Listing.objects().fields(date_str=1).distinct('date_str') return render_template( 'index-by-loc.html', Listing=Listing, loc_to_pull=loc_to_pull, dates=reversed(sorted(list(dates))) #latest date on top )
def run_scraper(mls=INITIAL_MLS): misses = 0 foundHit = False while (MISS_THRESHOLD > misses): try: existing_listing = Listing.get_listing(mls) listing = scrape_listing(mls) print 'Fetching Listing ID %s' % mls print 'Have existing listing? %s ' % (existing_listing is not None) if existing_listing: existing_listing.is_new = False if int(existing_listing.price) != listing.price: existing_listing.price = listing.price existing_listing.is_updated = True else: existing_listing.is_updated = False existing_listing.save() else: if fits_criteria(listing): print '%sHIT%s' % (bcolors.OKGREEN, bcolors.ENDC) foundHit = True listing.is_new = True listing.save() else: print '%sMISS. Count: %s%s' % (bcolors.WARNING, misses, bcolors.ENDC) mls = mls + 1 misses = 0 except ListingNotFound: existing_listing = Listing.get_listing(mls) mls = mls + 1 misses = misses + 1 if existing_listing: existing_listing.active = False existing_listing.last_scraped = datetime.now() existing_listing.save() else: print 'misses are %s ' % misses if foundHit: print '%sFound a Hit!%s' % (bcolors.HEADER, bcolors.ENDC) else: print '%sNo Hits this time.%s' % (bcolors.FAIL, bcolors.ENDC)
def index(): db = get_db() ## get the last date the webscraper was run for listing in Listing.objects().fields( date_str=1).order_by('-date_str').limit(1): day_to_pull = listing.date_str return render_template('index.html', Listing=Listing, day_to_pull=day_to_pull)
def get(self): id = int(self.request.get('id')) item = Listing.get(db.Key.from_path('Listing', id)) self.response.out.write('<html><body>' '<form method="POST" ' 'action="/edit">' '<table>') self.response.out.write(ListingForm(instance=item)) self.response.out.write('</table>' '<input type="hidden" name="id" value="%s">' '<input type="submit">' '</form></body></html>' % id)
def add_listing(asin, listing={}): # We may need to add a listing with just an asin. listing_exists = dbapi.listing_exists(asin) if not (listing_exists or listing): dbapi.add(Listing(asin=asin)) elif not listing_exists: try: fprice = float(listing['lowest_price']) except: fprice = None try: l = Listing(asin=asin, manufacturer=listing['manufacturer'], title=listing['title'], part_number=listing['part_number'], price=fprice, upc=listing['upc']) dbapi.add(l) except: print('Unable to add listing to db') else: print('{0} already exists in db'.format(asin))
def listings(setting = "aaa"): allListingsQuery = Listing.query.filter_by(active = True) if "b" in setting: allListingsQuery = allListingsQuery.filter_by(buysell = True) if "s" in setting: allListingsQuery = allListingsQuery.filter_by(buysell = False) if "l" in setting: allListingsQuery = allListingsQuery.filter_by(blockOrDinex = "Block") if "d" in setting: allListingsQuery = allListingsQuery.filter_by(blockOrDinex = "Dinex") if "u" in setting: allListingsQuery = allListingsQuery.order_by(Listing.price) if "g" in setting: allListingsQuery = allListingsQuery.order_by(desc(Listing.price)) allListings = allListingsQuery.all() form = ListingForm() if(form.validate_on_submit()): newList = Listing(blockOrDinex = form.blockOrDinex.data, timestamp = datetime.now(), price = float(form.price.data), details = form.details.data, location = form.location.data, active = True) if form.buysell.data == 'Buy': newList.buysell = True else: newList.buysell = False newList.user = g.user db.session.add(newList) db.session.commit() if newList.buysell == True: alertuserstup = db.session.query(User.email).filter_by(buyAlert=True).all() else: alertuserstup = db.session.query(User.email).filter_by(sellAlert=True).all() alertusers = [x[0] for x in alertuserstup] alertusers = filter (lambda x: ('@' in x) and ('.' in x), alertusers) if (len(alertusers) != 0): msg = Message('New Post Alert on Dining Exchange',sender='*****@*****.**',recipients=alertusers) msg.body = "Hello,\n\n A listing for which you are receiving alerts has been posted to CMU Dining Exchange." msg.body += "\n\nThe listing is visible at " + "http://127.0.0.1:5000" + url_for('listings') mail.send(msg) return redirect(url_for('listings')) return render_template("listings.html",title ='Listings',form = form,lists=allListings, user=g.user,set = setting, updateFunc = update)
def create(args): try: prof = Profile.objects.get(email=args['owner_email']) except Profile.DoesNotExist: return False, ERROR_NO_SUCH_PROFILE if not profile.check_password(args['password'], prof): return False, ERROR_INCORRECT_PASSWORD dt = datetime.datetime.now() time_created = dt.strftime('%m-%d-%Y %I:%M%p') encode = '%s%s%s' % (args['job_title'], args['address'], dt) listing_id = base64.b64encode(encode, '-_') neg = prof.negative_reputation pos = prof.positive_reputation rep = (pos / (pos + neg)) * 100 if pos + neg != 0 else 0 owner_name = '%s %s' % (prof.first_name, prof.last_name) # Check to see if optional params were given upon creation expiration_date = args['expiration_date'].replace('/', '-') if 'expiration_date' in args else None min_reputation = args['min_reputation'] if 'min_reputation' in args else None job_description = args['job_description'] if 'job_description' in args else None # End optional settings listing = Listing(job_title=args['job_title'], job_picture='[]', starting_amount=args['starting_amount'], current_bid=args['starting_amount'], min_reputation=min_reputation, expiration_date=expiration_date, owner_name=owner_name, owner_email=args['owner_email'], listing_id=listing_id, time_created=time_created, owner_reputation=rep, status=0, bids='[]', address=args['address'], city=args['city'], state=args['state'], lat=args['latitude'], long=args['longitude'], job_description=job_description) listing.save() owned_listings = json.loads(prof.owned_listings) owned_listings.append(listing_id) prof.__dict__['owned_listings'] = json.dumps(owned_listings) prof.save() return { 'error': -1, 'listing_id' : listing_id }, None
def _create_listing(listing_data): listing = Listing() listing.build(listing_data) if 'id' in listing_data: listing.external_id = listing_data['id'] listing.put() return listing
def ListingDetail(listing_id): try: listing = Listing.get(id=listing_id) except peewee.DoesNotExist: abort(404) if request.method == 'POST': form = ListingForm(request.form, obj=listing) if form.validate(): form.populate_obj(listing) listing.save() else: form = ListingForm(obj=listing) return render_template('detail.html', form=form, listing=listing)
def import_listings(): listings = pd.read_csv('../../data/listings/nyc_listings.csv') failed_import = [] for idx, listing in listings.iterrows(): db_item = Listing(airBnbId=listing['id'], url=listing['listing_url'], name=listing['name'], picture_url=listing['picture_url'], host_url=listing['host_url'], host_name=listing['host_name'], latitude=listing['latitude'], longitude=listing['longitude'], room_type=listing['room_type'], price=listing['price'][1:], minimum_nights=listing['minimum_nights'], number_of_reviews=listing['number_of_reviews'], review_scores_rating=listing['review_scores_rating']) try: db_item.save() except: failed_import.append(listing['id']) with open('../../data/listings/failed_import.json', 'w') as outfile: json.dump(failed_import, outfile)
def get(self, currency): listings = Listing.select().where( Listing.currency == currency).order_by(Listing.date.asc()) value = 0 amount = 0 for listing in listings: if listing.type == 'buy': amount += listing.amount value += listing.price elif listing.type == 'sell': amount -= listing.amount value -= listing.price return dict((('value', float(value)), ('amount', float(amount))))
def create_listing(): is_valid = True date = datetime.strptime(request.form.get("datepicker"), '%Y-%m-%d %I:%M') price = request.form.get("price") driving_from = request.form.get("from") going_to = request.form.get("to") description = f"Driving from {driving_from} to {going_to}" new_listing = Listing(user_id=session['user_id'], date=date, price=price, location_from=driving_from, location_to=going_to, description=description) db.session.add(new_listing) db.session.commit() return redirect('/profile')
def private_listing(request,board_slug,category_slug,listing_slug=None): """Create or edit listing""" category = get_object_or_404(Category,slug=category_slug, is_active=True) board = get_object_or_404(Board,slug=board_slug, is_active=True) if listing_slug is not None: listing = get_object_or_404(Listing,slug=listing_slug, category=category, board=board, user=request.user) else: listing = Listing(category=category, board=board, user=request.user) if request.method == "POST": form = ListingForm(request.POST, instance=listing) if form.is_valid(): form.save() form.instance.create_slug() args = form.instance.board.slug, form.instance.category.slug, form.instance.slug return HttpResponseRedirect(reverse('public_listing', args=args))
def listing_create(): """ Create a new listing. Takes in { listing: { title, description, photo, price, longitude, latitude, beds, rooms, bathrooms, created_by, }} Returns => { listing: { id, title, description, photo, price, longitude, latitude, beds, rooms, bathrooms, created_by, rented_by, } } TODO: Auth required: admin or logged in user """ listing_data = request.json.get("listing") form = ListingCreateForm(data=listing_data) if form.validate(): listing = Listing.create(form) db.session.commit() # TODO: reevaluate error with a try and except later return (jsonify(listing=listing.serialize(isDetailed=True)), 201) else: errors = [] for field in form: for error in field.errors: errors.append(error) return (jsonify(errors=errors), 400)
def deactivate_user(user_id): # Edit Datastore entity # Get the user u = User.get_by_id(user_id) if u is None: raise InvalidUsage('User ID does not match any existing user', 400) if u.status == 'Deactivated': raise InvalidUsage('User is already deactivated!', 400) # Set user status to 'Deactivated' u.status = 'Deactivated' # Set all of the user's listings to 'Deactivated' u_key = ndb.Key('User', user_id) qry = Listing.query(Listing.owner == u_key) listings = qry.fetch() for l in listings: if l.status != 'Deleted': l.status = 'Deactivated' try: l.put() except: abort(500) # Add the updated user status to the Datastore try: u.put() except: abort(500) # Delete Search App entity try: index = search.Index(name='User') index.delete(str(user_id)) except: abort(500) # Return response data = { 'user_id deactivated': user_id, 'date_deactivated': u.date_last_modified } resp = jsonify(data) resp.status_code = 200 return resp
def get_listing_snapshot(listing_id): l = Listing.get_by_id(listing_id) if l is None: raise InvalidUsage('Listing does not exist!', status_code=400) listing_img_media_links = get_listing_images(listing_id) # Add function for related items? # Return the attributes of the new item # Figure out what we want to send in a snapshot.. listing_data = {'name':l.name,'rating':l.rating,'hourly_rate':l.hourly_rate, 'daily_rate':l.daily_rate,'weekly_rate':l.weekly_rate, 'image_media_links':listing_img_media_links} resp = jsonify(listing_data) resp.status_code = 200 return resp
def get_part_listings(part): print('fetching listings for part', part) listings = [] url = 'http://www.bricklink.com/search.asp' params = { 'viewFrom': 'sf', 'qMin': part.qty, 'colorID': part.color_id, 'shipCountryID': 'US', 'sellerCountryID': 'US', 'moneyTypeID': 1, 'w': part.element_id, 'sellerLoc': 'C', 'searchSort': 'P', 'sz': 500 } html = requests.get(url, headers=headers, params=params).text if 'Quota Exceeded' in html: raise QuotaExceededError() results = soup(html, 'html.parser').findAll('td', {'valign' : 'TOP'}) if len(results) == 0: raise PartNotFoundError(part) for r in results: try: link = r.find('a') price = r.findAll('b')[1].text price = float(price.replace('US $', '')) store_id = parse_qs(urlparse(link['href']).query)['p'][0] inventory_id = int(parse_qs(urlparse(link['href']).query)['itemID'][0]) if not price > 0: price = 0.01 print('price: fallback on price 0.01') if price is None or link['href'] is None or store_id is None: raise ListingParsingError() listing = Listing(part.element_id, part.color_id, part.qty, price, link.text, link['href'], store_id, inventory_id) if not listing.price > 0: raise PriceZeroError(listing) listings.append(listing) except KeyError as e: # Skip the malformed listing print('KeyError', e) print('Found {} listings'.format(len(listings))) return listings
def get_property_id(listing_id): """ by taking agent listing id,returing rex property id form listing address we are building rex compatable address, with that address we are searching for rex property, if property found, returning that property id else creating property and returing new property id """ listing = Listing.get(listing_id) rex_property_id = None try: full_address = listing.address.unit_number if full_address is None: full_address = "" if listing.address.address_low is not None: if listing.address.unit_number is not None: full_address += " /" + str(listing.address.address_low) else: full_address += str(listing.address.address_low) full_address += ( listing.address.street_name + " " + listing.address.suburb + " " + listing.address.state + " " + listing.address.postcode ) rex_property_id = p_api.get_property_id(full_address) except: traceback.print_exc() if rex_property_id is None: property = properties.Property( unit_number=listing.address.unit_number, street_number=listing.address.address_low, street_name=listing.address.street_name, suburb=listing.address.suburb, state=listing.address.state, postcode=listing.address.postcode, ) rex_property_id = p_api.create(property)["_id"] return rex_property_id
def get_matched_listings(user, radius_miles, results): matched_listings = [] for matched_listing in results: try: l = Listing.get_by_id(int(matched_listing.doc_id)) except: abort(500) if l is None: # If there is an inconsistency and item is not found in Datastore, raise some error here... # raise InvalidUsage('Inconsistency in databases. Item not found in Datastore.', status_code=400) continue # FIXME: send only what's needed.. distance = haversine(l.location.lat,l.location.lon,user.last_known_location.lat,user.last_known_location.lon) listing_data = {'listing_id':l.key.id(), 'name':l.name, 'rating':l.rating, 'daily_rate':l.daily_rate, 'distance':distance, 'score':item_score(distance, radius_miles, l.total_value, global_vars.MAX_PRICE, l.category, user.category_weights), 'image_media_links':get_listing_images(l.key.id())} matched_listings += [listing_data] return matched_listings
def check_market(): push_context() log.info('Checking market prices') psa = PixelStarshipsApi() market_data = psa.get_market_data() for k, v in market_data.items(): listing = Listing(id=k, sale_at=v['sale_at'], item_name=v['item_name'], item_id=v['item_id'], amount=v['amount'], currency=v['currency'], price=v['price'], user_id=v['user_id'], modification=v['modification']) db.session.merge(listing) log.info('{} listings updated'.format(len(market_data))) db.session.commit()
def post(self): id = int(self.request.get('id')) item = Listing.get(db.Key.from_path('Listing', id)) data = ListingForm(data=self.request.POST, instance=item) if data.is_valid(): # Save the data, and redirect to the view page entity = data.save(commit=False) entity.added_by = users.get_current_user() entity.put() self.redirect('/list') else: # Reprint the form self.response.out.write('<html><body>' '<form method="POST" ' 'action="/edit">' '<table>') self.response.out.write(data) self.response.out.write('</table>' '<input type="hidden" name="id" value="%s">' '<input type="submit">' '</form></body></html>' % id)
def post(self): id = int(self.request.get('id')) item = Listing.get(db.Key.from_path('Listing', id)) data = ListingForm(data=self.request.POST, instance=item) if data.is_valid(): # Save the data, and redirect to the view page entity = data.save(commit=False) entity.added_by = users.get_current_user() entity.put() self.redirect('/list') else: # Reprint the form self.response.out.write('<html><body>' '<form method="POST" ' 'action="/edit">' '<table>') self.response.out.write(data) self.response.out.write( '</table>' '<input type="hidden" name="id" value="%s">' '<input type="submit">' '</form></body></html>' % id)
def balance(self): print('----WALLET----') listing_currencies = Listing.select().group_by(Listing.currency) ids = '' for currency in listing_currencies: ids += '{},'.format(currency.currency) profit_loss = 0 total_wallet_value = 0 total_spent = 0 if len(listing_currencies) > 0: data = self.api.live(ids) for i in range(len(listing_currencies)): currency = listing_currencies[i] for item in data: if item['id'] == currency.currency: live_value = float(item['price']) balance = self.get(currency.currency) current_value = balance['amount'] * live_value diff = current_value - balance['value'] print('{}{} \t= ${} \tProfit/Loss: ${} \t\tSpent: ${}'. format(round(balance['amount'], 2), currency.currency, round(current_value, 6), round(diff, 6), round(balance['value'], 6))) profit_loss += diff total_wallet_value += current_value total_spent += balance['value'] print('----BALANCE----') print('Total wallet value: ${} \tSpent: ${}'.format( total_wallet_value, total_spent)) profit_loss_pct = total_spent / profit_loss print('Total profit/loss: %{} ==> ${}'.format(profit_loss_pct, profit_loss))
def list_book(): form = ListBook() msg = "" if request.method == "POST": isbn = clean_isbn(form.isbn.data) if valid_isbn(isbn): book = get_book_by_isbn(isbn) print book if book is not None: listing = Listing(user_id=g.user.id, listing_ref=book.id, course=form.course.data.replace(" ", "").upper(), notes=form.notes.data, cost=form.cost.data) db.session.add(listing) db.session.commit() msg = "Listing added successfully!" else: msg = "Book not found!" else: msg = "ISBN was not 10 or 13 characters long!" return render_template("list_book.html", form=form, msg=msg)
def perform(self, vendor_id=None, order_id=None, items=None): amazon = Vendor.query.filter_by(name='Amazon').one() vendor = Vendor.query.filter_by(id=vendor_id).one() for item_doc in items: sku = item_doc['sku'] amz_listing = Listing.query.filter_by(vendor_id=amazon.id, sku=sku).first() \ or Listing(vendor=amazon, sku=sku) item = OrderItem.query.filter( OrderItem.order_id == order_id, OrderItem.extra['order_item_id'].astext == item_doc['order_item_id']).first() or OrderItem( order_id=order_id) item.quantity = item_doc.pop('qty_shipped') item.received = item.quantity item.source = Inventory.query.filter_by(owner_id=vendor.id, listing_id=amz_listing.id).first() \ or Inventory(owner=vendor, listing=amz_listing) item.destination = None item.update(item_doc) db.session.add(item) db.session.commit()
def perform(self, listing=None): # Do some basic cleaning for field, value in listing.items(): if isinstance(value, str): listing[field] = value.strip() # Try to locate the listing in the database sku = listing.pop('sku') vendor_id = listing.pop('vendor_id', None) if vendor_id is None: netloc = urlparse(listing['detail_url'])[1] vendor = Vendor.query.filter(Vendor.url.ilike(f'%{netloc}%')).one() vendor_id = vendor.id model = Listing.query.filter_by(vendor_id=vendor_id, sku=sku).one_or_none()\ or Listing(vendor_id=vendor_id, sku=sku) # Update and commit model.update(listing) db.session.add(model) db.session.commit() return model.id
def updateDBTickets(eventId, listings): try: e = Event.objects.get(id=eventId) except Event.DoesNotExist: # User queried an event not in the database, probably search by modifying url logging.info('event ' + str(eventId) + ' not in db yet') if int(eventId) == 1: # for testing only e = Event(id=1) e.save() else: event = getEvent(eventId) logging.info(event) if not event.get('error'): e = storeEvent(event) else: return {'error': 'event does not exist'} oldListings = dict((l.id,l) for l in e.listing_set.all()) newListings = dict((l['listingId'], l) for l in listings) for lid in oldListings: oldListing = oldListings[lid] if lid not in newListings.keys(): # if the listing in database is not in the latest listings, then it is sold logging.info("All tickets in the listing " + str(lid) + " have been sold out!") if(oldListing.quantity > 0): # insert all tickets into SoldTicket seats = [seat.strip() for seat in oldListing.seatNumbers.split(',')] for seat in seats: t = SoldTicket(listingId=lid, event=e, zoneId=oldListing.zoneId, zoneName=oldListing.zoneName, sectionId=oldListing.sectionId, sectionName=oldListing.sectionName, row=oldListing.row, seatNumber=seat, currentPrice=oldListing.currentPrice) t.save() remaining = oldListing.quantity - len(seats) if remaining > 0: # mainly caused by seatNumbers == "General Admission" for i in xrange(0, remaining): t = SoldTicket(listingId=lid, event=e, zoneId=oldListing.zoneId, zoneName=oldListing.zoneName, sectionId=oldListing.sectionId, sectionName=oldListing.sectionName, row=oldListing.row, seatNumber=None, currentPrice=oldListing.currentPrice) t.save() # Delete the empty listing Listing.objects.filter(id=lid).delete() else: # the listing is still active, then check its quantity curListing = newListings[lid] if curListing['quantity'] < oldListing.quantity: # Some tickets are sold but not all of them # find out which seats were sold, update listing and insert into sold tickets oldSeats = [seat.strip() for seat in oldListing.seatNumbers.split(',')] newSeats = [seat.strip() for seat in curListing['seatNumbers'].split(',')] missingSeats = [seat for seat in oldSeats if seat not in newSeats] for seat in missingSeats: t = SoldTicket(listingId=lid, event=e, zoneId=oldListing.zoneId, zoneName=oldListing.zoneName, sectionId=oldListing.sectionId, sectionName=oldListing.sectionName, row=oldListing.row, seatNumber=seat, currentPrice=oldListing.currentPrice) t.save() remaining = oldListing.quantity - curListing['quantity'] - len(missingSeats) if remaining > 0: # mainly caused by seatNumbers == "General Admission" for i in xrange(0, remaining): t = SoldTicket(listingId=lid, event=e, zoneId=oldListing.zoneId, zoneName=oldListing.zoneName, sectionId=oldListing.sectionId, sectionName=oldListing.sectionName, row=oldListing.row, seatNumber=None, currentPrice=oldListing.currentPrice) t.save() oldListing.seatNumbers = curListing['seatNumbers'] oldListing.quantity = curListing['quantity'] oldListing.save() for lid in newListings: if lid not in oldListings.keys(): # Someone has posted a new listing that is not in the database, put it into database newListing = newListings[lid] l = Listing(id=newListing['listingId'], event=e, quantity=newListing['quantity'], zoneId=newListing['zoneId'], zoneName=newListing['zoneName'], sectionId=newListing['sectionId'], sectionName=newListing['sectionName'], row=newListing['row'], seatNumbers=newListing['seatNumbers'], currentPrice=newListing['currentPrice']['amount']) l.save()
def newListing(): categories = session.query(Category).all() if request.method == 'POST': if checkIfExists(request.form['name'],Listing): existingListing = session.query(Listing).filter_by(name=request.form['name']).one() flash('A listing called "%s" already exists. Please re-name your listing.' % existingListing.name) return render_template('newListing.html', categories=categories) elif request.form['name'] == '': flash('Please enter a name for your listing.') return render_template('newListing.html', categories=categories) elif request.form['new-category'] == '' and request.form['category'] == 'False': flash('You must select or create a category') return render_template('newListing.html', categories=categories) elif request.form['description'] == '': flash('You must fill out a description') return render_template('newListing.html', categories=categories) else: imagename = '' # check if user uploaded an image if request.files['image']: if 'image' not in request.files: flash('No file part') return render_template('newListing.html', categories=categories) image = request.files['image'] # if user does not select file, browser also # submits an empty part without the filename if image.filename == '': flash('No selected image') return redirect(request.url) if image and allowed_file(image.filename): imagename = secure_filename(image.filename) image.save(os.path.join(app.config['UPLOAD_FOLDER'], imagename)) if request.form['new-category']: if checkIfExists(request.form['new-category'],Category): flash('A category with that name already exists. Please re-name your category.') return render_template('newListing.html', categories=categories) else: newCategory = Category(name=request.form['new-category']) session.add(newCategory) session.commit() newListing = Listing( name=request.form['name'], description=request.form['description'], image=imagename, category_id=getCategoryID(request.form['new-category']), user_id=login_session['user_id']) session.add(newListing) flash('New Listing "%s" Successfully Created' % newListing.name) session.commit() return redirect(url_for('showListings')) else: newListing = Listing( name=request.form['name'], description=request.form['description'], image=imagename, category_id=getCategoryID(request.form['category']), user_id=login_session['user_id']) session.add(newListing) flash('New Listing "%s" Successfully Created' % newListing.name) session.commit() return redirect(url_for('showListings')) else: return render_template('newListing.html', categories=categories)
def get (self): id = int(self.request.get('id')) item = Listing.get(db.Key.from_path('Listing', id)) self.response.out.write('Listing: %s' % vars(item));
def get(self): def _simple_error(message, code=400): self.error(code) self.response.out.write(simplejson.dumps({ 'status': 'error', 'error': { 'message': message }, 'results': [] })) return None self.response.headers['Content-Type'] = 'application/json' query_type = self.request.get('type') user = self.request.get('user'); if not query_type in ['proximity', 'bounds', 'user', 'default']: return _simple_error('type parameter must be ' 'one of "proximity", "bounds", "user".', code=400) if query_type == 'proximity': try: center = geotypes.Point(float(self.request.get('lat')), float(self.request.get('lon'))) except ValueError: return _simple_error('lat and lon parameters must be valid latitude ' 'and longitude values.') elif query_type == 'bounds': try: bounds = geotypes.Box(float(self.request.get('north')), float(self.request.get('east')), float(self.request.get('south')), float(self.request.get('west'))) except ValueError: return _simple_error('north, south, east, and west parameters must be ' 'valid latitude/longitude values.') max_results = 100 if self.request.get('maxresults'): max_results = int(self.request.get('maxresults')) max_distance = 8000000 # 80 km ~ 50 mi if self.request.get('maxdistance'): max_distance = float(self.request.get('maxdistance')) results = [] try: # Can't provide an ordering here in case inequality filters are used. base_query = Listing.all() #if property_type: #base_query.filter('property_type =', property_type) # Natural ordering chosen to be public school enrollment. #base_query.order('-') # Perform proximity or bounds fetch. if query_type == 'proximity': results = Listing.proximity_fetch(base_query, center, max_results=max_results, max_distance=max_distance) elif query_type == 'bounds': results = Listing.bounding_box_fetch(base_query, bounds, max_results=max_results) elif query_type == 'user': limit = self.request.get("limit") offset = self.request.get("offset") if not limit: limit = 1000 else: limit = int(limit) if not offset: offset = 0 else: offset = int(offset) results = base_query.fetch(limit, offset); public_attrs = Listing.public_attributes() results_obj = [ _merge_dicts({ 'lat': result.location.lat, 'lng': result.location.lon, }, dict([(attr, getattr(result, attr)) for attr in public_attrs])) for result in results] self.response.out.write(simplejson.dumps({ 'status': 'success', 'results': results_obj, })) except: return _simple_error(str(sys.exc_info()[1]), code=500)
def dict_to_listing(d): """ Convert dictionary to listing object :param d: dictionary :return: listing object """ ls = Listing() ls.title = d['title'] if 'title' in d else '' ls.description = d['description'] if 'description' in d else '' ls.employer = d['employer'] if 'employer' in d else '' ls.student_matches = d['student_matches'] if 'student_matches' in d else [] ls.salary = float(d['salary']) if 'salary' in d else 0 ls.location = d['location'] if 'location' in d else '' if 'desired_skills' in d: if type(d['desired_skills']) == 'str': skills = d['desired_skills'].strip().split(',') ls.desired_skills = [i.strip() for i in skills] else: ls.desired_skills = d['desired_skills'] else: ls.desired_skills = [] ls.job_type = d['job_type'] if 'job_type' in d else [ 'Internship', 'Full-Time' ] return ls
except CapabilityDisabledError: flash(u'The database is currently in read-only mode. We apologize for the inconvenience. Please try again later', 'info') logging.error(traceback.extract_stack()) except TransactionFailedError: logging.error(traceback.extract_stack()) flash(u'Error saving item to database', 'error') finally: return redirect(url_for('all_listings')) else: return render_template('add_listing.html', form=form) @account_required def manage_listings(): '''Manage listings''' '''Get all listings in database''' listings = Listing.get_by_user(users.get_current_user()) return render_template('manage_listings.html', listings=listings) @account_required def manage_listing(id): listing = Listing.get_by_id(id) if listing is None: flash(u'No listing with that ID in database') return redirect(url_for('manage_listings')) form = EditListingForm(obj=listing) if form.validate_on_submit(): try: persist_listing(form, listing) flash(u'Listing %s successfully updated' % listing.key().id(), 'success') except CapabilityDisabledError: flash(u'The database is currently in read-only mode. We apologize for the inconvenience. Please try again later', 'info')
def manage_listing(id): listing = Listing.get_by_id(id) if listing is None:
def get(self): def _simple_error(message, code=400): self.error(code) self.response.out.write( simplejson.dumps({ 'status': 'error', 'error': { 'message': message }, 'results': [] })) return None self.response.headers['Content-Type'] = 'application/json' query_type = self.request.get('type') user = self.request.get('user') if not query_type in ['proximity', 'bounds', 'user', 'default']: return _simple_error( 'type parameter must be ' 'one of "proximity", "bounds", "user".', code=400) if query_type == 'proximity': try: center = geotypes.Point(float(self.request.get('lat')), float(self.request.get('lon'))) except ValueError: return _simple_error( 'lat and lon parameters must be valid latitude ' 'and longitude values.') elif query_type == 'bounds': try: bounds = geotypes.Box(float(self.request.get('north')), float(self.request.get('east')), float(self.request.get('south')), float(self.request.get('west'))) except ValueError: return _simple_error( 'north, south, east, and west parameters must be ' 'valid latitude/longitude values.') max_results = 100 if self.request.get('maxresults'): max_results = int(self.request.get('maxresults')) max_distance = 8000000 # 80 km ~ 50 mi if self.request.get('maxdistance'): max_distance = float(self.request.get('maxdistance')) results = [] try: # Can't provide an ordering here in case inequality filters are used. base_query = Listing.all() #if property_type: #base_query.filter('property_type =', property_type) # Natural ordering chosen to be public school enrollment. #base_query.order('-') # Perform proximity or bounds fetch. if query_type == 'proximity': results = Listing.proximity_fetch(base_query, center, max_results=max_results, max_distance=max_distance) elif query_type == 'bounds': results = Listing.bounding_box_fetch(base_query, bounds, max_results=max_results) elif query_type == 'user': limit = self.request.get("limit") offset = self.request.get("offset") if not limit: limit = 1000 else: limit = int(limit) if not offset: offset = 0 else: offset = int(offset) results = base_query.fetch(limit, offset) public_attrs = Listing.public_attributes() results_obj = [ _merge_dicts( { 'lat': result.location.lat, 'lng': result.location.lon, }, dict([(attr, getattr(result, attr)) for attr in public_attrs])) for result in results ] self.response.out.write( simplejson.dumps({ 'status': 'success', 'results': results_obj, })) except: return _simple_error(str(sys.exc_info()[1]), code=500)
from models import Listing MISS_THRESHOLD = 50 ZIPS = ['84121'] MAX_PRICE = 350000 CITY1 = 'cottonwood' CITY2 = 'salt' # INITIAL_MLS = 1239271, 1206693, 1204927 INITIAL_MLS = Listing.get_last_mls()