Ejemplo n.º 1
0
def select_witcher(cursor, params):
    cursor.execute(
        "select * from Client where id_profile=(select id_profile from Token_Table where token='{}')"
        .format(params[User.Token.value]))
    row = cursor.fetchone()
    status = Object()

    if row is not None:
        cursor.execute("select id from Witcher where id_profile={}".format(
            params[Witcher.IDpost.value]))
        id_witcher = cursor.fetchone()[0]
        cursor.execute(
            "update Contract set id_witcher={}, status=1 where id={}".format(
                id_witcher, params[Advert.IDpost]))

        cursor.execute("select header from Contract where id={}".format(
            params[Advert.IDpost]))
        cont = cursor.fetchone()[0]
        title = 'Вы выбраны для выполнения контракта!'
        body = 'Контракт ' + cont
        send_firebase_push(cursor, body, title, params[Witcher.IDpost.value])

        status.status = Status.Ok.value
        status.message = EventWitcher.Success.value
    else:
        status.status = Status.Ok.value
        status.message = EventWitcher.WitcherSelect.value

    return status.toJSON()
Ejemplo n.º 2
0
def registration(cursor, params):
    cursor.execute("select * from Authorization_info where login='******'".format(
        params[User.Login.value]))
    row = cursor.fetchone()

    obj = Object()
    status = Object()

    if row is None:
        status.status = Status.Ok.value
        status.message = EventRegistration.SuccessRegistration.value

        cursor.execute(
            "insert into Authorization_info (login, password, phone_number) values('{}', '{}', '{}')"
            .format(params[User.Login.value], params[User.Password.value],
                    params[User.Phone.value]))
        cursor.execute("select max(id) from Authorization_info")
        row = cursor.fetchone()
        id_auth = row[0]

        cursor.execute("insert into List_Comments (empty) values(null)")
        cursor.execute("select max(id) from List_Comments")
        row = cursor.fetchone()
        id_lcomment = row[0]

        cursor.execute(
            "insert into Photo (id_list_photos, photo) values(null, '{}')".
            format(defaultPhotoBase64))
        cursor.execute("select max(id) from Photo")
        id_photo = cursor.fetchone()[0]

        cursor.execute(
            "insert into Profile (id_authorization_info, id_list_comments, id_photo, name, about) \
                        values({}, {}, {}, null, null)".format(
                id_auth, id_lcomment, id_photo))

        cursor.execute("select max(id) from Profile;")
        row = cursor.fetchone()
        id_prof = row[0]

        if params.get(User.IsWitcher.value, None) == 1:
            cursor.execute(
                "insert into Witcher (id_profile) values({})".format(id_prof))
            obj.is_witcher = True
        else:
            cursor.execute(
                "insert into Client (id_profile) values({})".format(id_prof))
            obj.is_witcher = False
        status.object = obj
    else:
        status.status = Status.Error.value
        status.message = EventRegistration.LoginExist.value

    return status.toJSON()
Ejemplo n.º 3
0
def create_advert(cursor, params):
    cursor.execute(
        "select * from Client where id_profile=(select id_profile from Token_Table where token='{}')"
        .format(params[User.Token.value]))
    row = cursor.fetchone()

    obj = Object()
    status = Object()

    if row is not None:
        id_client = row[0]

        cursor.execute("insert into List_Comments (empty) values(null)")
        cursor.execute("select max(id) from List_Comments")
        id_lcomment = cursor.fetchone()[0]

        cursor.execute("insert into List_Photos (empty) values(null)")
        cursor.execute("select max(id) from List_Photos")
        id_lphoto = cursor.fetchone()[0]

        if params.get(Advert.Photo.value, None) is not None:
            lst = params[Advert.Photo.value]
            for photo in lst:
                cursor.execute(
                    "insert into Photo (id_list_photos, photo) values({}, '{}')"
                    .format(id_lphoto, photo))

        cur_time = int(time.time())
        cursor.execute(
            "insert into Contract (id_witcher, id_client, id_list_comments, id_task_located, \
                        id_list_photos, text, bounty, status, last_update_status, last_update, header) \
                         values(null, {}, {}, {}, {}, N'{}', {}, {}, {}, {}, N'{}')"
            .format(id_client, id_lcomment, params[Advert.TaskLocated.value],
                    id_lphoto, params[Advert.Text.value],
                    params[Advert.Bounty.value], 0, cur_time, cur_time,
                    params[Advert.Header.value]))

        status.status = Status.Ok.value
        status.message = EventAdvert.Success.value
        cursor.execute("select max(id) from Contract")
        obj.id_advert = cursor.fetchone()[0]

    else:
        status.status = Status.Error.value
        status.message = EventAdvert.WitcherCreator.value

    status.object = obj
    return status.toJSON()
