def get(self, id=None):
        self.response.headers["Content-Type"] = "text/html"
        url = ""
        user = users.get_current_user()
        review_form_url = '/electric_vehicles/review/'
        if user:
            url = users.create_logout_url(self.request.uri)
            url_string = "logout"
        else:
            url = users.create_login_url(self.request.uri)
            url_string = "login"

        if id:
            electric_vehicle = ElectricVehicle.get_by_id(int(id))
            reviews = Review.query(Review.electric_vehicle == electric_vehicle.
                                   key).order(-Review.date).fetch()
            for_score = [rev.score for rev in reviews]
            avg_review = 0
            if len(for_score):
                avg_review = sum(for_score) / len(for_score)

        template_values = {
            "avg_review": avg_review,
            "reviews": reviews,
            "review_form_url": review_form_url,
            "electric_vehicle": electric_vehicle,
            "url": url,
            "url_string": url_string,
            "user": user
        }
        template = JINJA_ENVIRONMENT.get_template(
            "electric_vehicles/show.html")
        self.response.write(template.render(template_values))
示例#2
0
 def get(self, member_id):
     q = Review.query().filter(Review.member_id == member_id)
     count = q.count()
     to_fetch = count if count < 10 else 10
     reviews = q.fetch(to_fetch)
     print("Found " + str(count) + " reviews for member " + member_id +
           ", returning " + str(to_fetch))
     self.response.headers["Content-Type"] = "application/json"
     write_reviews(reviews, self.response)
示例#3
0
 def get(self, member_id):
     q = Review.query().filter(Review.member_id == member_id)
     count = q.count()
     to_fetch = count if count < 10 else 10
     results = q.fetch(to_fetch)
     print("Found " + str(count) + " reviews for member " + member_id +
           ", returning " + str(to_fetch))
     self.response.headers["Content-Type"] = "text/plain"
     first_result = True
     for result in results:
         if first_result:
             self.response.write('{\n    "reviews" : [\n')
             first_result = False
         else:
             self.response.write(',\n')
         json_response_dict = result.to_dict()
         json_response_dict["time"] = json_response_dict["time"].isoformat(
         ) + "Z"
         json_response_string = json.dumps(json_response_dict)
         self.response.write('        ')
         self.response.write(json_response_string)
     self.response.write('\n    ]\n}\n')
    def get(self):
        self.response.headers["Content-Type"] = "text/html"
        url = ""
        user = users.get_current_user()
        ids = self.request.get('ids', None)
        if user:
            url = users.create_logout_url(self.request.uri)
            url_string = "logout"
        else:
            url = users.create_login_url(self.request.uri)
            url_string = "login"

        columns = [
            'Name', 'Manufacturer', 'Year', 'Battery Size', 'WLTP Range',
            'Cost', 'Power'
        ]
        names = []
        manufacturers = ['Manufacturer']
        years = []
        battery_sizes = []
        wltp_ranges = []
        costs = []
        powers = []
        reviews = []

        if ids:
            ids = ids.split(",")
            elec_key_ids = [ndb.Key(ElectricVehicle, int(id)) for id in ids]
            electric_vehicles = ndb.get_multi(elec_key_ids)
            for x in electric_vehicles:
                names.append({"name": x.name, "id": x.key.id()})
                manufacturers.append(x.manufacturer)
                years.append({"color": "", "value": x.year})
                battery_sizes.append({"color": "", "value": x.batterySize})
                wltp_ranges.append({"color": "", "value": x.rangeWltp})
                costs.append({"color": "", "value": x.cost})
                powers.append({"color": "", "value": x.power})

                reviews_res = Review.query(
                    Review.electric_vehicle == x.key).fetch()
                for_score = [rev.score for rev in reviews_res]
                avg_review = 0
                if len(for_score):
                    avg_review = sum(for_score) / len(for_score)
                reviews.append({"color": "", "value": avg_review})

            years_highest = max(years, key=lambda x: x['value'])
            years_lowest = min(years, key=lambda x: x['value'])
            for y in years:
                if y["value"] == years_lowest["value"]:
                    y["color"] = 'danger'
                if y["value"] == years_highest["value"]:
                    y["color"] = 'success'
            years.insert(0, {"color": "", "value": "Year"})
            battery_sizes_highest = max(battery_sizes,
                                        key=lambda x: x['value'])
            battery_sizes_lowest = min(battery_sizes, key=lambda x: x['value'])
            for y in battery_sizes:
                if y["value"] == battery_sizes_lowest["value"]:
                    y["color"] = 'danger'
                if y["value"] == battery_sizes_highest["value"]:
                    y["color"] = 'success'
            battery_sizes.insert(0, {"color": "", "value": "Battery Size"})

            wltp_ranges_highest = max(wltp_ranges, key=lambda x: x['value'])
            wltp_ranges_lowest = min(wltp_ranges, key=lambda x: x['value'])
            for y in wltp_ranges:
                if y["value"] == wltp_ranges_lowest["value"]:
                    y["color"] = 'danger'
                if y["value"] == wltp_ranges_highest["value"]:
                    y["color"] = 'success'
            wltp_ranges.insert(0, {"color": "", "value": "WLTP Range"})

            powers_highest = max(powers, key=lambda x: x['value'])
            powers_lowest = min(powers, key=lambda x: x['value'])
            for y in powers:
                if y["value"] == powers_lowest["value"]:
                    y["color"] = 'danger'
                if y["value"] == powers_highest["value"]:
                    y["color"] = 'success'
            powers.insert(0, {"color": "", "value": "Power"})

            costs_highest = max(costs, key=lambda x: x['value'])
            costs_lowest = min(costs, key=lambda x: x['value'])
            for y in costs:
                if y["value"] == costs_lowest["value"]:
                    y["color"] = 'success'
                if y["value"] == costs_highest["value"]:
                    y["color"] = 'danger'
            costs.insert(0, {"color": "", "value": "Cost"})

            reviews_highest = max(reviews, key=lambda x: x['value'])
            reviews_lowest = min(reviews, key=lambda x: x['value'])
            for y in reviews:
                if y["value"] == reviews_lowest["value"]:
                    y["color"] = 'danger'
                if y["value"] == reviews_highest["value"]:
                    y["color"] = 'success'
            reviews.insert(0, {
                "color": "",
                "value": "Review Score (out of 10)"
            })

        template_values = {
            "reviews": reviews,
            "names": names,
            "manufacturers": manufacturers,
            "years": years,
            "battery_sizes": battery_sizes,
            "wltp_ranges": wltp_ranges,
            "costs": costs,
            "powers": powers,
            "electric_vehicles": electric_vehicles,
            "url": url,
            "url_string": url_string,
            "user": user
        }
        template = JINJA_ENVIRONMENT.get_template(
            "electric_vehicles/compare.html")
        self.response.write(template.render(template_values))