Exemple #1
0
def update_location(id):
    data = {
        "name": querystring_get("name"),
        "description": querystring_get("description"),
        "address1": querystring_get("address1"),
        "address2": querystring_get("address2"),
        "address3": querystring_get("address3"),
        "county": querystring_get("county"),
        "postcode": querystring_get("postcode"),
        "contactname": querystring_get("contactname"),
        "contactemail": querystring_get("contactemail"),
        "contactphone1": querystring_get("contactphone1"),
        "contactphone2": querystring_get("contactphone2"),
    }

    if not data["name"]:
        Alert.bad("The <strong>Name</strong> field is required")
        return render_template("location/view.html", **data)

    if not data["address1"]:
        Alert.bad("The <strong>Address 1</strong> field is required")
        return render_template("location/view.html", **data)

    if not data["postcode"]:
        Alert.bad("The <strong>Post Code</strong> field is required")
        return render_template("location/view.html", **data)

    create = str(id).lower() == "create"

    item = None
    if create:
        item = EventLocation()
    else:
        item = api.get(EventLocation, id)
        if not item:
            Alert.bad("Could not find <strong>Location</strong> {}; {}".format(
                id, api.error))
            return render_template("location/view.html", **data)

    address = None
    if create or not item.address:
        address = Address()
    else:
        address = item.address

    address.line1 = data["address1"]
    address.line2 = data["address2"]
    address.line3 = data["address3"]
    address.county = data["county"]
    address.postcode = data["postcode"]

    if create or not item.address:
        address = api.create(Address, address)
        if not address:
            Alert.bad("Failed to create <strong>Address</strong>; {}".format(
                api.error))
            return render_template("location/view.html", **data)
    else:
        address = api.update(Address, address)
        if not address:
            Alert.bad("Failed to update <strong>Address</strong>; {}".format(
                api.error))
            return render_template("location/view.html", **data)

    item.name = data["name"]
    item.description = data["description"]
    item.contact_name = data["contactname"]
    item.contact_email_address = data["contactemail"]
    item.contact_telephone1 = data["contactphone1"]
    item.contact_telephone2 = data["contactphone2"]
    item.address = address

    item = api.update(EventLocation, item)

    if not item:
        Alert.bad("Failed to create <strong>Address</strong>; {}".format(
            api.error))
        return render_template("location/view.html", **data)

    Alert.good("Location <strong>{}</strong> {}".format(
        item.name, "created" if create else "updated"))
    return redirect("/locations/{}".format(item.id))
Exemple #2
0
def update_user(id):
    data = {
        "username": querystring_get("username"),
        "forename": querystring_get("forename"),
        "surname": querystring_get("surname"),
        "email": querystring_get("email"),
        "dob": querystring_get("dob"),
        "address1": querystring_get("address1"),
        "address2": querystring_get("address2"),
        "address3": querystring_get("address3"),
        "county": querystring_get("county"),
        "postcode": querystring_get("postcode")
    }

    if not data["username"]:
        Alert.bad("The <strong>username</strong> field is required")
        return render_template("user/view.html", **data)

    if not data["forename"]:
        Alert.bad("The <strong>forename</strong> field is required")
        return render_template("user/view.html", **data)

    if not data["surname"]:
        Alert.bad("The <strong>surname</strong> field is required")
        return render_template("user/view.html", **data)

    if not data["address1"]:
        Alert.bad("The <strong>Address 1</strong> field is required")
        return render_template("user/view.html", **data)

    if not data["postcode"]:
        Alert.bad("The <strong>Post Code</strong> field is required")
        return render_template("user/view.html", **data)

    create = str(id).lower() == "create"

    item = None
    if create:
        item = User()
    else:
        item = api.get(User, id)
        if not item:
            Alert.bad("Could not find <strong>User</strong> {}; {}".format(
                id, api.error))
            return render_template("user/view.html", **data)

    address = None
    if create or not item.address:
        address = Address()
    else:
        address = item.address

    address.line1 = data["address1"]
    address.line2 = data["address2"]
    address.line3 = data["address3"]
    address.county = data["county"]
    address.postcode = data["postcode"]

    if create or not item.address:
        address = api.create(Address, address)
        if not address:
            Alert.bad("Failed to create <strong>Address</strong>; {}".format(
                api.error))
            return render_template("user/view.html", **data)
    else:
        address = api.update(Address, address)
        if not address:
            Alert.bad("Failed to update <strong>Address</strong>; {}".format(
                api.error))
            return render_template("user/view.html", **data)

    item.username = data["username"]
    item.forename = data["forename"]
    item.surname = data["surname"]
    item.email_address = data["email"]
    item.dob = data["dob"]
    item.address = address
    item = api.update(User, item)

    if not item:
        Alert.bad(api.error)
        return render_template("user/view.html", **data)

    Alert.good("User <strong>{}</strong> {}".format(
        item.name, "created" if create else "updated"))
    return redirect("/users/{}".format(item.id))