Ejemplo n.º 4
0
def get_list_comment(cursor, params):
    sel = "Contract"
    if params.get(Comments.Type.value, None) == Comments.Profile.value:
        sel = "Profile"
    cursor.execute("select text, create_date from Comment where \
                    id_list_comment=(select id_list_comments from {} where id={})"
                   .format(sel, params[Profile.ID.value]))
    row = cursor.fetchall()

    status, obj = Object(), Object()
    status.status = Status.Ok.value
    status.message = EventComments.Success.value
    obj.id_comments = {}
    for i in row:
        comm = Object()
        comm.text = i[0]
        comm.date = i[1]

        #cursor.execute("select a.id, a.name, b.photo from Profile as a left join Photo as b on a.id_photo = b.id \
        #                where a.id_list_comments=(select id_list_comment from Comment where id={})".format(i[2]))
        #prof = cursor.fetchone()
        #comm.id_prof = prof[0]
        #comm.name = prof[1]
        #comm.photo = prof[2]
        obj.id_comments[len(obj.id_comments)] = comm

    status.object = obj
    return status.toJSON()
Ejemplo n.º 5
0
def authorization(cursor, params):
    cursor.execute("select * from Authorization_info where login='******'".format(params[User.Login.value]))
    row = cursor.fetchone()

    status = Object()
    obj = Object()
    status.status = Status.Error.value

    if row is None:
        status.message = EventAuth.LoginNotExist.value
    elif row[2] == params[User.Password.value]:
        id_auth = row[0]
        obj.phone_number = '+' + row[3][1:]
        status.message = EventAuth.SuccessAuthorizaion.value
        status.status = Status.Ok.value
        #tok = params[User.Login.value] + str(time.time())
        #obj.token = md5(tok.encode('utf-8')).hexdigest()

        cursor.execute("select id from Profile where id_authorization_info={}".format(id_auth))
        row = cursor.fetchone()
        obj.id_profile = row[0]
        #cursor.execute("insert into Token_Table (token, last_update, id_profile) values('{}', {}, {})"
        #               .format(obj.token, int(time.time()), obj.id_profile))

        #  Вставка fcm_tokena
        cursor.execute("select * from FCM_Token where id_profile = {}".format(obj.id_profile))
        fcm = cursor.fetchone()
        if fcm is not None:
            cursor.execute("delete FCM_Token where id_profile = {}".format(obj.id_profile))
        cursor.execute("insert into FCM_Token (id_profile, fcm_token) values( {}, '{}')"
                       .format(obj.id_profile, params[User.FCM_Token.value]))

        cursor.execute("select * from Witcher where id_profile={}".format(obj.id_profile))
        row = cursor.fetchone()
        if row is not None:
            obj.type = 0
        else:
            obj.type = 1

        status.object = obj
    else:
        status.message = EventAuth.PasswordIncorrect.value

    return status.toJSON()
Ejemplo n.º 6
0
def delete_advert(cursor, params):
    cursor.execute(
        "select * from Client where id_profile=(select id_profile from Token_Table where token='{}')"
        .format(params[User.Token.value]))
    row = cursor.fetchone()

    status = Object()

    if row is not None:
        id_client = row[0]
        cursor.execute(
            "select id_client, header from Contract where id={}".format(
                params[Advert.ID.value]))
        row = cursor.fetchone()
        id_cont_client = row[0]
        cont = row[1]

        if id_client == id_cont_client:
            cursor.execute("delete Contract where id={}".format(
                params[Advert.ID.value]))
            status.status = Status.Ok.value
            status.message = EventAdvert.Success.value

            cursor.execute(
                "select a.id_profile from Witcher as a inner join Desired_Contract as b on \
                            a.id=b.id_witcher where id_contract={}".format(
                    params[Advert.ID.value]))

            row = cursor.fetchall()
            title = 'Наниматель удалил контракт'
            body = 'Контракт ' + cont

            for i in row:
                send_firebase_push(cursor, body, title, i[0])
        else:
            status.status = Status.Error.value
            status.message = EventAdvert.AlienDelete.value
    else:
        status.status = Status.Error.value
        status.message = EventAdvert.WitcherCreator.value

    return status.toJSON()
