Example #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()
Example #2
0
def contract_complited(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:
        cursor.execute('update Contract set status=3 where id={}'.format(
            params[Advert.ID.value]))
        status.status = Status.Ok.value

        cursor.execute(
            'select id_client, header from Contract where id={}'.format(
                params[Advert.ID.value]))
        row = cursor.fetchone()
        id_client = row[0]
        title = 'Контракт ' + 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 = 'Ведьмак ' + name + ' выполнил выше задание!'
        cursor.execute(
            "select id_profile from Client where id={}".format(id_client))
        row = cursor.fetchone()
        id_sender = row[0]
        send_firebase_push(cursor, title, body, id_sender)
    else:
        status.status = Status.Error.value

    return status.toJSON()
Example #3
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()
Example #4
0
def check_phone(cursor, params):
    cursor.execute(
        "select id from Authorization_info where phone_number='{}'".format(
            params["phone_number"]))
    row = cursor.fetchone()
    status = Object()
    if row is None:
        status.status = Status.Ok.value
    else:
        status.status = Status.Error.value

    return status.toJSON()
Example #5
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()
Example #6
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()
Example #7
0
def set_token(cursor, params):
    cursor.execute("select id from Profile where id_authorization_info=(select id from Authorization_info \
                    where phone_number='{}')".format(params["phone_number"]))
    id_prof = cursor.fetchone()[0]
    cursor.execute("insert into Token_Table(token, last_update, id_profile) values('{}', {}, {})"
                   .format(params[User.Token.value], int(time.time()), id_prof))
    status = Object()
    status.status = Status.Ok.value
    return status.toJSON()
Example #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:
        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()
Example #9
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()
Example #10
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()
Example #11
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()
Example #12
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()
Example #13
0
def write_comment_contract(cursor, params):
    cursor.execute("select id_list_comments from Contract where id={}".format(
        params[Advert.ID.value]))
    id_lcomment = cursor.fetchone()[0]

    cursor.execute(
        "insert into Comment (id_list_comment, text, create_date) values({}, N'{}', {})"
        .format(id_lcomment, params[Comment.TextComment.value],
                int(time.time())))

    status = Object()
    status.status = Status.Ok.value

    return status.toJSON()
Example #14
0
def exit_profile(cursor, params):
    status = Object()
    status.status = Status.Ok.value
    cursor.execute(
        "select id_profile from Token_Table where token='{}'".format(
            params[User.Token.value]))
    row = cursor.fetchone()
    if row is not None:
        id_profile = row[0]
        cursor.execute(
            "delete Token_Table where id_profile={}".format(id_profile))
        cursor.execute(
            "delete FCM_Token where id_profile={}".format(id_profile))

    return status.toJSON()
Example #15
0
def write_comment_profile(cursor, params):
    #  cursor.execute("select id_profile from Token_Table where token='{}'".format(params[User.Token.value]))
    #  id_sender = cursor.fetchone()[0]
    cursor.execute("select id_list_comments from Profile where id={}".format(
        params["id"]))
    id_lcomment = cursor.fetchone()[0]

    cursor.execute(
        "insert into Comment (id_list_comment, text, create_date) values({}, N'{}', {})"
        .format(id_lcomment, params[Comment.TextComment.value],
                int(time.time())))

    status = Object()
    status.status = Status.Ok.value

    return status.toJSON()
Example #16
0
def update_profile(cursor, params):
    cursor.execute(
        "select id_profile from Token_Table where token='{}'".format(
            params[User.Token.value]))
    id_prof = cursor.fetchone()[0]
    cursor.execute("select * from Profile where id={}".format(id_prof))
    prof = cursor.fetchone()
    id_photo = prof[3]
    id_auth = prof[1]

    update_string = ""
    if params.get(Profile.About.value, None) is not None:
        update_string += "about=N'{}',".format(params[Profile.About.value])
    if params.get(Profile.Name.value, None) is not None:
        update_string += "name=N'{}'".format(params[Profile.Name.value])
    if update_string != "":
        if update_string[-1] == ',':
            update_string = update_string[:-1]

        cursor.execute("update Profile set {} where id={}".format(
            update_string, id_prof))

    if params.get(Profile.Photo.value, None) is not None:
        if id_photo is None:
            cursor.execute(
                "insert into Photo (id_list_photos, photo) values(null, '{}')".
                format(params[Profile.Photo.value]))
            cursor.execute("select max(id) from Photo")
            id_photo = cursor.fetchone()[0]
            cursor.execute("update Profile set id_photo={} where id={}".format(
                id_photo, id_prof))
        else:
            cursor.execute("update Photo set photo='{}' where id={}".format(
                params[Profile.Photo.value], id_photo))

    if params.get(Profile.Password.value, None) is not None:
        cursor.execute(
            "update Authorization_info set password='******' where id={}".format(
                params[Profile.Password.value], id_auth))

    status = Object()
    status.status = Status.Ok.value

    return status.toJSON()
Example #17
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()
Example #18
0
def get_towns(cursor, params):
    cursor.execute("select * from Kingdom")
    row = cursor.fetchall()

    status = Object()
    status.status = Status.Ok.value
    status.count_kingdom = len(row)
    status.kingdoms = {}
    for i in row:
        cursor.execute("select * from Town where id_kingdom={}".format(i[0]))
        towns = cursor.fetchall()
        kingdoms = i[1]
        status.kingdoms[kingdoms] = Object()
        status.kingdoms[kingdoms].count_town = len(towns)
        status.kingdoms[kingdoms].town = {}
        for j in towns:
            town = Object()
            town.id_town = j[0]
            town.name_town = j[1]
            status.kingdoms[kingdoms].town[len(
                status.kingdoms[kingdoms].town)] = town

    return status.toJSON()
Example #19
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()
Example #20
0
 def error_request(self, message):
     obj = Object()
     obj.status = Status.Error.value
     obj.message = message
     self.wfile.write(str.encode(obj.toJSON()))
Example #21
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()
Example #22
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()
Example #23
0
def get_list_contracts(cursor, params):
    req = "select * from Contract"
    status = Object()
    obj = Object()
    status.status = Status.Ok.value

    filtr = params.get(Params.Filter.Name)
    if filtr is not None:
        req += " where"
        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 "").value
            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:
                req += " Contract.id_task_located in (select id from Town where"

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

                    if kingdom is not None:
                        req += " and"
                    else:
                        req += ')'

                if kingdom is not None:
                    req += " Town.id_kingdom in (select id from Kingdom where Kingdom.name = '{}'))".format(kingdom)

    sort = params.get(Params.Sort.Name)
    if sort is not None:
        req += " order by"
        if sort == Params.Sort.Alph:
            req += " text"
        elif sort == Params.Sort.Locate:
            req += " id_task_located"
        elif sort == Params.Sort.LastUpdate:
            req += " last_update"
        else:
            status.status = Status.Error.value
            obj.value = EventGetListContracts.SortErr.value

        sort_type = params.get(Params.SortType.Name)
        if sort_type is not None and sort_type == Params.SortType.Desc:
            req += " desc"

    if status.status == Status.Ok.value:
        cursor.execute(req)
        contracts = cursor.fetchall()

        cursor.execute("select name from sys.columns where object_id = OBJECT_ID('dbo.Contract')")
        headers = cursor.fetchall()

        obj.contracts = {}
        for i in range(len(headers)):
            head = str(headers[i])[2:-4]
            obj.contracts[head] = []
            for j in range(len(contracts)):
                obj.contracts[head].append(contracts[j][i])

    status.object = obj
    return status.toJSON()
