Пример #1
0
    def process_csv(self,blob_info,couple_key):
        blob_reader = blobstore.BlobReader(blob_info.key())
        reader = csv.reader(blob_reader, delimiter=',', quotechar='"')
        for row in reader:
            city_state = row[2].split(',')
            if row[11] == '':
                average_rating = None
            else:
                average_rating = float(row[11])

            r = dict(RestaurantName = row[0]
                        ,CuisineType = row[1]
                        ,City = city_state[0].strip()
                        ,State = city_state[1].strip()
                        ,NotesComments = row[3]
                        ,Completed = bool(int(row[4]))
                        ,FirstTripDate = helpers.convert_string_to_date(row[5])
                        ,LastVisitDate = helpers.convert_string_to_date(row[6])
                        ,NumberOfTrips = helpers.int_or_null(row[7])
                        ,DaysSinceLastTrip = helpers.int_or_null(row[8])
                        ,P1Rating = helpers.int_or_null(row[9])
                        ,P2Rating = helpers.int_or_null(row[10])
                        ,AverageRating = average_rating
                        ,parent=couple_key
                        )        
            entry = models.Eatery(**r)
            entry.put()
            entry_id = entry.key().id()
            key = "Eatery|" + str(entry_id)
            # Add Eatery entry to memcache.
            ra_memcache.cache_entity(key,entry_id,couple_key,models.Eatery.by_id,update=True)