Ejemplo n.º 7
0
def add_witcher_in_contract(cursor, params):
    cursor.execute(
        "select * from Witcher where id_profile=(select id_profile from Token_Table where token='{}')"
        .format(params[User.Token.value]))
    row = cursor.fetchone()

    status = Object()

    if row is not None:
        id_witcher = row[0]
        status.status = Status.Ok.value
        status.message = EventAdvert.Success.value

        cursor.execute(
            "insert into Desired_Contract (id_witcher, id_contract) values({}, {})"
            .format(id_witcher, params[Advert.ID.value]))
    else:
        status.status = Status.Error.value
        status.message = EventAdvert.ClientAsWitcher.value

    return status.toJSON()
Ejemplo n.º 8
0
def authorization(cursor, params):
    cursor.execute("select * from Authorization_info where login='******'".format(params[User.Login.value]))
    row = cursor.fetchone()

    status = Object()
    obj = Object()
    status.status = Status.Error.value

    if row is None:
        obj.message = EventAuth.LoginNotExist.value
    elif row[2] == params[User.Password.value]:
        obj.message = EventAuth.SuccessAuthorizaion.value
        status.status = Status.Ok.value
        tok = params[User.Login.value] + time.time().__str__()
        obj.token = md5(tok.encode('utf-8')).hexdigest()
        cursor.execute("insert into Token_Table (token, last_update) values('{}', {})".format(obj.token, time.time().__int__()))
    else:
        obj.message = EventAuth.PasswordIncorrect.value

    status.object = obj
    return status.toJSON()
Ejemplo n.º 9
0
def answer_witcher(cursor, params):
    cursor.execute(
        "select * from Witcher where id_profile=(select id_profile from Token_Table where token='{}')"
        .format(params[User.Token.value]))
    row = cursor.fetchone()
    status = Object()

    if row is not None:
        id_answer = params[Witcher.Status.value]
        cursor.execute(
            "select id_client, header from Contract where id={}".format(
                params[Advert.IDpost]))
        row = cursor.fetchone()
        id_sender = row[0]
        cont = row[1]
        cursor.execute(
            "select name from Profile where id=(select id_profile from Token_Table where token='{}')"
            .format(params[User.Token.value]))
        name = cursor.fetchone()[0]
        body = 'Контракт ' + cont

        if id_answer == 2:
            cursor.execute("update Contract set status=2 where id={}".format(
                params[Advert.IDpost]))
            title = 'Ведьмак ' + name + ' взялся за контракт!'
        else:
            cursor.execute(
                "update Contract set id_witcher=null, status=0 where id={}".
                format(params[Advert.IDpost]))
            title = 'Ведьмак ' + name + ' отказался от контракта!'

        send_firebase_push(cursor, body, title, id_sender)

        status.status = Status.Ok.value
        status.message = EventWitcher.Success.value
    else:
        status.status = Status.Error.value
        status.message = EventWitcher.ClientSelect.value

    return status.toJSON()
Ejemplo n.º 10
0
def refuse_contract(cursor, params):
    # создать копию объявления в зависимости от отказа и выставить статус
    st = 4
    cursor.execute(
        "select * from Client where id_profile=(select id_profile from Token_Table where token='{}')"
        .format(params[User.Token.value]))
    row = cursor.fetchone()
    if row is None:
        cursor.execute("select * from ")
        #token
    else:
        st = 5

    status = Object()
    cursor.execute(
        "update Contract set id_witcher=null, status=0 where id={}".format(
            params[Advert.IDpost]))
    status.status = Status.Ok.value
    status.message = EventWitcher.Success.value

    return status.toJSON()
