Пример #1
0
def delete_attendant(user, practice):
    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_practices_ctrl
    practices_ctrl = get_practices_ctrl(
        DB(db_configuration).get_database_connection())
    practices_ctrl.delete_attendant(practice.id, user.id)
    return
Пример #2
0
    def get_events_follows(self, user_id):
        db_configuration = json.loads(open("api/db/db.json").read())
        follow_ctrl = api.db.CtrlFactory.get_follow_ctrl(
            DB(db_configuration).get_database_connection())
        id_colles_follow = follow_ctrl.get_id_followed_colles_by_user(user_id)
        events = []
        for id_colla in id_colles_follow:
            sql = "SELECT * FROM events WHERE id_colla = %s" % id_colla
            cursor = self.cnx.cursor()
            cursor.execute(sql)

            result = cursor.fetchone()
            event = None
            if result is not None:
                event = Event(id=result[0],
                              title=result[1],
                              description=result[2],
                              img=result[3],
                              date=result[4],
                              address=result[5],
                              user_id=result[6],
                              colla_id=result[7])
                events.append(event)

        return events
Пример #3
0
    def testInit(self):
        json_data = json.loads(open("../db.json").read())
        db = DB(json_data)

        self.assertEquals("enfaixappDB_test", db.database)
        self.assertEquals("enfaixapp_server", db.user)
        self.assertEquals("", db.password)
Пример #4
0
def update(colla, body):
    name = body['name']
    uni = body['uni']
    description = body['description']
    phoneNumber = body['phoneNumber']
    email = body['email']
    web = body['web']
    address = body['address']
    color = body['color']
    img = body['img']

    image_name = None
    if img is not None:
        if colla.img is not None and os.path.exists(colla.img):
            os.remove(colla.img)
        image_name = os.path.expanduser("~/images/colles") + "/" + colla.name.lower() + ".png"
        with open(image_name, "wb") as fh:
            fh.write(img.decode('base64'))

    colla.name = name
    colla.uni = uni
    colla.description = description
    colla.phoneNumber = phoneNumber
    colla.email = email
    colla.web = web
    colla.address = address
    colla.color = color
    colla.img = image_name

    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_colla_ctrl
    colla_ctrl = get_colla_ctrl(DB(db_configuration).get_database_connection())
    colla_ctrl.update(colla)
    colla.img = img
    return colla
Пример #5
0
def create_event():
    try:
        body = json.loads(urllib.unquote(request.data))
        title = body['title']
        description = body['description']
        img = body['img']
        image_name = None
        if img is not None:
            image_name = os.path.expanduser(
                "~/images") + "/" + datetime.datetime.today().strftime(
                    "%Y-%m-%d_%H:%M:%S") + ".png"
            with open(image_name, "wb") as fh:
                fh.write(img.decode('base64'))
        date = body['date']
        address = body['address']
        user_id = body['user_id']
        colla_id = body['colla_id']
        db_configuration = json.loads(open("api/db/db.json").read())
        event_ctrl = api.db.CtrlFactory.get_event_ctrl(
            DB(db_configuration).get_database_connection())
        event = Event(title=title,
                      description=description,
                      img=image_name,
                      date=date,
                      address=address,
                      user_id=user_id,
                      colla_id=colla_id)
        event = event_ctrl.insert(event)
        event.img = img
        return make_response(jsonify(event.__dict__), 201)
    except KeyError:
        abort(500)
Пример #6
0
def insert(practice):
    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_practices_ctrl
    practices_ctrl = get_practices_ctrl(
        DB(db_configuration).get_database_connection())
    practice = practices_ctrl.insert(practice)
    return practice
Пример #7
0
def create_token(email, user_id):
    m = hashlib.md5(email)
    token = m.hexdigest()
    db_configuration = json.loads(open("api/db/db.json").read())
    user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection())
    user_ctrl.add_token(user_id, token)
    return token
Пример #8
0
def grant_admin():
    user_id = request.args.get('user_id')
    colla_id = request.args.get('colla_id')
    if colla_id is None or user_id is None:
        abort(400)
    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_user_ctrl
    user = get_user_ctrl(DB(db_configuration).get_database_connection()).get(user_id)
    from api.db.CtrlFactory import get_colla_ctrl
    colla = get_colla_ctrl(DB(db_configuration).get_database_connection()).get(colla_id)
    if colla is None or user is None:
        abort(404)
    try:
        from api.db.CtrlFactory import get_admin_ctrl
        get_admin_ctrl(DB(db_configuration).get_database_connection()).insert(user.id, colla.id)
    except Exception:
        abort(409)
    return make_response(jsonify({}), 202)
