aboutsummaryrefslogtreecommitdiff
path: root/src/lb_app/app_db/user.py
blob: 7aeab7cb9d8c93b45aaa8908bb09b41485ae20e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import current_app

from .db import MongoDB

class User(object):
    def __init__(self, app=None):
        self.app = app

    def all(self):
        db = MongoDB(self.app)
        mongo = db.connection()
        return list(mongo.db.users.find({}))

    def register(self, username):
        db = MongoDB(self.app)
        mongo = db.connection()
        error = None

        if not username:
            error = 'Username is required.'
        elif len(list(mongo.db.users.find({"username": username}))) > 0:
            error = 'User {} is already registered.'.format(username)

        if error is None:
            mongo.db.users.insert({"username": username})
            return None
        return error

    def remove(self, username):
        db = MongoDB(self.app)
        mongo = db.connection()
        error = None

        if not username:
            error = 'Username is required.'
        elif len(list(mongo.db.users.find({"username": username}))) == 0:
            error = "User {} not exist. So it's good.".format(username)

        if error is None:
            mongo.db.users.remove({"username": username})
            return None
        return error