Ejemplo n.º 1
0
def makeMeasure(text,
                region_id=None,
                district_id=None,
                display_id=None,
                title=None,
                source="",
                isOFP=None,
                subtitle=None):
    if district_id != None and region_id == None:
        is_district = True
    elif district_id == None and region_id != None:
        is_district = False
    else:
        quit("Fataler Fehler beim Aufruf von makeMeasure")

    if display_id == None:
        if title == None:
            raise ValueError("Neither display_id nor title given")
        d = getOrmakeCategory(title)
        # Entsprechende Kategorie anlegen
    else:
        d = display.query.get(display_id)
    if isOFP != None:  #TODO: Solve this more effectively!
        d.is_OFP = isOFP
    rr = createDefaultGroup(d.id, text, subtitle=subtitle)
    if not rr.ok:
        raise Exception("Error: createDefaultGroup: " + rr.etxt)
    g = rr.val

    if not is_district:
        dhg = regionHasGroup.query.filter(
            regionHasGroup.region_id == region_id,
            regionHasGroup.displayGroup_id == g.id).first()
        if not dhg:
            # Check if link between region and Group already exists
            dhg = regionHasGroup(region_id, g.id)
            db.session.add(dhg)
            db.session.flush()
            # Create new Link
    else:
        dhg = districtHasGroup.query.filter(
            districtHasGroup.district_id == district_id,
            districtHasGroup.displayGroup_id == g.id).first()
        if not dhg:
            # Check if link between region and Group already exists
            dhg = districtHasGroup(district_id, g.id)
            db.session.add(dhg)
            db.session.flush()
            # Create new Link
    dhg.source_id = createSource(source).id
    dhg.autolinked = True
    dhg.is_deleted = False
    return g
Ejemplo n.º 2
0
def makeMeasure(text, region_id, display_id=None, title=None):
    if display_id == None:
        if title == None:
            return 0, "Neither display_id nor title given"
        c = categories.query.filter(categories.name == title).first()
        # Prüfen, ob eine gleichnamige Kategorie schon existiert (Ausschlaggebend ist IMMER der Name in deutscher Sprache.)
        if not c:
            # Fehler schmeißen!!!
            # Wenn keine Kategorie existiert, erstellen
            c = categories(title)
            db.session.add(c)
            db.session.flush()
            # Erzeuge neue Kategorie
            d = display("$_text_",
                        None,
                        category_id=c.id,
                        is_default=True,
                        varlist={"text": {
                            "type": "str"
                        }})
            db.session.add(d)
            # Erzeuge Default-Display für die neue Kategorie
            db.session.flush()

        d = display.query.filter(display.category_id == c.id,
                                 display.is_default == True).first()
        # Display-Id ermitteln
        if not d:
            return 0, "No default display found for this category. This is due to a data integrety error."
            # TODO: add to log
        display_id = d.id
    rr = createDefaultGroup(display_id, {"text": text})
    if not rr.ok:
        return 0, "Error: createDefaultGroup: " + rr.etxt
    g = rr.val
    #dhg = regionHasGroup.query.get({"region_id": region_id, "displayGroup_id": g.id})
    dhg = regionHasGroup.query.filter(
        regionHasGroup.region_id == region_id,
        regionHasGroup.displayGroup_id == g.id).first()
    if not dhg:
        # Check if link between region and Group already exists
        dhg = regionHasGroup(region_id, g.id)
        db.session.add(dhg)
        db.session.flush()
        # Create new Link
    dhg.source_id = createSource("https://tourismus-wegweiser.de/json").id
    dhg.autolinked = True
    dhg.is_deleted = False
Ejemplo n.º 3
0
def mainLinks(neueKreise, overwrite=False):
    sid = createSource("Initialisation").id
    with open('links.json') as f:
        data = json.load(f)
    if overwrite:
        db.session.execute(
            links.__table__.delete().where(links.source_id == sid))
    for d in data:
        if overwrite or d["id"] in neueKreise:
            di = districts.query.get(d["id"])
            di.mainLink = d["link"]
            if di.category in ["Kreisfreie Stadt", "Bezirk", "Stadtkreis"]:
                l = links("Zur Webseite der Stadt", d["link"], d["id"], sid)
            else:
                l = links("Zur Webseite des Kreises", d["link"], d["id"], sid)
            db.session.add(l)