Пример #9
0
def get_user(user_id):
    db_configuration = json.loads(open("api/db/db.json").read())
    user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection())
    user = user_ctrl.get(user_id)
    if user is None:
        abort(404)
    else:
        user = user_service.get_all_info(user)
        return make_response(jsonify(user.__dict__), 200)
Пример #10
0
def follow(user_id):
    db_configuration = json.loads(open("api/db/db.json").read())
    user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection())
    user = user_ctrl.get(user_id)
    if user is None:
        abort(404)
    else:
        colla_id = request.args.get('colla_id')
        if colla_id is not None:
            colla = get_colla_ctrl(
                DB(db_configuration).get_database_connection()).get(colla_id)
            if colla:
                user_service.add_following(user.id, colla_id)
                user = user_service.get_all_info(user)
                return make_response(jsonify(user.__dict__), 200)
            else:
                abort(404)
        abort(400)
Пример #11
0
def check_if_new(pub_date):
    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_result_ctrl
    result_ctrl = get_result_ctrl(
        DB(db_configuration).get_database_connection())
    latest_date_in_db = result_ctrl.get_latest_date()
    if latest_date_in_db:
        return datetime.fromtimestamp(mktime(pub_date)) > latest_date_in_db
    else:
        return True
Пример #12
0
def get_results(count=5, offset=0):
    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_result_ctrl
    result_ctrl = get_result_ctrl(
        DB(db_configuration).get_database_connection())
    performances = result_ctrl.get_performances(count=int(count),
                                                offset=int(offset))
    for performance in performances:
        performance.results = result_ctrl.get_results(performance.id)

    return performances
Пример #13
0
def get_some_info_belonging_colles_by_user(user_id):
    db_configuration = json.loads(open("api/db/db.json").read())
    ids_belongs_colles = get_belong_ctrl(
        DB(db_configuration).get_database_connection()
    ).get_id_belonging_colles_by_user(user_id)
    belonging_colles = []
    for id in ids_belongs_colles:
        colla = colla_service.get_some_info_colla(id)
        belonging_colles.append(colla)

    return belonging_colles
Пример #14
0
def delete(event_id):
    db_configuration = json.loads(open("api/db/db.json").read())
    events_ctrl = api.db.CtrlFactory.get_event_ctrl(
        DB(db_configuration).get_database_connection())
    event = events_ctrl.get(event_id)
    if event:
        if event.img is not None and os.path.exists(event.img):
            os.remove(event.img)
        events_ctrl.delete(event)
        return make_response(jsonify({}), 204)
    else:
        abort(404)
Пример #15
0
def sign_in():
    try:
        body = json.loads(urllib.unquote(request.data))
        email = body['email']
        password = body['password']
        name = body['name']
        surname = body['surname']
        colles_that_belongs_to = body['belongs']
        colles_followed = body['follows']
        db_configuration = json.loads(open("api/db/db.json").read())
        user_ctrl = get_user_ctrl(
            DB(db_configuration).get_database_connection())
        new_user = user_ctrl.get_by_email(email)
        if new_user is None:
            new_user = create_user(name=name,
                                   surname=surname,
                                   email=email,
                                   password=password,
                                   belonging_list=colles_that_belongs_to,
                                   following_list=colles_followed)
            token = create_token(email, new_user.id)
            new_user.session_token = token
            new_user.follows = follow_service.get_some_info_followed_colles_by_user(
                new_user.id)
            new_user.belongs = belong_service.get_some_info_belonging_colles_by_user(
                new_user.id)
            new_user.admin = get_admin_ctrl(
                DB(db_configuration).get_database_connection()).is_admin(
                    new_user.id)
            response = make_response(
                json.dumps(new_user,
                           encoding='utf-8',
                           cls=UserEncoder,
                           indent=4), 201)
            response.headers[0] = ('Content-Type',
                                   'application/json; charset=utf-8')
            return response
        abort(409)
    except KeyError:
        abort(500)
Пример #16
0
def can_join(user, colla):
    db_configuration = json.loads(open("api/db/db.json").read())
    belonging_ctrl = get_belonging_ctrl(
        DB(db_configuration).get_database_connection())
    list_colles = belonging_ctrl.get_belonging_colles_by_user(user.id)
    if len(list_colles) == 2:
        return False
    elif len(list_colles) == 1:
        belong_colla = list_colles[0]
        if (belong_colla.uni and colla.uni) or (not belong_colla.uni
                                                and not colla.uni):
            return False
    return True
Пример #17
0
def add_attendant(practice_id):
    user_id = request.args.get('user_id')
    if user_id:
        practice = practice_service.get_practice(practice_id)
        from api.db.CtrlFactory import get_user_ctrl
        db_configuration = json.loads(open("api/db/db.json").read())
        user = get_user_ctrl(DB(db_configuration).get_database_connection()).get(user_id)
        if practice and user:
            practice = practice_service.insert_attendant(user, practice)
            return make_response(jsonify(practice.__dict__), 201)
        else:
            abort(404)
    else:
        abort(400)
