def test_get_active_users(self):
     queries.update_user(User('test2', 'updated', active=False))
     rv = self.app.get('/api/user/active{}'.format(token))
     users = rv.json
     usernames = []
     for user in users:
         usernames.append(user.get('username'))
     self.assertNotIn('test2', usernames)
     self.assertIn('test3', usernames)
示例#2
0
def add_user():
    r = request.get_json()

    if r is None or 'username' not in r or 'name' not in r:
        abort(400)

    username = r['username']
    name = r['name']
    user = User(username, name)
    queries.add_user(user)
    return format_users([user])
示例#3
0
def add_user():
    user_json = request.get_json()

    if user_json is None:
        abort(400)
    if all(k not in user_json.keys() for k in ('username', 'name')):
        abort(400)

    username = user_json['username']
    name = user_json['name']
    user = User(username, name)
    queries.add_user(user)
    return format_users([user])
示例#4
0
def set_up_db():
    app.app_context().push()
    db.init_app(app)
    db.create_all()
    persons = slackbot.get_persons_from_slack()
    for person in persons:
        queries.add_user(
            User(person['username'], person['name'], person['image']))
    threshold1 = Threshold('pizza', 50)
    threshold2 = Threshold('cake', 42)

    queries.add_threshold(threshold1)
    queries.add_threshold(threshold2)
示例#5
0
def update_users():
    with app.app_context():
        users = queries.get_all_users()
        active_users = queries.get_active_users()
        slack_users = slackbot.get_persons_from_slack()
        # update and add users from slack
        for slack_user in slack_users:
            if not any(user.username == slack_user['username']
                       for user in users):
                queries.add_user(
                    User(slack_user['username'], slack_user['name'],
                         slack_user['image']))
            else:
                user = User(slack_user['username'], slack_user['name'],
                            slack_user['image'])
                user.active = True
                queries.update_user(user)
        #set users to inactive if they are not present in slack users
        for user in active_users:
            if not any(user.username == u['username'] for u in slack_users):
                user.active = False
                queries.update_user(user)
 def setUp(self):
     # Create a new database for each test
     app.config[
         'SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres@localhost:5432/test_db'
     app.config['SECRET_KEY'] = 'TEST'
     app.config['TESTING'] = True
     self.app = app.test_client()
     app.app_context().push()
     db.init_app(app)
     db.create_all()
     # Test users
     data = [
         {
             'active': True,
             'name': 'Per Pål',
             'username': '******'
         },
         {
             'active': True,
             'name': 'Per Pål',
             'username': '******'
         },
         {
             'active': True,
             'name': 'Per Pål',
             'username': '******'
         },
         {
             'active': True,
             'name': 'Per Pål',
             'username': '******'
         },
         {
             'active': True,
             'name': 'Per Pål',
             'username': '******'
         },
     ]
     for user in data:
         queries.add_user(User(user['username'], user['name']))
     thresholds = [{
         'threshold': 2,
         'reward_type': 'pizza'
     }, {
         'threshold': 1,
         'reward_type': 'cake'
     }]
     for threshold in thresholds:
         self.app.post('/api/threshold/add{}'.format(token),
                       data=json.dumps(threshold),
                       content_type='application/json')
from backend.DB.api import app, db
from backend.DB.api.tables import Threshold, User, Pair
from backend.DB.api import queries

from backend.slack import slackbot

db.create_all()
db.init_app(app)
persons = slackbot.get_persons_from_slack()
for person in persons:
    queries.add_user(User(person['username'], person['name'], person['image']))
threshold1 = Threshold('pizza', 50)
threshold2 = Threshold('cake', 42)

queries.add_pair(Pair('mleik', 'ohald'))
queries.add_pair(Pair('ohald', 'mleik'))
queries.add_pair(Pair('esog', 'ohald'))
queries.add_pair(Pair('esog', 'ohald'))
queries.add_pair(Pair('esog', 'mleik'))
queries.add_pair(Pair('mleik', 'ohald'))
queries.add_pair(Pair('ohald', 'mleik'))
queries.add_pair(Pair('esog', 'ohald'))
queries.add_pair(Pair('esog', 'ohald'))
queries.add_pair(Pair('esog', 'mleik'))

queries.add_threshold(threshold1)
queries.add_threshold(threshold2)