Ejemplo n.º 11
0
def get_profile_desired_contract(cursor, params):
    cursor.execute(
        "select d.id, d.name from Contract as a inner join Desired_Contract as b on a.id=b.id_contract \
                    inner join Witcher as c on b.id_witcher=c.id inner \
                    join Profile as d on c.id_profile=d.id where a.id={}".
        format(params[Advert.ID.value]))
    rows = cursor.fetchall()

    status = Object()
    obj = Object()

    status.status = Status.Ok.value
    status.message = EventAdvert.Success.value
    obj.witchers = Object()
    obj.witchers.witcher = {}
    obj.witchers.count = len(rows)
    for prof in rows:
        profile = Object()
        profile.id = prof[0]
        profile.name = prof[1]
        obj.witchers.witcher[len(obj.witchers.witcher)] = profile

    status.object = obj
    return status.toJSON()
Ejemplo n.º 12
0
def edit_advert(cursor, params):
    cursor.execute(
        "select * from Client where id_profile=(select id_profile from Token_Table where token='{}')"
        .format(params[User.Token.value]))
    row = cursor.fetchone()

    status = Object()

    if row is not None:
        cursor.execute("select * from Contract where id={}".format(
            params[Advert.ID.value]))
        row = cursor.fetchone()
        update_string = ""
        last_update = int(time.time())
        id_lphoto = row[5]
        cont = row[11]

        if params.get(Advert.Witcher.value, None) is not None:
            update_string += "id_witcher={},".format(
                params[Advert.Witcher.value])
        if params.get(Advert.TaskLocated.value, None) is not None:
            update_string += "id_task_located={},".format(
                params[Advert.TaskLocated.value])
        if params.get(Advert.Text.value, None) is not None:
            update_string += "text=N'{}',".format(params[Advert.Text.value])
        if params.get(Advert.Bounty.value, None) is not None:
            update_string += "bounty={},".format(params[Advert.Bounty.value])
        if params.get(Advert.Status.value, None) is not None:
            update_string += "status={},".format(params[Advert.Status.value])
            update_string += "last_update_status={},".format(last_update)
        if params.get(Advert.Header.value, None) is not None:
            update_string += "header=N'{}',".format(
                params[Advert.Header.value])
        update_string += "last_update={}".format(last_update)

        if params.get(Advert.DelPhoto.value, None) is not None:
            lst = params[Advert.DelPhoto.value]
            for photo in lst:
                cursor.execute("delete Photo where token='{}'".format(photo))

        if params.get(Advert.NewPhoto.value, None) is not None:
            lst = params[Advert.NewPhoto.value]
            for photo in lst:
                cursor.execute(
                    "insert into Photo (id_list_photos, photo) values({}, '{}')"
                    .format(id_lphoto, photo))

        cursor.execute("update Contract set {} where id={}".format(
            update_string, params[Advert.ID.value]))

        cursor.execute(
            "select a.id_profile from Witcher as a inner join Desired_Contract as b on \
                        a.id=b.id_witcher where id_contract={}".format(
                params[Advert.ID.value]))
        row = cursor.fetchall()
        title = 'Наниматель изменил контракт'
        body = 'Контракт ' + cont
        for i in row:
            send_firebase_push(cursor, body, title, i[0])

        status.status = Status.Ok.value
        status.message = EventAdvert.Success.value
    else:
        status.status = Status.Error.value
        status.message = EventAdvert.WitcherCreator.value

    return status.toJSON()
Ejemplo n.º 13
0
def get_advert(cursor, params):
    cursor.execute("select * from Contract where id={}".format(
        params[Advert.ID.value]))
    status = Object()
    obj = Object()

    row = cursor.fetchone()
    obj.id = row[0]
    obj.text = row[6]
    obj.bounty = row[7]
    obj.status = row[8]
    obj.last_update_status = row[9]
    obj.last_update = row[10]
    obj.header = row[11]
    id_witcher = row[1]
    id_client = row[2]
    id_list_comments = row[3]
    id_task_located = row[4]
    id_list_photos = row[5]

    if id_witcher is not None:
        cursor.execute(
            "select id, name from Profile where id=(select id_profile from Witcher where id={})"
            .format(id_witcher))
        row = cursor.fetchone()
        witcher = Object()
        witcher.id = row[0]
        witcher.name = row[1]
        status.witcher = witcher

    cursor.execute(
        "select id, name, id_photo from Profile where id=(select id_profile from Client where id={})"
        .format(id_client))
    row = cursor.fetchone()
    client = Object()
    client.id = row[0]
    client.name = row[1]
    if row[2] is not None:
        cursor.execute("select photo from Photo where id={}".format(row[2]))
        client.photo = cursor.fetchone()[0]
    status.client = client

    cursor.execute(
        "select a.name, b.name from Town as a inner join Kingdom as b on a.id_kingdom=b.id where a.id={}"
        .format(id_task_located))
    row = cursor.fetchone()
    obj.town = row[0]
    obj.kingdom = row[1]

    cursor.execute(
        "select b.photo from Contract as a inner join Photo as b on a.id_list_photos = b.id_list_photos where \
        a.id_list_photos={}".format(id_list_photos))
    row = cursor.fetchall()
    obj.photoContract = Object()
    obj.photoContract.photo = {}
    obj.photoContract.count = len(row)
    for i in range(len(row)):
        ph = Object()
        ph.photo = row[i][0]
        obj.photoContract.photo[len(obj.photoContract.photo)] = ph

    status.object = obj
    status.status = Status.Ok.value
    status.message = EventAdvert.Success.value
    return status.toJSON()