Пример #18
0
def get_event(event_id):
    db_configuration = json.loads(open("api/db/db.json").read())
    events_ctrl = api.db.CtrlFactory.get_event_ctrl(
        DB(db_configuration).get_database_connection())
    event = events_ctrl.get(event_id)
    if event is None:
        abort(404)
    else:
        encoded_img = None
        if event.img is not None:
            with open(event.img, "rb") as fh:
                encoded_img = base64.b64encode(fh.read())
        event.img = encoded_img
        return make_response(jsonify(event.__dict__), 200)
Пример #19
0
class TestFollowCtrlMySQL(TestCase):
    def setUp(self):
        self.db_configuration = json.loads(open("../db.json").read())
        self.cnx = DB(self.db_configuration).get_database_connection()

    def tearDown(self):
        self.cnx.rollback()

    def test_get_followed_colles_by_user(self):
        test_user = User(name='Test_name',
                         surname='Test surnames',
                         email='*****@*****.**',
                         password='')
        user_ctrl = get_user_ctrl(self.cnx)
        user_ctrl.insert(test_user)

        colla1 = Colla(name='Test colla 1', uni=0, color='#FFFFFF')
        colla2 = Colla(name='Test colla 2', uni=1, color='#FFFFFF')
        colla3 = Colla(name='Test colla 3', uni=1, color='#FFFFFF')

        colla_ctrl = get_colla_ctrl(self.cnx)
        colla_ctrl.insert(colla1)
        colla_ctrl.insert(colla2)
        colla_ctrl.insert(colla3)

        sql = "INSERT INTO follows(id_user, id_colla) VALUES ('%s','%s')"
        data = [(test_user.id, colla1.id), (test_user.id, colla2.id)]

        self.cnx.cursor().executemany(sql, data)

        following_ctrl = get_following_ctrl(self.cnx)

        following_colles = following_ctrl.get_followed_by_user(test_user.id)

        self.assertEquals(2, len(following_colles))
        self.assertEquals(colla1, following_colles[0])
        self.assertEquals(colla2, following_colles[1])
Пример #20
0
def add_performance(item):
    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_result_ctrl
    result_ctrl = get_result_ctrl(
        DB(db_configuration).get_database_connection())
    pub_date = strptime(item.pubDate.string[:-6], '%a, %d %b %Y %H:%M:%S')
    performance = Performance(title=item.title.string,
                              date=datetime.fromtimestamp(mktime(pub_date)))
    performance = result_ctrl.insert_performance(performance)

    inner_html = BeautifulSoup(item.description.string, 'html.parser')
    rows = inner_html.find('table').find_all('tr')
    colla_name = None
    colla = None
    first = True
    from api.db.CtrlFactory import get_colla_ctrl
    for row in rows:
        if first:
            first = False
        else:
            cells = row.find_all('td')
            name = cells[0].get_text()
            if colla_name != name and name.encode('utf-8') != '':
                colla_name = name
                colla = get_colla_ctrl(
                    DB(db_configuration).get_database_connection()
                ).get_by_name(colla_name)
            if colla:
                ronda = Round(num=cells[1].get_text(),
                              tries=cells[2].get_text(),
                              castell=cells[3].get_text(),
                              result=cells[4].get_text())
                try:
                    result_ctrl.insert_result(performance, colla, ronda)
                except _mysql_exceptions.IntegrityError:
                    pass
    return
Пример #21
0
def get_all_info_colla(colla_id):
    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_colla_ctrl
    colla_ctrl = get_colla_ctrl(DB(db_configuration).get_database_connection())
    colla = colla_ctrl.get_full(colla_id)
    encoded_img = None
    if colla:
        if colla.img is not None:
            try:
                img_path = os.path.expanduser(colla.img)
                with open(img_path, "rb") as fh:
                    encoded_img = base64.b64encode(fh.read())
            except IOError:
                pass
            finally:
                colla.img = encoded_img
    return colla
Пример #22
0
def update_user(user_id):
    db_configuration = json.loads(open("api/db/db.json").read())
    user_ctrl = get_user_ctrl(DB(db_configuration).get_database_connection())
    user = user_ctrl.get(user_id)
    if user is None:
        abort(404)
    else:
        body = json.loads(urllib.unquote(request.data))
        new_password = body['password']
        new_name = body['name']
        new_surname = body['surname']
        user.name = new_name
        user.surname = new_surname
        user.password = new_password
        user_ctrl.update(user)
        user = user_service.get_all_info(user)
        return make_response(jsonify(user.__dict__), 200)
