def post(self): """ This receives a Query Object and performs a Search based on information from that Query. It then compares the results to the search_date and if they are deemed fresh, stores them. """ new_search = Search() f = formatDatetime() q = db.get(self.request.get('key')) results = new_search.getResults(q.term, q.min, q.max, q.city) # Pull fresh listings from query, if they exist if q.fresh: fresh = q.fresh else: fresh = FreshEntries() fresh.entries = [] # Store fresh listings here search_date = q.search_date latest_entry_time = search_date # Compare each entry datetime to the saved datetime for e in results: # Extract and format times from feed f_entry_time = f.craigslist_to_datetime(e.date) # Compute elapsed time since last search and this listing difference = f_entry_time - search_date # If entry is after the saved time, flag it as fresh if f_entry_time > search_date: # Check and see if this is the chronologically latest listing if f.craigslist_to_datetime(e.date) > latest_entry_time: latest_entry_time = f.craigslist_to_datetime(e.date) entry = Entry(date = e.date, title = e.title, link = e.link) db.put(entry) fresh.entries.append(entry.key()) db.put(fresh) # put back query with new search_date and new fresh listings q.search_date = latest_entry_time q.fresh = fresh db.put(q)
def get(self): if users.get_current_user(): f = formatDatetime() current_user = users.get_current_user() old_user = db.GqlQuery("SELECT * FROM User WHERE user_id = '%s'" % current_user.user_id()).get() term = self.request.get('term') min = self.request.get('Min') max = self.request.get('Max') city = self.request.get('City') search_date = self.request.get('Search_Date') if search_date: search_date = f.string_to_datetime(search_date) else: search_date = datetime.now() if old_user: found_existing_query = False for q in old_user.queries: if db.get(q).term.lower() == term.lower() and db.get(q).city.lower() == city.lower(): found_existing_query = True if found_existing_query: self.response.out.write("Sorry-- It looks like you've already searched for this so, we won't save this one again<br>") else: # Save to the datastore query = Query(author = current_user, search_date = search_date, term = term, min = min, max = max, city = city) query.put() old_user.queries.append(query.key()) old_user.put() self.redirect("/user") else: query = Query(author = users.get_current_user(), search_date = search_date, term = term, min = min, max = max, city = city) query.put() current_user = users.get_current_user() user_profile = User(user = current_user, user_id = current_user.user_id(), email = current_user.email()) user_profile.queries = [query.key()] user_profile.put() self.redirect("/user") else: self.redirect(users.create_login_url(self.request.uri))
def get(self): if users.get_current_user(): url = "/user" url_linktext = 'My Want List' else: url = users.create_login_url(self.request.uri) url_linktext = 'Login' f = formatDatetime() formatted_term = self.request.get('q') # handle spaces in query terms min = self.request.get('Min') max = self.request.get('Max') city = self.request.get('City') new_search = Search() try: results = new_search.getResults(formatted_term, min, max, city) if results: search_date = f.craigslist_to_datetime(results[0].date) else: search_date = datetime.now() template_values = { 'search_date': search_date, 'term': formatted_term, 'min': min, 'max': max, 'city': city, 'results': results, 'url': url, 'url_linktext': url_linktext, } path = os.path.join(os.path.dirname(__file__), '../static/html/search.html') self.response.out.write(template.render(path, template_values)) except: self.response.out.write("Sorry, there was an error connecting to Craigslist. Try checking your 'City' field, or possibly your internet connection, and try again.")