Ejemplo n.º 1
0
async def createUser(request):

    if request.method == "POST":

        result = {}
        form = UserForm(request)
        #         print("validate form", form.validate())
        #         print("errors", form.errors)
        #         print("form", dir("form"))

        dSession = getSession()

        # Make this faster later
        unameResult = dSession.query(
            User.id).filter(User.username == form.data["username"]).scalar()
        emailResult = dSession.query(
            User.id).filter(User.email == form.data["email"]).scalar()
        errors = []
        if unameResult:
            errors.append({"msg": "This user already exists"})
        if emailResult:
            errors.append({"msg": "This email already exists"})

        if errors:
            result["errors"] = errors
            result["ok"] = False
        else:
            createUser_(form.data, dSession)
            result["ok"] = True

        return json(result)

    else:
        form = UserForm(request)
        return json({"csrf_token": form.csrf_token._value()})
Ejemplo n.º 2
0
def userExists(info, dSession=None):

    dSession = getSession(session=dSession)
    uid = dSession.query(User.id).filter(or_(User.username == info,
                                             User.email == info)).\
        scalar()

    return True if uid else False
Ejemplo n.º 3
0
def deleteUser(info, dSession=None):
    """
    deletes a user
    """
    dSession = getSession(session=dSession)
    query = User.__table__.delete().where(
        or_(User.username == info, User.email == info))
    dSession.execute(query)
    dSession.commit()
Ejemplo n.º 4
0
def checkCredentials(username, password, dSession=None):
    """
    if the authentication info is ok
    then sets the authentication to authenticated
    returns the user.id if the authentication request
    was ok
    """

    dSession = dSession or getSession()
    user = dSession.query(User).filter(User.username == username).one_or_none()
    if user and user.password == password:
        return user.id
    else:
        return None
Ejemplo n.º 5
0
def createUser(data, dSession=None):
    """
    creates a user
    """
    dSession = getSession(session=dSession)
    bulkInsert(dSession, User, [data])