Пример #23
0
def get_all():
    db_configuration = json.loads(open("api/db/db.json").read())
    from api.db.CtrlFactory import get_colla_ctrl
    colla_ctrl = get_colla_ctrl(DB(db_configuration).get_database_connection())
    colles = colla_ctrl.get_all()

    # for colla in colles:
    #     encoded_img = None
    #     if colla.img is not None:
    #         try:
    #             img_path = os.path.expanduser(colla.img)
    #             with open(img_path, "rb") as fh:
    #                 encoded_img = base64.b64encode(fh.read())
    #         except IOError:
    #             pass
    #         finally:
    #             colla.img = encoded_img

    return colles
Пример #24
0
def remove_following(user_id, colla_id):
    db_configuration = json.loads(open("api/db/db.json").read())
    following_ctrl = get_following_ctrl(
        DB(db_configuration).get_database_connection())
    following_ctrl.delete(user_id, colla_id)
    return
Пример #25
0
def remove_belong(user, colla):
    db_configuration = json.loads(open("api/db/db.json").read())
    get_belonging_ctrl(DB(db_configuration).get_database_connection()).delete(
        user.id, colla.id)
    return
Пример #26
0
def add_belonging_colla(user, colla):
    db_configuration = json.loads(open("api/db/db.json").read())
    belonging_ctrl = get_belonging_ctrl(
        DB(db_configuration).get_database_connection())
    belonging_ctrl.insert(user, colla)
    return
Пример #27
0
 def setUp(self):
     self.db_configuration = json.loads(open("../db.json").read())
     self.cnx = DB(self.db_configuration).get_database_connection()
Пример #28
0
    def testGetConnection(self):
        json_data = json.loads(open("../db.json").read())
        cnx = DB(json_data).get_database_connection()

        self.assertIsNotNone(cnx)
Пример #29
0
 def __init__(self, dbconfig):
     self.db = DB.getDatabase(dbconfig.user, dbconfig.password,
                              dbconfig.database_name)
Пример #30
0
class CollaCtrlTest(TestCase):
    def setUp(self):
        self.db_configuration = json.loads(open("../db.json").read())
        self.cnx = DB(self.db_configuration).get_database_connection()

    def tearDown(self):
        self.cnx.rollback()

    def test_insert(self):
        colla = Colla(name="Test_Name", uni=0, color="#123456")

        db_colla = get_colla_ctrl(self.cnx).insert(colla)

        self.assertIsNotNone(db_colla.id)
        self.assertEquals("Test_Name", db_colla.name)
        self.assertFalse(db_colla.uni)
        self.assertEquals("#123456", db_colla.color)

    def test_get_universitaries(self):
        sql = "INSERT INTO colles (name, uni, color, img_path) " \
              "VALUES ('%s','%s','%s','%s')" % ('Test_Name', 1, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        sql = 'INSERT INTO colles (name, uni, color, img_path) ' \
              'VALUES ("%s", "%s", "%s", "%s")' % ('Test_Name2', 1, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        sql = 'INSERT INTO colles (name, uni, color, img_path) ' \
              'VALUES ("%s", "%s", "%s", "%s")' % ('Test_Name3', 0, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        colles = get_colla_ctrl(self.cnx).get_universitaries()

        self.assertIsNotNone(colles)
        self.assertEquals(2, len(colles))

    def test_get_convencionals(self):
        sql = 'INSERT INTO colles (name, uni, color, img_path) ' \
              'VALUES ("%s", "%s", "%s", "%s")' % ('Test_Name', 1, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        sql = 'INSERT INTO colles (name, uni, color, img_path) ' \
              'VALUES ("%s", "%s", "%s", "%s")' % ('Test_Name2', 1, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        sql = 'INSERT INTO colles (name, uni, color, img_path) ' \
              'VALUES ("%s", "%s", "%s", "%s")' % ('Test_Name3', 0, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        colles = get_colla_ctrl(self.cnx).get_convencionals()

        self.assertIsNotNone(colles)
        self.assertEquals(1, len(colles))

    def test_get_all(self):
        sql = 'INSERT INTO colles (name, uni, color, img_path) ' \
              'VALUES ("%s", "%s", "%s", "%s")' % ('Test_Name', 1, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        sql = 'INSERT INTO colles (name, uni, color, img_path) ' \
              'VALUES ("%s", "%s", "%s", "%s")' % ('Test_Name2', 1, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        sql = 'INSERT INTO colles (name, uni, color, img_path) ' \
              'VALUES ("%s", "%s", "%s", "%s")' % ('Test_Name3', 0, 'FFFFFF', '~/TestProjectes')
        cursor = self.cnx.cursor()
        cursor.execute(sql)

        colles = get_colla_ctrl(self.cnx).get_all()

        self.assertIsNotNone(colles)
        self.assertEquals(3, len(colles))