Пример #2
0
    def post(self):
        submit = self.request.get('submit')
        if submit:
            couple_memcache_key = "Couple_Key|" + self.user.user_id()
            couple_key = ra_memcache.cache_entity(couple_memcache_key,self.user.user_id(),None,models.Couple.by_user_id,keys_only=True)
            if couple_key:
                eatery_id = self.request.get("id")
                if eatery_id: 
                    if eatery_id.isnumeric():                               
                        eatery_id = int(eatery_id)
                        # check if valid eatery id for the couple_key.
                        edit_found = self.check_hitlist(couple_key, eatery_id)
                        new = False
                    elif eatery_id.upper() == "NEW":
                        # User is posting data for new Eatery
                        edit_found = False
                        new = True
                    else:
                        # Fail the post attempt and 403 it.
                        edit_found = False
                        new = False

                    if edit_found or new:                        
                        # verify user inputs
                        failed = False
                        error_dict = {}
                        # Verify that the user entered a restaurant name.
                        restaurant_name = self.request.get("restaurantName")                        
                        if not restaurant_name:
                            failed = True
                            error_name = "You must enter a name."
                            error_dict['error_name'] = error_name
                        
                        cuisine_type = self.request.get("cuisineType")                        
                        city = self.request.get("city")
                        state = self.request.get("state")
                        notes_comments = self.request.get("notesComments")
                        completed = self.request.get("completed").upper()
                        if completed == "TRUE":
                            completed = True
                        else:
                            completed = False

                        first_trip_date = self.request.get("firstTripDate")
                        # Check if a value was entered first.
                        if first_trip_date:
                            first_trip_date = helpers.convert_string_to_date(first_trip_date)
                            if not first_trip_date:
                                failed = True
                                error_first_trip = "Invalid date format."
                                error_dict['error_first_trip'] = error_first_trip
                        else:
                            first_trip_date = None

                        last_visit_date = self.request.get("lastVisitDate")
                        # Check if a value was entered first.
                        if last_visit_date:
                            last_visit_date = helpers.convert_string_to_date(last_visit_date)
                            if not last_visit_date:
                                failed = True
                                error_last_visit = "Invalid date format."
                                error_dict['error_last_visit'] = error_last_visit
                        else:
                            last_visit_date = None

                        number_of_trips = self.request.get("numberOfTrips")
                        if number_of_trips and not number_of_trips.isnumeric():
                            failed = True
                            error_number_of_trips = "Invalid number entered."
                            error_dict['error_number_of_trips'] = error_number_of_trips
                        else:
                            number_of_trips = int(number_of_trips)

                        p1_Rating = self.request.get("p1Rating")
                        if p1_Rating:
                            check  = self.check_rating(1,p1_Rating)
                            if check[0]:
                                p1_Rating = int(p1_Rating)
                            else:
                                error_dict[check[1]] = check[2]
                        else:
                            p1_Rating = None

                        p2_Rating = self.request.get("p2Rating")
                        if p2_Rating:
                            check = self.check_rating(2,p2_Rating)
                            if check[0]:
                                p2_Rating = int(p2_Rating)
                            else:
                                error_dict[check[1]] = check[2]
                        else:
                            p2_Rating = None
                        # Get additional fields.
                        street_address = self.request.get("streetAddress")
                        # Verify that the zip code is a number.
                        zip_code = self.request.get("zipCode")
                        if zip_code:
                            if zip_code.isnumeric():
                                zip_code = int(zip_code)
                            else:                                
                                failed = True
                                error_zip_code = "Please enter a valid 5 digit zip code."
                        else:
                            zip_code = None
                        # Get yelp ID
                        yelp_business_id = self.request.get("yelpBusinessID")

                        # Get eatery entity.
                        if edit_found:
                            # handler for edits.
                            key = 'Eatery|' + str(eatery_id)
                            eatery = ra_memcache.cache_entity(key,eatery_id,couple_key,models.Eatery.by_id)

                        if failed:
                            if edit_found:
                                # add eatery to render dictionary
                                error_dict['eatery'] = eatery
                            # get couple object
                            couple_memcache_key = "Couple|" + self.user.user_id()
                            couple = ra_memcache.cache_entity(couple_memcache_key,self.user.user_id(),None,models.Couple.by_user_id,keys_only=False)
                            # add couple to render dictionary
                            error_dict['couple'] = couple
                            if edit_found:
                                # render edit template for edits.
                                self.render('hitlist-edit.html',**error_dict)
                            else:
                                # Render create template for creates.
                                self.render('hitlist-create.html',**error_dict)
                        else:
                            # User inputs validated prepare to commit to
                            days_last_trip = 0
                            # Calculate average rating.
                            if p1_Rating and p2_Rating:
                                average_rating = (p1_Rating + p2_Rating) / 2.0
                            elif p1_Rating:
                                average_rating = float(p1_Rating)
                            elif p2_Rating:
                                average_rating = float(p2_Rating)
                            else:
                                average_rating = None
                            
                            # change eatery entry to reflect changes.
                            if edit_found:
                                eatery.RestaurantName = restaurant_name                                
                            else:
                                # Or create new entity.
                                eatery = models.Eatery(RestaurantName=restaurant_name,parent=couple_key)                                   

                            eatery.CuisineType = cuisine_type                            
                            eatery.City = city
                            eatery.State = state
                            eatery.NotesComments = notes_comments
                            eatery.Completed = completed
                            eatery.FirstTripDate = first_trip_date
                            eatery.LastVisitDate = last_visit_date
                            eatery.NumberOfTrips = number_of_trips
                            eatery.P1Rating = p1_Rating
                            eatery.P2Rating = p2_Rating
                            eatery.AverageRating = average_rating
                            eatery.StreetAddress = street_address
                            eatery.ZipCode = zip_code
                            eatery.YelpBusinessID = yelp_business_id                            
                            # Save new Eatery to DB
                            eatery.put()
                            # if new trip add to trip db.           
                            self.increment_trip_db(eatery=eatery, couple_key=couple_key,
                                            first_trip_date=first_trip_date, last_visit_date=last_visit_date,
                                            number_of_trips=number_of_trips, edit=edit_found)

                            if new:
                                eatery_id = eatery.key().id()
                                key = 'Eatery|' + str(eatery_id)
                                hitlist_key = "Hitlist|" + str(couple_key.id())
                                ra_memcache.hitlist_cache(hitlist_key,couple_key,update=True)
                            # refresh memcache
                            eatery = ra_memcache.cache_entity(key,eatery_id,couple_key,models.Eatery.by_id,update=True)
                            # Redirect to hitlist.
                            self.redirect("/")
                    else:
                        self.error(403)
                else:
                    self.redirect("/")
            else:
                self.redirect("/register")