Ejemplo n.º 4
0
def createMeasure(inputs={}, district_id=None, region_id=None):
    if district_id != None and region_id == None:
        is_district = True
    elif district_id == None and region_id != None:
        is_district = False
    else:
        return "Internal Server Error: createMeasure being called with nonsense arguments.", 500
    #INPUTS:
    #   source (highly recommended)
    #   display_id*
    #   configuration* (JSON)
    #   replicats_id (highly not recommended to use) #TODO: Remove?
    #   no_dedup (true or false)

    # check for required values
    if not all(e in inputs for e in ("display_id", "configuration")):
        return "Error: Required values missing", 400
    if not isinstance(inputs.get("configuration"), dict):
        try:
            configuration = json.loads(inputs.get("configuration"))
        except ValueError:
            return "Invalid JSON in configuration", 400
    else:
        configuration = inputs.get("configuration")
    display_id = inputs.get("display_id")

    if "replicats_id" in inputs:
        replicats_id = inputs.get("replicats_id")
    else:
        replicats_id = None

    if "no_dedup" in inputs:
        if isinstance(inputs.get("no_dedup"), bool):
            if inputs.get("no_dedup"):
                deduplication = False
            else:
                deduplication = True
        else:
            if inputs.get("no_dedup") == "true":
                deduplication = False
            elif inputs.get("no_dedup") == "false":
                deduplication = True
            else:
                return jsonify({
                    "status": "ValueError",
                    "value": "no_dedup",
                    "error": "boolean: use 'true' or 'false'"
                }), 400
    else:
        deduplication = True

    rr = createDefaultGroup(display_id,
                            configuration,
                            replicats_id=replicats_id,
                            deduplication=deduplication)
    if not rr.ok:
        return "Error: createDefaultGroup: " + rr.etxt, 400
    g = rr.val
    if is_district:
        dhg = districtHasGroup.query.get({
            "district_id": district_id,
            "displayGroup_id": g.id
        })
    else:
        dhg = regionHasGroup.query.get({
            "region_id": region_id,
            "displayGroup_id": g.id
        })
    if not dhg:
        # Check if link between district/region and Group already exists
        if is_district:
            dhg = districtHasGroup(district_id, g.id)
        else:
            dhg = regionHasGroup(region_id, g.id)
        db.session.add(dhg)
        db.session.flush()
        # Create new Link
    if "source" in inputs:
        dhg.source_id = createSource(inputs.get("source")).id
    db.session.commit()
    return jsonify({"status": "Done", "displayGroup_id": g.id}), 201
