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(description_id=None): if not description_id: description_id = flask.request.args.get("pid", 0) geo_resource = None prefs = None main = Main(db) if int(description_id) == 0: prefs = main.get_user_pref() geo_resource = GeoResource(db, description_id, type_id=prefs[1], country_id=prefs[2], state_id=prefs[3]) else: geo_resource = GeoResource(db, description_id) html_repr = Html(geo_resource) html = html_repr.generate_editable() title = geo_resource.get_resource_name(geo_resource.type_name) if not title: title = "New %s %s" % (main.get_type_name(prefs[1]), prefs[0]) user_pref = main.make_html_user_pref() return flask.render_template("form.html", modules=html, title=title, user_pref=user_pref, is_moderated=geo_resource.is_moderated if description_id > 0 else True)
def get_all_resources(self, country_id=0, type_id=0, state_id=0): """ Fetches the most recent version for all the resources for the given country and type. @param: country_id: country id @param: type_id: type id @param: state_id: state id """ db_cxn = self.db_conn.session db_cur = db_cxn.cursor(dictionary=True) if int(country_id) <= 0 or int(type_id) <= 0: return ([], []) main = Main(self.db_conn) type_name = main.get_type_name(type_id) if not type_name: return ([], []) """ parent_ids = select.read("History", columns=["distinct(Parent_Plant_ID)"], where=[["Country_ID", "=", country_id], ["and"], ["Type_ID", "=", type_id], ["and"], ["Accepted", "=", 1] ] ) description_ids = select.read("History", columns=["Parent_Plant_ID", "Description_ID"], where=[["Parent_Plant_ID", "in", [x[0] for x in parent_ids]] ] ) """ name_field = "Name_omit" if type_id in self.types_with_segments: name_field = "Name_of_this_Segment" sql = "SELECT Parent_Plant_ID,Description_ID FROM History WHERE \ Parent_Plant_ID in \ (SELECT distinct(Parent_Plant_ID) \ FROM History \ WHERE Country_ID=%(country_id)s \ and Type_ID=%(type_id)s \ and Accepted=%(accepted)s \ ) \ and Accepted=1;" data = { "country_id": country_id, "type_id": type_id, "accepted": 1 } if state_id > 0: sql = "SELECT Parent_Plant_ID,Description_ID FROM History WHERE \ Parent_Plant_ID in \ (SELECT distinct(Parent_Plant_ID) \ FROM History \ WHERE Country_ID=%(country_id)s \ and State_ID=%(state_id)s \ and Type_ID=%(type_id)s \ and Accepted=%(accepted)s \ ) \ and Accepted=1;" data = { "country_id": country_id, "type_id": type_id, "state_id": state_id, "accepted": 1 } db_cur.execute(sql, data) keys = ["Description_ID", "Name"] select = Select(self.db_conn) # get the latest description id for all resources resources = {} for did in db_cur: if resources.get(did['Parent_Plant_ID'], 0) < int(did['Description_ID']): resources[did['Parent_Plant_ID']] = did['Description_ID'] result = select.read(type_name + "_Description", columns=["Description_ID", name_field], where=[["Description_ID", "in", list(resources.values())] ], order_by=[name_field, "ASC"]) db_cxn.close() return select.process_result_set(result)
def view(): main = Main(db) pref = main.get_user_pref() modules = [] select = Select(db) met = select.read("metadata") metadata = met.fetchall() module = {} module['heading'] = "Cumulative 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'] for t in types])) module['content'].append("<div id='pie_chart' style='width: 900px; height: 500px;'></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>") 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'], summary_link="".join(s_link), map_link="".join(map_link), pie_id=type_name, pie_value=t['Cumulative_Capacity']) 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="Chart.plotPieChart('', 'pie_chart')")
class GeoResource(object): """ Defines an energy resource. """ parent_plant_id = 0 type_id = 0 country_id = 0 state_id = 0 description_id = 0 def __init__(self, connection, description_id, type_id=None, country_id=None, state_id=None): """ The primitive class for all geo resources. """ #Html.__init__(self) if not description_id or int(description_id) == 0: if not type_id and not country_id and not state_id: raise AttributeError("Invalid request. Necessary details were not provided.") self.description_id = description_id self.connection = connection self.type_id = type_id self.country_id = country_id self.state_id = state_id self.parent_plant_id = 0 self.latest_revision_id = 0 self.is_moderated = 0 self.name = "" self.select = Select(self.connection) self.main = Main(self.connection) if not self.type_id or not self.country_id or not self.state_id: self.__get_ids() self.type_name = self.main.get_type_name(self.type_id) self.types_with_segments = [19, 20, 24, 25, 26, 27] def __get_ids(self): """ Private class, gets the necessary ids from the History table for a resource. """ result = self.select.read("History", where=[["Description_ID", "=", self.description_id]]) if result.rowcount == 0: raise LookupError("Description ID %s does not exist." % self.description_id) ids = result.fetchone() self.type_id = ids['Type_ID'] self.country_id = ids['Country_ID'] self.state_id = ids['State_ID'] self.parent_plant_id = ids['Parent_Plant_ID'] self.is_moderated = ids['Moderated'] def get_latest_revision_id(self, moderated=True): """ Get the latest revision id for this resource. """ if self.latest_revision_id > 0: return self.latest_revision_id if self.description_id == 0: # new plant creation return None if self.parent_plant_id == 0: self.__get_ids() where = [["Parent_Plant_ID", "=", self.parent_plant_id]] if moderated: where.extend([["and"], ["Accepted", "=", "1"]]) ids = self.select.read("History", columns=["max(Description_ID)"], where=where) res = ids.fetchone() print(res) self.latest_revision_id = res.get("max(Description_ID)") return self.latest_revision_id def get_resource_name(self, type_name=None): """ Get the name of the resource. Requires type name to be passed as the name is in the description table. """ if self.description_id == 0: return None if self.name: return self.name if not type_name: type_name = self.type_name name_field = "Name_omit" if self.type_id in self.types_with_segments: name_field = "Name_of_this_Segment" desc_table = type_name + "_Description" desc = self.select.read(desc_table, columns=[name_field], where=[["Description_ID", "=", self.get_latest_revision_id(moderated=False)]] ) self.name = desc.fetchone().get(name_field) return self.name
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)}