def view(db_type=None, type_id=None, country_id=None, state_id=None): main = Main(db) pref = main.get_user_pref() if not type_id or not country_id: url = "/".join(["/allunits", pref[0], pref[1], pref[2]]) #url = main.get_search_redirect_url("summary/type", return_type="type_summary") #print(url) return flask.redirect(url) type_name = main.get_type_name(type_id) country_name = main.get_country_name(country_id) units = [] moderation = Moderation(db) res_label, resources = moderation.get_all_resources(country_id=country_id, type_id=type_id) for res in resources: desc_id = res[0] name = res[1] unit = {"resource_name": name, "resource_id": desc_id} geo_resource = GeoResource(db, desc_id) html = Html(geo_resource) html.editable = False unit["content"] = html.make_unit_module("Unit_Description") units.append(unit) main.store_user_pref(pref[0], country_id, type_id, pref[3]) user_pref = main.make_html_user_pref() return flask.render_template("allunits.html", resources=units, country=country_name, type=type_name, user_pref=user_pref)
def view(type_id=None, country_id=None): main = Main(db) pref = main.get_user_pref() if not type_id or not country_id: url = "/".join(["/summary/type", pref[1], pref[2]]) #url = main.get_search_redirect_url("summary/type", return_type="type_summary") #print(url) return flask.redirect(url) type_name = main.get_type_name(type_id) country_name = main.get_country_name(country_id) content = [] content.extend(["<input type='hidden'", "name='summary_json'", "id='summary_json'", "value='" + flask.url_for("summarydata.summarydata", country_id=country_id, type_id=type_id) + "'", "class='widget_urls'", "/>" ]) main.store_user_pref(pref[0], country_id, type_id, pref[3]) user_pref = main.make_html_user_pref() title = type_name + " - " + country_name return flask.render_template("type_summary.html", content="".join(content), title=title, user_pref=user_pref)
def get_resources_to_moderate(self): """ Returns a list of resources awaiting moderation. Will return both new and edited resources. """ select = Select(self.db_conn) description_ids = select.read("History", columns=[ "Parent_Plant_ID", "Description_ID" ], where=[["Moderated", "=", "0"]], order_by=["Description_ID", "desc"] ) main = Main(self.db_conn) new_submits = [] edits = [] for description_id in description_ids: if not description_id['Parent_Plant_ID']: #TODO: this should not happen. all versions shd have a parent. error from old code? continue geo_resource = GeoResource(self.db_conn, description_id['Description_ID']) type_id = geo_resource.type_id country_id = geo_resource.country_id type_name = main.get_type_name(type_id) country_name = main.get_country_name(country_id) geo_name = geo_resource.get_resource_name(type_name=type_name) if description_id['Description_ID'] == description_id['Parent_Plant_ID']: new_submits.append({ 'type_name': type_name, 'country_name': country_name, 'geo_name': geo_name, 'description_id': str(description_id['Description_ID']) }) else: edits.append({ 'type_name': type_name, 'country_name': country_name, 'geo_name': geo_name, 'description_id': str(description_id['Description_ID']) }) return new_submits, edits
def get_resources_to_moderate(self): """ Returns a list of resources awaiting moderation. Will return both new and edited resources. """ select = Select(self.db_conn) description_ids = select.read( "History", columns=["Parent_Plant_ID", "Description_ID"], where=[["Moderated", "=", "0"]], order_by=["Description_ID", "desc"], ) main = Main(self.db_conn) new_submits = [] edits = [] for description_id in description_ids: geo_resource = GeoResource(self.db_conn, description_id["Description_ID"]) type_id = geo_resource.type_id country_id = geo_resource.country_id type_name = main.get_type_name(type_id) country_name = main.get_country_name(country_id) geo_name = geo_resource.get_resource_name(type_name=type_name) if description_id["Description_ID"] == description_id["Parent_Plant_ID"]: new_submits.append( { "type_name": type_name, "country_name": country_name, "geo_name": geo_name, "description_id": str(description_id["Description_ID"]), } ) else: edits.append( { "type_name": type_name, "country_name": country_name, "geo_name": geo_name, "description_id": str(description_id["Description_ID"]), } ) return new_submits, edits
def view(db_type=None, typ=None, country=None, state=None): if not typ or not country: typ = flask.request.args.get("type", 0) country = flask.request.args.get("country", 0) state = flask.request.args.get("state", 0) main = Main(db) if not typ or not country: pref = main.get_user_pref() url_comp = ["/resources"] url_comp.extend(pref) url = "/".join(url_comp) #url = main.get_search_redirect_url("resources") return flask.redirect(url) moderation = Moderation(db) keys, values = moderation.get_all_resources(country_id=country, type_id=typ) baseurl = "/geoid/" # storing the prefs to a cookie main.store_user_pref(db_type, country, typ, state) type_name = main.get_type_name(typ) country_name = main.get_country_name(country) state_name = None if state > 0: state_name = main.get_state_name(state) user_pref = main.make_html_user_pref() return flask.render_template("resources.html", values=values, baseurl=baseurl, user_pref=user_pref, type=type_name, country=country_name, state=state_name if state > 0 else False, category=db_type)
def view(): typ = flask.request.args.get("type", 0) country = flask.request.args.get("country", 0) state = flask.request.args.get("state", 0) description_id = flask.request.args.get("description_id", 0) loc = Location(db) try: desc_id = int(description_id) except ValueError: desc_id = 0 if desc_id > 0: return flask.jsonify(data=loc.for_one_resource(description_id=description_id)) elif int(typ) > 0 and int(country) > 0: return flask.jsonify(data=loc.for_many_resources(type_id=typ, country_id=country)) else: main = Main(db) prefs = main.get_user_pref() country_name = main.get_country_name(prefs[2]) state_name = main.get_state_name(prefs[3]) bound_location = "%s, %s" % (state_name, country_name) return flask.jsonify(data={"boundLocation": bound_location})
def view(country_id=None, country_abbr=None): main = Main(db) pref = main.get_user_pref() if not country_id and not country_abbr: url = "/".join(["/summary/country", pref[2]]) #url = main.get_search_redirect_url("summary/country", # return_type='country_summary') return flask.redirect(url) elif country_abbr: country_id = main.get_country_id_from_abbr(country_abbr) return flask.redirect("/summary/country/" + str(country_id)) country_name = main.get_country_name(country_id) modules = [] select = Select(db) res = select.read("metadata", columns=["Type_ID", "Number_of_Plants", "Cumulative_Capacity_Total"], where=[["Country_ID", "=", country_id]], order_by=["Type_ID", "asc"]) types = res.fetchall() module_content = """ <table style="width: 90%"> <tr> <td style="width: 70%">Number of {type_name} {db}:</td> <td>{total}</td> </tr> <tr> <td style="width: 70%">Total Cumulative Capacity (MWe):</td> <td>{cumulative_capacity}</td> </tr> <tr> <td style="width: 70%">Map All {type_name} {db}:</td> <td>{map_link}</td> </tr> <tr> <td style="width: 70%">List All Units in Individual Plants</td> <td>{all_units_link}</td> </tr> <tr> <td style="width: 70%">Complete summary:</td> <td>{summary_link}</td> </tr> </table> <input type='hidden' id='{pie_id}' class='pie_chart_values' value='{pie_value}' /> """ module = {} module['heading'] = "Installed Capacity by Category" module['content'] = [] module['content'].append("<p style='text-align: center'><b>Total Cumulative Capacity: %s MWe</b></p>" % sum([t['Cumulative_Capacity_Total'] for t in types])) module['content'].extend(["<div id='pie_chart'>", "<div id='pie_chart_1'>", "<div id='pie_chart_1_title' style='text-align: center'>", "<b>Traditional Energy Sources</b></div>", "</div>", "<div id='pie_chart_2'>", "<div id='pie_chart_2_title' style='text-align: center'>", "<b>Renewable Energy Sources</b></div>", "</div>", "</div>"]) module['content'] = "".join(module['content']) modules.append(module) #keys, types = main.get_types_for_country(country_id) for t in types: type_name = main.get_type_name(t['Type_ID']).replace("_", " ") db_name = main.get_databases(type_id=t['Type_ID'])[1][0][0] dbn = "" if db_name == "PowerPlants": dbn = "power plants" else: dbn = "" s_link = [] s_link.append("<a href='/summary/type/") s_link.append(str(t['Type_ID'])) s_link.append("/" + str(country_id)) s_link.append("'>Click Here</a>") map_link = [] map_link.append("<a href='/map/") map_link.append(db_name.lower() + "/") map_link.append(str(t['Type_ID']) + "/") map_link.append(str(country_id) + "/0/") map_link.append("'>Map</a>") all_units = [] all_units.append("<a href='/allunits/") all_units.append(pref[0] + "/") all_units.append(str(t['Type_ID']) + "/") all_units.append(str(country_id)) all_units.append("'>All Units</a>") module = {} module['heading'] = type_name module['content'] = module_content.format(type_name=type_name, db=dbn, total=t['Number_of_Plants'], cumulative_capacity=t['Cumulative_Capacity_Total'], summary_link="".join(s_link), map_link="".join(map_link), all_units_link="".join(all_units), pie_id=type_name, pie_value=t['Cumulative_Capacity_Total']) modules.append(module) main.store_user_pref(pref[0], country_id, pref[1], pref[3]) user_pref = main.make_html_user_pref() title = "Summary for " + country_name return flask.render_template("country_summary.html", modules=modules, title=title, country=country_name, user_pref=user_pref, body_onload="CountrySummary.init()")
class Location(object): """ A class for all the location information. """ def __init__(self, connection): self.connection = connection self.select = Select(self.connection) self.main = Main(self.connection) def __get_lat_lng(self, table_name, description_id): """ A private class to get lat and lng for a resource. """ result = self.select.read(table_name, where=[["Description_ID", "=", description_id]] ) if not result.with_rows: return (None, None) loc = result.fetchone() lat = loc['Latitude_Start'] lng = loc['Longitude_Start'] lat_end = loc['Latitude_End'] lng_end = loc['Longitude_End'] return (lat, lng), (lat_end, lng_end) def for_one_resource(self, description_id): """ Returns the location for a resource. :@param description_id :@returns dict: {lat, lng, overlays} """ gresource = GeoResource(self.connection, description_id) type_id = gresource.type_id type_name = self.main.get_type_name(type_id) (lat, lng), (lat_end, lng_end) = self.__get_lat_lng(type_name + "_Location", description_id) overlay_result = self.select.read(type_name + "_Overlays", where=[["Description_ID", "=", description_id]] ) o_results = overlay_result.fetchall() overlays = [] for overlay in o_results: details = {} details['color'] = overlay['Color'] details['weight'] = overlay['Weight'] details['opacity'] = overlay['Opacity'] details['points'] = overlay['Points'] details['numLevels'] = overlay['Num_Levels'] details['zoomFactor'] = overlay['Zoom_Factor'] details['overlayType'] = overlay['Overlay_Type'] details['overlayName'] = overlay['Overlay_Name'] overlays.append(details) locations = {} locations['lat'] = lat locations['lng'] = lng if lat_end and lng_end: locations['lat_end'] = lat_end locations['lng_end'] = lng_end locations['name'] = gresource.get_resource_name(type_name) locations['overlays'] = overlays return {"locations": [locations]} def for_many_resources(self, country_id=None, type_id=None): """ Returns the location information for all the resources in a country for the type. :@param country: the country id :@param typ: type id """ if int(country_id) <= 0 or int(type_id) <= 0: return {} moderation = Moderation(self.connection) keys, values = moderation.get_all_resources(country_id=country_id, type_id=type_id) del keys table_name = self.main.get_type_name(type_id) + "_Location" loc = [] locations = self.select.read(table_name, columns=["Description_ID", "Latitude_Start", "Longitude_Start"], where=[["Description_ID", "in", [value[0] for value in values]]], dict_cursor=False) locations = locations.fetchall() for value in values: for location in locations: if value[0] == int(location[0].decode("utf8")): loc.append([location[1].decode("utf8"), location[2].decode("utf8"), value[1] ]) """ lat = loc['Latitude_Start'] lng = loc['Longitude_Start'] for value in values: desc_id = value[0] name = value[1] lat, lng = self.__get_lat_lng(table_name, desc_id) loc.append([lat, lng, name]) """ return {"locations": loc, "boundLocation": self.main.get_country_name(country_id)}