Esempio n. 1
0
def register():
    username = request.form['username']
    password = request.form['password']
    email = request.form['email']
    realname = request.form['realname']
    User.add(username, password, email, realname)
    return redirect("/login?created")
Esempio n. 2
0
 def handler(self, data):
     database.connect()
     clientID = data['clientID']
     try:
         client = Client.get(Client.id == clientID)
     except DoesNotExist:
         print("Client does not exist")
         return
     for userID in client.subscriber_list:
         user = User.get(User.id == userID)
         if data['status']:
             email = {
                 "dst":
                 user.email,
                 "subject":
                 "high {}".format(clientID),
                 "msg":
                 "Client {} has high value readings, which indicates bad air quality."
                 .format(clientID)
             }
         else:
             email = {
                 "dst": user.email,
                 "subject": "low {}".format(clientID),
                 "msg":
                 "Client {} readings has gone normal.".format(clientID)
             }
         sendQueue("mail", json.dumps(email))
         email['dst'] = user.phone
         sendQueue("sms", json.dumps(email))
     database.close()
Esempio n. 3
0
def checkLogin():
    if ('tokenKey' not in session) or ('tokenValue' not in session) or (
            'wsTokenKey' not in session) or ('wsTokenValue' not in session):
        g.user = None
        session['previous'] = request.path
    else:
        try:
            g.user = LoginToken.use(session['tokenKey'], session['tokenValue'])
            wsUser = WebsocketToken.use(session['wsTokenKey'],
                                        session['wsTokenValue'])
        except (InvalidToken):
            User.logout()
            session['previous'] = request.path
            return
        if wsUser != g.user:
            User.logout()
            session['previous'] = request.path
            return
        g.wsTokenKey = session['wsTokenKey']
        g.wsTokenValue = session['wsTokenValue']
Esempio n. 4
0
def resetPassword():
    email = request.form['email']
    try:
        user = User.get(User.email == email)
    except DoesNotExist:
        return json.jsonify(result="not found")
    ft_obj = ForgotToken.new(user_id=user.id)
    mailContent = render_template("authentication/forgotEmail.html",
                                  token=ft_obj.token,
                                  expiration=ft_obj.timestamp.isoformat())
    async_ops.sendMail(email, "MakerWeek reset your password", mailContent)
    return json.jsonify(result="success")
Esempio n. 5
0
def login():
    username = request.form['username']
    password = request.form['password']
    try:
        user = User.get(User.username == username)
    except DoesNotExist:
        return redirect("/login?failure")
    try:
        session['tokenKey'], session['tokenValue'] = user.login(password)
    except (DoesNotExist, IncorrectPassword):
        return redirect("/login?failure")
    session['wsTokenKey'], session['wsTokenValue'] = WebsocketToken.new(user)
    if 'remember-me' in request.form:
        session.permanent = True
    if 'previous' in session:
        return redirect(session['previous'])
    else:
        return redirect("/")
Esempio n. 6
0
def getUserClients():
    username = request.args['username']
    try:
        page = int(request.args['page'])
    except Exception:
        page = 1
    try:
        user = User.get(User.username == username)
    except DoesNotExist:
        return "user not found", 404
    query = (LastEvent.select(LastEvent, Client, User).join(
        Client, on=(LastEvent.client_id == Client.id)).join(
            User,
            on=(Client.owner == User.id)).where(Client.owner == user).paginate(
                page, 10))
    if (g.user is None) or (g.user != user):
        query = (query.where(~Client.private))
    return json.jsonify([
        q.event_id.toFrontendObject(include_id=True,
                                    include_geo=True,
                                    include_owner=True) for q in query
    ])
Esempio n. 7
0
def userPage(username):
    try:
        user = User.get(User.username == username)
    except DoesNotExist:
        return "no such user", 404
    return render_template("user.html", user=user)
Esempio n. 8
0
def navbarSearch():
    type = request.args['type']
    keywords = request.args['q']
    if 'page' in request.args:
        page = int(request.args['page'])
    else:
        page = 1
    if type == "Tags":
        query = (Tags.select(
            Tags.title, Tags.description,
            fn.COUNT(SQL("client_id_id")).alias("count")).join(TagsMap).where(
                SQL("MATCH(title) AGAINST(%s IN BOOLEAN MODE)", (keywords, ))
                | SQL("MATCH(description) AGAINST(%s IN BOOLEAN MODE)",
                      (keywords, ))).group_by(TagsMap.tag_id).paginate(
                          page, 12).dicts())
        return json.jsonify([{
            "title": tag['title'],
            "description": tag['description'],
            "count": tag['count']
        } for tag in query])
    elif type == "Users":
        query = (User.select().where(
            SQL("MATCH(username) AGAINST(%s IN BOOLEAN MODE)", (keywords, ))
            | SQL("MATCH(realname) AGAINST(%s IN BOOLEAN MODE)",
                  (keywords, ))).paginate(page, 10))
        return json.jsonify([{
            "username":
            user.username,
            "realname":
            user.realname,
            "avatar":
            url_for("static", filename=os.path.join("avatar", user.avatar))
        } for user in query])
    elif type == "Clients":
        tags = []
        names = []
        for keyword in keywords.split(" "):
            if keyword.startswith("tags:"):
                tags.append(keyword[5:])
            else:
                names.append(keyword)
        names[-1] += '*'

        query = (LastEvent.select(LastEvent, Client, User, TagsMap, Tags).join(
            Client, on=(LastEvent.client_id == Client.id)).join(
                User, on=(Client.owner == User.id)).join(
                    TagsMap, on=(TagsMap.client_id == Client.id)).join(
                        Tags, on=(Tags.id == TagsMap.tag_id)).where(
                            SQL("MATCH(name) AGAINST(%s IN BOOLEAN MODE)",
                                (" ".join(names), ))).group_by(Client.id))
        if len(tags) != 0:
            query = (query.where(
                SQL("MATCH(title) AGAINST(%s IN BOOLEAN MODE)",
                    (" ".join(tags), ))))
        if g.user is None:
            query = (query.where(~Client.private))
        else:
            query = (
                query.where(~Client.private
                            | (Client.private & (Client.owner == g.user))))
        query = query.paginate(page, 10)
        return json.jsonify([
            q.event_id.toFrontendObject(include_id=True,
                                        include_geo=True,
                                        include_owner=True) for q in query
        ])
Esempio n. 9
0
def signOut():
    User.logout()
    return redirect("/")