diff options
Diffstat (limited to 'avon.py')
| -rwxr-xr-x | avon.py | 91 |
1 files changed, 91 insertions, 0 deletions
| @@ -0,0 +1,91 @@ | |||
| 1 | #! venv/bin/python | ||
| 2 | |||
| 3 | import os | ||
| 4 | import sys | ||
| 5 | import pprint | ||
| 6 | import json | ||
| 7 | from flask import Flask,render_template,request | ||
| 8 | from argparse import ArgumentParser | ||
| 9 | import psycopg2 | ||
| 10 | |||
| 11 | app = Flask(__name__) | ||
| 12 | |||
| 13 | class Convert_jinja_object: | ||
| 14 | |||
| 15 | def __init__(self): | ||
| 16 | self.myvar = 'sample_var' | ||
| 17 | |||
| 18 | def bits_to_years(self, x): | ||
| 19 | year_map = [ "92Q2", "95Q0", "96Q0", "96Q1", "97Q1", "97Q3", "98Q1", "98Q3", "99Q1", "99Q3", "00Q1", "00Q3", "01Q1", "01Q2", "01Q3", "01Q4", "02Q1", "02Q3", "03Q1", "03Q3", "04Q1", "04Q3", "05Q1", "05Q3", "06Q1", "06Q3", "07Q1", "07Q3", "08Q1", "08Q3", "09Q1", "09Q3", "10Q1", "10Q3", "11Q1", "11Q3", "12Q1", "12Q3", "13Q1", "13Q3", "14Q1", "14Q3", "15Q1", "15Q3", "16Q1", "16Q3", "17Q1", "17Q3", "18Q1", "18Q3", "19Q1", "19Q3", "20Q1", "20Q3" ] | ||
| 20 | if x == 0: | ||
| 21 | return '/' | ||
| 22 | years = '' | ||
| 23 | index = 0 | ||
| 24 | while x > 0: | ||
| 25 | if index > 0: | ||
| 26 | years = years + ',' | ||
| 27 | start_off = (x&-x).bit_length()-1 | ||
| 28 | x = ~(x >> start_off) | ||
| 29 | end_off = (x&-x).bit_length()-1 | ||
| 30 | x = ~(x >> (end_off + 1)) | ||
| 31 | years = years + (year_map[index + start_off]) | ||
| 32 | if end_off > 1: | ||
| 33 | years = years + '-' + year_map[index + start_off + end_off - 1] | ||
| 34 | index = index + start_off + end_off + 1 | ||
| 35 | return years | ||
| 36 | |||
| 37 | @app.route("/", methods=['GET', 'POST']) | ||
| 38 | def root(): | ||
| 39 | content = request.json | ||
| 40 | |||
| 41 | query = "SELECT DISTINCT ON (id) presence_flag, reverse_flag, biz_flag, zip, nachname, vorname, zusaetze, strasse, hausnummer, verweise, ort, vorwahl, rufnummer, web, email, coords FROM Telefonbuch " | ||
| 42 | where = "" | ||
| 43 | params = [] | ||
| 44 | anyjoin = "" | ||
| 45 | |||
| 46 | for i in range(1,5): | ||
| 47 | search = request.form.get("search_{0}_string".format(i)) | ||
| 48 | if not search: continue | ||
| 49 | column = request.form.get("search_{0}_column".format(i)) | ||
| 50 | operator = request.form.get("search_{0}_operator".format(i)) | ||
| 51 | isany = request.form.get("search_{0}_any".format(i)) | ||
| 52 | |||
| 53 | if not column in [ 'zip', 'nachname', 'vorname', 'zusaetze', 'strasse', 'hausnummer', 'verweise', 'ort', 'vorwahl', 'rufnummer', 'web', 'email', 'coords' ]: continue | ||
| 54 | |||
| 55 | query = query + "INNER JOIN table_{0} ON Telefonbuch.id = table_{0}.telefonbuch_id ".format(column) | ||
| 56 | if len(where): | ||
| 57 | where = where + "AND " | ||
| 58 | else: | ||
| 59 | where = "WHERE " | ||
| 60 | where = where + "table_{0}.value {1} %s ".format(column, {'equals': '=', 'equalsnot': '<>', 'contains': 'ILIKE', 'containsnot': 'NOT ILIKE', 'beginswith': 'ILIKE'}[operator]) | ||
| 61 | if not isany: | ||
| 62 | if len(anyjoin) == 0: | ||
| 63 | anyjoin = "table_" + column | ||
| 64 | else: | ||
| 65 | where = where + "AND " + anyjoin + ".offs = table_" + column + ".offs " | ||
| 66 | if operator in ['contains','containsnot']: | ||
| 67 | search = "%" + search + "%" | ||
| 68 | if operator in ['beginswith']: | ||
| 69 | search = search + "%" | ||
| 70 | params.append(search) | ||
| 71 | |||
| 72 | if len(where) > 0: | ||
| 73 | conn = psycopg2.connect(database="erdgeist", user="postgres", password="", host="127.0.0.1") | ||
| 74 | cur = conn.cursor() | ||
| 75 | print (query + where + ' LIMIT 10000') | ||
| 76 | pprint.pprint( params ) | ||
| 77 | |||
| 78 | cur.execute(query + where + ' LIMIT 10000', params) | ||
| 79 | rows = cur.fetchall() | ||
| 80 | else: | ||
| 81 | rows = [[0,0,0,{},{},{},{},{},{},{},{},{},{}]] | ||
| 82 | |||
| 83 | return render_template('index.html', rows=rows, request=request, convert=Convert_jinja_object()) | ||
| 84 | |||
| 85 | if __name__ == "__main__": | ||
| 86 | parser = ArgumentParser(description="AVON") | ||
| 87 | parser.add_argument("-H", "--host", help="Hostname of the Flask app " + "[default %s]" % "127.0.0.1", default="127.0.0.1") | ||
| 88 | parser.add_argument("-P", "--port", help="Port for the Flask app " + "[default %s]" % "5000", default="5000") | ||
| 89 | args = parser.parse_args() | ||
| 90 | |||
| 91 | app.run(host=args.host, port=int(args.port)) | ||