Ejemplo n.º 5
0
def modifyMeasure(inputs, group_id, district_id=None, region_id=None):
    if district_id != None and region_id == None:
        is_district = True
    elif district_id == None and region_id != None:
        is_district = False
    else:
        return "Internal Server Error: modifyMeasure being called with nonsense arguments.", 500
    #Measure von Kreis bearbeiten = löschen und neu erstellen. oder bearbeiten, falls detatched
    #INPUTS:
    #   source (highly recommended)
    #   display_id
    #   configuration (JSON)
    #   no_dedup (true or false)

    # 1) Alte Maßnahme löschen = trennen (1): Alte Maßnahme holen. Löschen passiert erst während des erstellens der neuen.
    if is_district:
        dhg1 = districtHasGroup.query.get({
            "district_id": district_id,
            "displayGroup_id": group_id
        })
    else:
        dhg1 = regionHasGroup.query.get({
            "region_id": region_id,
            "displayGroup_id": group_id
        })
    if not dhg1:
        return "Not found", 404
    if dhg1.is_deleted:
        return "Measure is marked as deleted", 403

    if "display_id" in inputs:
        disp = display.query.get(inputs.get("display_id"))
        if not disp:
            #check if display_id is valid
            return jsonify({
                "status": "ValueError",
                "value": "display_id",
                "error": "Not found'"
            }), 400
    else:
        disp = dhg1.displayGroup.displayLink[0].display

    if "configuration" in inputs:
        if not isinstance(inputs.get("configuration"), dict):
            try:
                configuration = json.loads(inputs.get("configuration"))
            except ValueError:
                return "Invalid JSON in configuration", 400
        else:
            configuration = inputs.get("configuration")
    else:
        configuration = dhg1.displayGroup.displayLink[0].configuration

    if "no_dedup" in inputs:

        if isinstance(inputs.get("no_dedup"), bool):
            if inputs.get("no_dedup"):
                deduplication = False
            else:
                deduplication = True
        else:
            if inputs.get("no_dedup") == "true":
                deduplication = False
            elif inputs.get("no_dedup") == "false":
                deduplication = True
            else:
                return jsonify({
                    "status": "ValueError",
                    "value": "no_dedup",
                    "error": "boolean: use 'true' or 'false'"
                }), 400
    else:
        if dhg1.displayGroup.is_default == 2:
            deduplication = False
        else:
            deduplication = True

    if "source" in inputs:
        if inputs.get("source") != "":
            source_id = createSource(inputs.get("source")).id
        else:
            source_id = None
    else:
        source_id = dhg1.source_id

    # 2) Check ob alte Maßnahme detatched. Dann direkt ändern. sonst mit Löschen und neu-erstellen
    if not (
            dhg1.displayGroup.is_default == 2 and deduplication == True
    ) and not dhg1.displayGroup.default_of and not dhg1.displayGroup.overwritten_by and len(
            dhg1.displayGroup.regionLink) + len(
                dhg1.displayGroup.districtLink) == 1:
        # erste veroderung sorgt dafür, dass bei änderung von deduplication von false auf true die maßnahme neu erstellt wird, um von der deduplication gebrauch zu machen
        # Prüfen, ob Maßnahme nicht als default besteht und auch von keiner anderen überschrieben wird und nur zu einem Landkreis oder Land gehört
        # Änderungen direkt schreiben
        dhg1.source_id = source_id
        if "display_id" in inputs:
            dhg1.displayGroup.displayLink[0].display = disp
            #Update DisplayGroup
        if "configuration" in inputs or "display_id" in inputs:
            ok, err = validateConfig(configuration, disp.varlist)
            if not ok:
                return jsonify({
                    "status":
                    "ValueError",
                    "value":
                    "configuration",
                    "error":
                    "Invalid configuration for display '" + str(disp.id) +
                    "': " + err
                }), 400
            dhg1.displayGroup.displayLink[0].configuration = configuration
            #Update configuration
        if "no_dedup" in inputs:
            if deduplication:
                dhg1.displayGroup.is_default = 1
            else:
                dhg1.displayGroup.is_default = 2
        db.session.commit()
        return jsonify({
            "status": "Done",
            "displayGroup_id": group_id,
            "comment": "reused"
        }), 201

    # 3) Neue Maßnahme erstellen (wenn nicht geändert werden durfte)
    if (dhg1.displayGroup.is_default == 2 and deduplication == False
            or dhg1.displayGroup.is_default == 1 and deduplication == True
        ) and disp.id == dhg1.displayGroup.displayLink[
            0].display_id and configuration == dhg1.displayGroup.displayLink[
                0].configuration:
        # Wenn sich nichts geändert hat kann einfach nur die quelle aktualisiert werden, falls die Maßnahme zu mehreren Kreisen / Regionen gehört.
        dhg1.source_id = source_id
        #still update source
        return jsonify({
            "status": "Done",
            "displayGroup_id": group_id,
            "comment": "Already existed"
        }), 200
    rr = createDefaultGroup(disp.id,
                            configuration,
                            replicats_id=dhg1.displayGroup.id,
                            deduplication=deduplication)
    if not rr.ok:
        return "Error: createDefaultGroup: " + rr.etxt, 400
    if dhg1.displayGroup.is_default == 2 and deduplication == True:
        # löscht die alte Maßnahme, die zuvor nicht dedupliziert worden war
        db.session.delete(dhg1.displayGroup.displayLink[0])
        db.session.delete(dhg1)
    else:
        dhg1.is_deleted = True
        #Mark as deleted
    db.session.flush()

    g = rr.val
    if is_district:
        dhg2 = districtHasGroup.query.get({
            "district_id": district_id,
            "displayGroup_id": g.id
        })
    else:
        dhg2 = regionHasGroup.query.get({
            "region_id": region_id,
            "displayGroup_id": g.id
        })
    if not dhg2:
        # Check if link between district/region and Group already exists ()
        if is_district:
            dhg2 = districtHasGroup(district_id, g.id)
        else:
            dhg2 = regionHasGroup(region_id, g.id)
        db.session.add(dhg2)
        db.session.flush()
        # Create new Link
    dhg2.source_id = source_id
    dhg2.is_deleted = False
    # Mark as not deleted
    db.session.commit()
    return jsonify({"status": "Done", "displayGroup_id": g.id}), 201