Ejemplo n.º 14
0
 def error_request(self, message):
     obj = Object()
     obj.status = Status.Error.value
     obj.message = message
     self.wfile.write(str.encode(obj.toJSON()))
Ejemplo n.º 15
0
def list_contract(cursor, params, **kwargs):
    obj = Object()
    status = Object()
    status.status = Status.Error.value
    obj.message = "Неопознанная ошибка в методу Get List Contracts"

    req = 'select * from Contract'
    req += " inner join \
                (select town, kingdom, idT from ((select [Town].name as town, [Town].id_kingdom, \
                [Town].id as idT from [Town] ) as T\
                inner join \
                (select [Kingdom].name as kingdom, [Kingdom].id as idK From [Kingdom]) as K on T.id_kingdom = K.idK) )\
                 as TK on id_task_located = TK.idT"

    if kwargs.get('id_witcher') is not None:
        req += ' inner join Desired_Contract on Desired_Contract.id_contract = Contract.id \
                 where Desired_Contract.id_witcher={}'.format(
            kwargs.get('id_witcher'))
    elif kwargs.get('id_client') is not None:
        req += ' where id_client={}'.format(kwargs.get('id_client'))

    filtr = params.get(Params.Filter.Name)
    if filtr is not None:
        req += " and "
        if filtr == Params.Filter.Bounty:
            min = params.get(Params.Min)
            max = params.get(Params.Max)

            if min is None or max is None:
                status.status = Status.Error.value
                obj.value = "ERROR:{}{}".format(
                    EventGetListContracts.MinParamMiss if min is None else "",
                    EventGetListContracts.MaxParamMiss if max is None else "")
            else:
                req += " Bounty > {} and Bounty < {}".format(min, max)

        elif filtr == Params.Filter.Locate:
            town = params.get(Params.Town)
            kingdom = params.get(Params.Kingdom)

            if town is None and kingdom is None:
                status.status = Status.Error.value
                obj.value = EventGetListContracts.FilterLocateErr.value
            else:
                if town is not None:
                    req += " town = '{}' {} ".format(
                        town, "and " if kingdom is not None else "")

                if kingdom is not None:
                    req += " kingdom = '{}' ".format(kingdom)

    stat = params.get(Params.Status)
    if stat is not None and (kwargs.get('id_witcher') is not None
                             or kwargs.get('id_client') is not None):
        req += " and status={}".format(stat)

    sort = params.get(Params.Sort.Name)

    if sort is not None:
        sort_type_desc = params.get(Params.SortType.Name) is not None \
                         and params.get(Params.SortType.Name) == Params.SortType.Desc

        req += " order by"
        if sort == Params.Sort.Alph:
            req += " header"
        elif sort == Params.Sort.Locate:
            req += " kingdom {}, town".format("desc" if sort_type_desc else "")
        elif sort == Params.Sort.LastUpdate:
            req += " last_update"
        else:
            status.status = Status.Error.value
            obj.value = EventGetListContracts.SortErr.value

        if sort_type_desc:
            req += " desc"

    cursor.execute(req)
    row = cursor.fetchall()
    headers = tuple(cursor.description)

    obj.contracts = {}
    for N, i in enumerate(row):

        line = Object()
        for n, head in enumerate(headers):
            setattr(line, head[0], i[n])

        obj.contracts[N] = line

    status.object = obj
    status.status = Status.Ok.value
    obj.message = EventGetListContracts.SuccessGetListContracts.value
    return status.toJSON()