示例#1
0
def register(username, password):
    """Register an account."""
    c = get_config()

    # Limit accounts by email?
    if not domain_allowed(username):
        return resp(error="{} accounts only!".format(
            c.registration.email_domain
        )), BAD_REQUEST

    # Conflict?
    user = g.db.query(User).filter(User.username == username).first()
    if user and user.verified:
        return resp(error="User already exists."), CONFLICT

    # Make their account.
    if not user:
        user = User(username=username, password=hash_pass(password))
        g.db.add(user)
        g.db.commit()

    # Serialize the activation token.
    token = signed_serialize({"t": "activate", "u": user.id})
    print("Activation token:" + token)

    # Email them the token.
    try:
        send_email(username, token)
    except Exception as e:
        print("Couldn't send mail! " + e)

    return resp(message="Account created. Check your email for "
        "activation link."), CREATED
示例#2
0
def delete_train(train_id):
    """Delete a train."""
    train = g.db.query(Train).get(train_id)
    if not train:
        return resp(error="Train not found."), NOT_FOUND

    g.db.delete(train)
    g.db.commit()
    return resp(message="Deleted."), OK
示例#3
0
def leave_train(train_id):
    """Leave the train."""
    train = g.db.query(Train).get(train_id)
    if not train:
        return resp(error="Train not found."), NOT_FOUND

    train.passengers.remove(g.user)
    g.db.commit()

    return resp(message="Left the train."), OK
示例#4
0
def join_train(train_id):
    """Join a train."""
    train = g.db.query(Train).get(train_id)
    if not train:
        return resp(error="Train not found."), NOT_FOUND

    if datetime.utcnow() > train.expires:
        return resp(error="The train has already left the station."), FORBIDDEN

    train.passengers.append(g.user)
    g.db.commit()

    return resp(message="Joined the train."), CREATED
示例#5
0
def update_train(name, expires, train_id):
    """Update an existing train."""
    train = g.db.query(Train).get(train_id)
    if not train:
        return resp(error="Train not found."), NOT_FOUND

    # Turn expiration into a datetime.
    expires = datetime.utcnow() + timedelta(seconds=expires)

    train.name = name
    train.expires = expires

    g.db.commit()
    return resp(train.serialize), OK
示例#6
0
def login(username, password):
    """Log into the app."""

    user = g.db.query(User).filter(User.username == username).first()
    if not user:
        return resp(error="User name not found."), NOT_FOUND
    if not user.verified:
        return resp(error="This account is not verified. Check your email "
            "for the verification link and try again."), FORBIDDEN

    if not check_pass(password, user.password):
        return resp(error="Bad password."), FORBIDDEN

    session["auth"] = True
    session["user_id"] = user.id
    return resp(message="Welcome back."), OK
示例#7
0
def make_train(name, expires):
    """Make a new train."""

    # Turn expiration into a datetime.
    expiration = datetime.utcnow() + timedelta(seconds=expires)

    # Make the train.
    train = Train(
        name=name,
        owner=g.user.id,
        expires=expiration,
    )
    train.passengers.append(g.user) # Creator is automatically on board
    g.db.add(train)
    g.db.commit()

    pingback = url_for("index", _external=True).strip("/") + "/#/?train={}".format(
        train.id
    )

    # Notify Slack
    train_name = name
    if not train_name.lower().endswith("train"):
        train_name += " Train"
    post_message("*A train has pulled into the station!* :train:\n\n"
        "{user} has created the *{name}*! Board the train at <{url}>\n"
        "It leaves the station in *{expires} minute{pl}*!".format(
            user=g.user.username.split("@")[0],
            name=train_name,
            url=pingback,
            expires=int(expires / 60),
            pl="s" if int(expires / 60) != 1 else "",
        )
    )

    return resp(train.serialize), CREATED
示例#8
0
def logout():
    """Sign out."""
    session.pop("auth", None)
    session.pop("user_id", None)
    return resp(message="Bye."), OK
示例#9
0
 def decorated_function(*args, **kwargs):
     if "auth" in session and session["auth"]:
         return f(*args, **kwargs)
     else:
         return resp(error="Auth required.")
示例#10
0
def get_train(train_id):
    train = g.db.query(Train).get(train_id)
    if not train:
        return resp(error="Train not found."), NOT_FOUND
    return resp(train.serialize), OK
示例#11
0
def list_trains():
    trains = g.db.query(Train).filter(Train.expires >= datetime.utcnow()).all()
    return resp(result=[ t.serialize for t in trains ])