Example #24
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()
Example #25
0
def get_profile(cursor, params):
    status = Object()
    obj = Object()
    status.status = Status.Error.value

    id_profile = params.get(Profile.ID.value, None)

    if id_profile is None:
        cursor.execute(
            "select id_profile from Token_Table where token='{}'".format(
                params[User.Token.value]))
        row = cursor.fetchone()
        if row is None:
            return status.toJSON()
        else:
            id_profile = row[0]

    cursor.execute(
        "select a.id, a.name, a.about, b.photo from Profile as a left \
          join Photo as b on a.id_photo = b.id where a.id={}".format(
            id_profile))
    row = cursor.fetchone()

    if row is not None:
        status.status = Status.Ok.value
        obj.id = row[0]
        obj.name = row[1]
        obj.about = row[2]
        obj.photo = row[3]

        cursor.execute("select id from Witcher where id_profile={}".format(
            obj.id))
        row = cursor.fetchone()

        if row is not None:
            obj.type = Profile.Witcher.value
            cursor.execute(
                "select c.id, c.id_witcher, c.id_client, c.header, c.status, c.last_update,  \
                            c.last_update_status from Profile as a inner join Witcher as b on a.id=b.id_profile \
                            inner join Contract as c on c.id_witcher=b.id where b.id={}"
                .format(row[0]))
            row = cursor.fetchall()
            obj.history = Object()
            obj.history.count = len(row)
            obj.history.contract = {}
            for line in row:
                hist = Object()
                hist.id_contract = line[0]
                hist.id_witcher = line[1]
                hist.id_client = line[2]
                hist.header = line[3]
                hist.status = line[4]
                hist.last_update = line[5]
                hist.last_update_status = line[6]

                obj.history.contract[len(obj.history.contract)] = hist

        else:
            obj.type = Profile.Client.value
            cursor.execute("select id from Client where id_profile={}".format(
                obj.id))
            row = cursor.fetchone()

            cursor.execute(
                "select c.id, c.id_witcher, c.id_client, c.header, c.status, c.last_update, c.last_update_status \
                from Profile as a inner join Client as b on a.id=b.id_profile \
                inner join Contract as c on c.id_witcher=b.id where b.id={}".
                format(row[0]))
            row = cursor.fetchall()
            obj.history = Object()
            obj.history.count = len(row)
            obj.history.contract = {}
            for line in row:
                hist = Object()
                hist.id_contract = line[0]
                hist.id_witcher = line[1]
                hist.id_client = line[2]
                hist.header = line[3]
                hist.status = line[4]
                hist.last_update = line[5]
                hist.last_update_status = line[6]

                obj.history.contract[len(obj.history.contract)] = hist

    status.object = obj
    return status.toJSON()