Пример #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
Пример #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
Пример #3
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
Пример #4
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