Ejemplo n.º 6
0
def updateNINA(do_push=True):
    print("Lade Aktuelle Daten der NINA-App herunter")
    source_text = "NINA-API"
    so = sources.query.filter(sources.text == source_text).first()
    if not so:
        so = createSource(source_text)
    #Neue verknüpfungen erstellen
    ds = districts.query.all()
    jobQueue = Queue()
    resultQueue = Queue()
    for d in ds:
        jobQueue.put({
            "region_id": d.region_id,
            "district_id": d.id,
            "source_id": so.id
        })
    #jobQueue.put({"region_id": 11, "district_id": 11012, "source_id": so.id})

    for i in range(multiprocessing.cpu_count()):
        worker = threading.Thread(target=NINArequestHelper,
                                  args=(jobQueue, resultQueue))
        worker.start()
    #NINArequestHelper(jobQueue,resultQueue)
    jobQueue.join()

    # Alte Verknüpfungen löschen
    #db.session.execute(districtHasGroup.__table__.update().where(and_(districtHasGroup.autolinked == True, districtHasGroup.source_id == so.id)).values(is_deleted=True))
    db.session.execute(districtHasGroup.__table__.delete().where(
        and_(districtHasGroup.autolinked == True,
             districtHasGroup.source_id == so.id)))
    db.session.execute(
        links.__table__.delete().where(links.source_id == so.id))
    db.session.flush()
    print("Schreibe Änderungen...")
    tourismusSourceID = createSource("https://tourismus-wegweiser.de/json").id
    for qi in resultQueue.queue:
        for measure in qi["measures"]:
            if measure["source"] in ["LAND", "BUND"]:
                #makeMeasure(measure["mkdown"], region_id=qi["region_id"], title=measure["title"], source=source_text, isOFP=True)
                # Maßnahmen werden für jeden District gespeichert, um den code zu vereinfachen.
                makeMeasure(measure["mkdown"],
                            district_id=qi["district_id"],
                            title=measure["title"],
                            source=source_text,
                            isOFP=True)
            elif measure["source"] == "KREIS":
                g = makeMeasure(measure["mkdown"],
                                district_id=qi["district_id"],
                                title=measure["title"],
                                source=source_text,
                                isOFP=True,
                                subtitle="Kreisverordnung")
                # Bei Maßnahmen, die nur Kreisweit gelten, landesweite Tourismus-Wegweiser-Maßnahmen ersetzen.
                titles = []
                if measure["title"] == "Kontaktbestimmungen":
                    titles = ["Kontaktbeschränkungen"]
                elif measure["title"] == "Private Feiern":
                    titles = [
                        "Private Hochzeits-, Geburtstags- oder sonstige Familienfeiern"
                    ]
                elif measure["title"] == "Öffentliche Veranstaltungen":
                    titles = [
                        "Großveranstaltungen und Events (Kultur und Sport)",
                        "Messen und Kongresse (öffentlich)"
                    ]
                elif measure["title"] == "Gaststätten":
                    titles = [
                        "Restaurants, Cafés und Gaststätten (indoor)",
                        "Biergärten und Außengastronomie",
                        "Bars, Pubs und Kneipen", "Discotheken und Clubs"
                    ]
                elif measure["title"] == "Geschäfte":
                    titles = ["Einzelhandel"]
                if titles not in [[], None]:
                    values = {
                        "region_id": qi["region_id"],
                        "source_id": tourismusSourceID
                    }
                    GroupCondition = "SELECT count(rhg.region_id) from regionHasGroup rhg where rhg.region_id=:region_id AND rhg.autolinked = 1 AND rhg.source_id=:source_id and rhg.is_deleted=0 AND rhg.displayGroup_id = dg.id"
                    TitleList = ""
                    # Workaround to use SQL-IN_Statement with prepared statements
                    for i in range(len(titles)):
                        TitleList += ":title" + str(i) + ", "
                        values["title" + str(i)] = titles[i]
                    TitleList = TitleList[:-2]
                    TitleCondition = "SELECT count(dghd.display_id) from displayGroupHasDisplay dghd where dghd.displayGroup_id = dg.id and dghd.display_id in (SELECT d.id FROM display d WHERE d.title_de IN(" + TitleList + "))"
                    ReplacementRequest = "SELECT dg.id FROM displayGroup dg WHERE dg.is_default = 1 AND (" + GroupCondition + ") AND (" + TitleCondition + ")"
                    rows = db.session.execute(ReplacementRequest, values)
                    for row in rows:
                        g.overwrites.append(displayGroup.query.get(row[0]))
            else:
                # Dieser Fall sollte garnicht erst eintreten...
                print("WARNING:", "Unknown NINA-Source occured", measure)
                makeMeasure(measure["mkdown"],
                            district_id=qi["district_id"],
                            title=measure["title"],
                            source=source_text,
                            isOFP=True)
        for link in qi["links"]:
            l = links(link["title"], link["href"], qi["district_id"], so.id)
            db.session.add(l)
        # set last Modified
        if qi["lastModified"] != None:
            db.session.execute(districts.__table__.update().where(
                districts.id == qi["district_id"]).values(
                    lastModified=qi["lastModified"]))
        # Prepare to send out push-Notifications
        #if qi["pushReasons"] != [] and do_push:
        #    push(qi["district_id"], qi["pushReasons"])
        #TODO: Do this in threads
    db.session.flush()