def get(self): with Session() as session: valid = session.query(Notification).\ filter_by(status=NotificationStatus.GENERATED).\ order_by(Notification.date_generated.desc()) per_company = {} for notification in valid: candidate = notification.candidate current = { 'id': notification.id, 'generated': notification.date_generated, 'obstaravanie': obstaravanieToJson(candidate.obstaravanie, 0), 'reason': obstaravanieToJson(candidate.reason, 0) } company = candidate.company_id if (not company in per_company): per_company[company] = { "name": candidate.company.name, "address": getAddressForIco(candidate.company.ico), "notifications": [], "first_id": candidate.id } per_company[company]["notifications"].append(current) with open("notifications.tmpl") as f: singleTemplate = Template(f.read().decode("utf8")) sorted_values = sorted(per_company.values(), key=lambda x: -x["first_id"]) html = singleTemplate.render(data=sorted_values, secret=self.request.GET.get( "secret", "")) self.response.write(html.encode("utf8"))
def get(self): try: company_id = int(self.request.GET["id"]) except: self.returnError(400, "Malformed id") return with Session() as session: company = session.query(Firma).filter_by(id=company_id).first() if company is None: self.returnError(404, 'No matching id') return result = { "name": company.name, "ico": company.ico, "eid": getEidForIco(company.ico) } candidates = [] for candidate in session.query(Candidate). \ filter_by(company_id=company_id). \ order_by(-Candidate.score): candidates.append([ candidate.score, obstaravanieToJson(candidate.obstaravanie, candidates=0, full_candidates=0), obstaravanieToJson(candidate.reason, candidates=0, full_candidates=0) ]) result["obstaravania"] = candidates with open("firma.tmpl") as f: singleTemplate = Template(f.read().decode("utf8")) html = singleTemplate.render(firma=result) self.response.write(html.encode("utf8"))
def get(self): self.response.write("update_notifications") # As a response, we expect parameters called all_notificationid to # specify which notifications are affected. If on_notificationid is also # present, that means that the notification is approved. approved = [] declined = [] for param in self.request.GET.keys(): if param.startswith("all_") and param[4:].isdigit(): notification_id = int(param[4:]) if ("on_" + str(notification_id)) in self.request.GET: approved.append(notification_id) else: declined.append(notification_id) with Session() as session: for_report = [] for nid in approved: notification = session.query(Notification).filter_by( id=nid).first() notification.status = NotificationStatus.APPROVED self.response.write(notification.candidate.obstaravanie.title) for_report.append(notification) for nid in declined: notification = session.query(Notification).filter_by( id=nid).first() notification.status = NotificationStatus.DECLINED if generateReport(for_report): session.commit() self.response.write("Done!")
def UpdateLastSync(timestamp): with Session() as new_session: new_session.query(LastSync).delete(synchronize_session=False) new_session.commit() last_sync = LastSync(last_sync=timestamp) session.add(last_sync) session.commit()
def GenerateNotifications(): global ico_lat_lng ico_lat_lng = utils.IcoToLatLngMap() with Session() as session: # Get the highest id that has been already processed last_update = session.query(LastNotificationUpdate).first() min_id = last_update.last_id if last_update is not None else 0 print "Last processed id", min_id # compute id's of companies with <= max_won already won cnts = session.query(func.count(Obstaravanie.winner_id), Obstaravanie.winner_id).\ group_by(Obstaravanie.winner_id) eligible_companies = set() for cnt, company_id in cnts: if (cnt <= max_won): eligible_companies.add(company_id) # keep track of already generated candidates, so that a notification is not # generated twice generated_notifications = set(e[0] for e in session.query(Notification.candidate_id).all()) gens = 0 max_id = None for obst in session.query(Obstaravanie).\ filter(Obstaravanie.id >= min_id).\ order_by(-Obstaravanie.id).limit(1000): if (max_id is None): max_id = obst.id if (obst.winner_id is not None): continue if (obst.finished): continue good_candidates = 0 for candidate in obst.candidates: if (IsGoodCandidate(obst, candidate)): good_candidates += 1 if (good_candidates > max_good_candidates_per_obst): break # Process only candidates, for which we know the address if utils.getAddressForIco(candidate.company.ico) == "": continue if ((candidate.company_id in eligible_companies) and not (candidate.id in generated_notifications) and (candidate.reason.customer.ico != obst.customer.ico)): notification = Notification( candidate_id=candidate.id, status=NotificationStatus.GENERATED, date_generated=func.now(), date_modified=func.now()) gens += 1 session.add(notification) print "Generated #notifications:", gens # Update the highest synced obstaravanie.id session.query(LastNotificationUpdate).delete(synchronize_session=False) last_update = LastNotificationUpdate(last_id=max_id) session.add(last_update) session.commit() session.close()
def get(self): try: oid = int(self.request.GET["id"]) except: self.returnError(400, "Malformed id") return with Session() as session: obstaravanie = session.query(Obstaravanie).filter_by( id=oid).first() if obstaravanie is None: self.returnError(404, "No matching id") return j = obstaravanieToJson(obstaravanie, 20, 20) self.returnJSON(j)
def get(self): try: oid = int(self.request.GET["id"]) except: self.abort(400, 'Error: Malformed id.') with Session() as session: obstaravanie = session.query(Obstaravanie).filter_by(id=oid).first() if obstaravanie is None: self.abort(404, 'Error: No matching id.') j = obstaravanieToJson(obstaravanie, 20, 20) # TODO: before launching this, move this to load only once with open("obstaravanie.tmpl") as f: singleTemplate = Template(f.read().decode("utf8")) html = singleTemplate.render(obstaravanie=j) self.response.write(html.encode("utf8"))
if (obstaravanie.winner is None): continue if (obstaravanie.description is None): continue corpus.append(dictionary.doc2bow(processObstaravanie(obstaravanie))) suppliers.append(obstaravanie.winner_id) ids.append(obstaravanie.id) index = similarities.Similarity('shards/', lsi[corpus], num_features=len(dictionary.values())) processed = 0 # process last num_lines entries # table with generated suggestions table = [] print "Computing candidates" if options.delete_predictions: with Session() as new_session: new_session.query(Candidate).delete(synchronize_session=False) new_session.commit() min_score = float(options.min_score) results = [] generated_predictions = 0 last_generated_bulletin = -1 for obstaravanie in session.query(Obstaravanie). \ filter(and_(Obstaravanie.bulletin_year.isnot(None), Obstaravanie.bulleting_number.isnot(None))). \ order_by(-Obstaravanie.bulletin_year,-Obstaravanie.bulleting_number): if (generated_predictions >= int(options.num_predictions)): break if (obstaravanie.bulletin_year < int(options.min_year)): print "year too low", obstaravanie.bulletin_year break
sql = ( "SELECT ico, trzby2015, zisk2015, datum_vzniku " + \ "FROM company_stats " + \ "WHERE trzby2015 IS NOT NULL" ) cur = db.execute(cur, sql) data = {} for row in cur.fetchall(): ico = int(row["ico"]) data[ico] = row print "Number of companies with data", len(data) with Session() as session: cnt = 0 for obstaravanie in session.query(Obstaravanie). \ filter(and_(Obstaravanie.winner_id.isnot(None), Obstaravanie.bulletin_year>=2015)). \ order_by(-Obstaravanie.bulletin_year, -Obstaravanie.bulleting_number): j = json.loads(obstaravanie.json) try: value = float(j["estimated_value_amount"]) except: # No amount, skip for now. continue try: ico = int( obstaravanie.winner.ico) # TODO: can have multiple winners... except:
def TestReport(): with Session() as session: for_report = session.query(Notification).order_by(Notification.id)[:2] for f in for_report: print f.candidate.company_id utils.generateReport(for_report)