示例#1
0
def handle_requests():
    db.db_init()
    front_end_path='http://localhost:8080'
    if request.method == 'POST':
        print(request.json)
        
        if  request.json['header'] == 'insert':
            return db.db_insert(request.json)

        if request.json['header']  == 'update':
            db.db_update(request.json)        
        
        if request.json['header']  == 'fetch':
            return db.db_search(request.json)

        return str(request.method)

    if request.method == 'GET':
        return str(request.method)
示例#2
0
 def handle_duplicate_item(user_id: str) -> Optional[Dict[str, Any]]:
     return db_update(
         Key={TableKey.PARTITION: TablePartition.USER, TableKey.SORT: user_id,},
         UpdateExpression="#used_key_count = #used_key_count + 1",
         ConditionExpression=f"attribute_exists(#id)",
         ExpressionAttributeNames={
             "#id": TableKey.SORT,
             "#used_key_count": UserAttr.KEY_USED_COUNT,
         },
     )
示例#3
0
 def upsert_return(match_id: str, new_id: str) -> Optional[Dict[str, Any]]:
     return db_update(
         Key={
             TableKey.PARTITION: TablePartition.MATCH,
             TableKey.SORT: match_id
         },
         UpdateExpression="SET #sort = :new_match_id",
         ExpressionAttributeNames={"#sort": TableKey.SORT},
         ExpressionAttributeValues={":new_match_id": new_id},
         ReturnValues="ALL_NEW",
     )
示例#4
0
 def run(event, user_id, valid_data) -> bool:
     home_id = valid_data[UserAttr.CURRENT_HOME]
     meta_data = event[RequestField.Home.META]
     if not db_update(
         Key={TableKey.PARTITION: TablePartition.HOME, TableKey.SORT: home_id},
         UpdateExpression=f"SET #meta = :home_meta",
         ConditionExpression=f"attribute_exists(#id)",
         ExpressionAttributeNames={"#id": TableKey.SORT, "#meta": HomeAttr.META,},
         ExpressionAttributeValues={":home_meta": meta_data},
     ):
         end("Unable to save home meta.")
     return True
示例#5
0
 def clear_slot(home_id, grid_slot):
     return db_update(
         Key={
             TableKey.PARTITION: TablePartition.HOME,
             TableKey.SORT: home_id
         },
         UpdateExpression=f"REMOVE #grid.#slot",
         ConditionExpression=f"attribute_exists(#id) and #slot in #grid",
         ExpressionAttributeNames={
             "#id": TableKey.SORT,
             "#grid": HomeAttr.GRID,
             "#slot": str(grid_slot),
         },
     )
示例#6
0
 def archive(user_id: str):
     return db_update(
         Key={
             TableKey.PARTITION: TablePartition.USER,
             TableKey.SORT: user_id
         },
         UpdateExpression="set #partition = :user_archive",
         ConditionExpression=f"attribute_exists(#id)",
         ExpressionAttributeValues={
             ":user_archive": TablePartition.USER_ARCHIVE
         },
         ExpressionAttributeNames={
             "#id": TableKey.SORT,
             "#partition": TableKey.PARTITION,
         },
     )
示例#7
0
 def update(home_id, grid_slot, item_meta):
     return db_update(
         Key={
             TableKey.PARTITION: TablePartition.HOME,
             TableKey.SORT: home_id
         },
         UpdateExpression=f"SET #grid.#grid_slot.#slot_meta = :item_meta",
         ConditionExpression=
         f"attribute_exists(#id) and #grid_slot in #grid",
         ExpressionAttributeNames={
             "#id": TableKey.SORT,
             "#grid": HomeAttr.GRID,
             "#slot_meta": HomeAttr.GridSlot.META,
             "#grid_slot": grid_slot,
         },
         ExpressionAttributeValues={":item_meta": item_meta},
     )
示例#8
0
 def handle_new_item(user_id: str, item_id: int) -> Optional[Dict[str, Any]]:
     return db_update(
         Key={TableKey.PARTITION: TablePartition.USER, TableKey.SORT: user_id},
         UpdateExpression=(
             "set #inventory = list_append(#inventory, :item_id), "
             "#key_count = #key_count - 1, "
             "#used_key_count = #used_key_count + 1"
         ),
         ConditionExpression=f"attribute_exists(#id)",
         ExpressionAttributeValues={":item_id": item_id,},
         ExpressionAttributeNames={
             "#id": TableKey.SORT,
             "#inventory": UserAttr.INVENTORY,
             "#key_count": UserAttr.KEY_COUNT,
             "#used_key_count": UserAttr.KEY_USED_COUNT,
         },
     )
示例#9
0
 def claim(match_id: str, user_id: str) -> Optional[Dict[str, Any]]:
     return db_update(
         Key={
             TableKey.PARTITION: TablePartition.MATCH,
             TableKey.SORT: match_id
         },
         UpdateExpression="SET #finder_id = :user_id",
         ConditionExpression=
         f"attribute_exists(#id) AND #finder_id = :empty",
         ExpressionAttributeValues={
             ":user_id": user_id,
             ":empty": ""
         },
         ExpressionAttributeNames={
             "#id": TableKey.SORT,
             "#finder_id": MatchAttr.FINDER_ID,
         },
     )
示例#10
0
 def set_slot(home_id, grid_slot, item, meta):
     return db_update(
         Key={
             TableKey.PARTITION: TablePartition.HOME,
             TableKey.SORT: home_id,
         },
         UpdateExpression=f"SET #grid.#grid_slot = :item",
         ConditionExpression=f"attribute_exists(#id)",
         ExpressionAttributeNames={
             "#id": TableKey.SORT,
             "#grid": HomeAttr.GRID,
             "#grid_slot": grid_slot,
         },
         ExpressionAttributeValues={
             ":item": {
                 HomeAttr.GridSlot.ITEM: item,
                 HomeAttr.GridSlot.META: meta,
             },
         },
     )
示例#11
0
 def add_home(cls, user_id: str, home_id: str, name: str,
              biodome: int) -> Any:
     return db_update(
         Key={
             TableKey.PARTITION: TablePartition.USER,
             TableKey.SORT: user_id
         },
         UpdateExpression=
         ("set #homes = list_append(#homes, :home), #home_count = #home_count + :one"
          ),
         ConditionExpression=
         f"attribute_exists(#id) AND #home_count < :max_homes",
         ExpressionAttributeNames={
             "#id": TableKey.SORT,
             "#homes": UserAttr.HOMES,
             "#home_count": UserAttr.HOME_COUNT,
         },
         ExpressionAttributeValues={
             ":one": 1,
             ":max_homes": Config.HOME_COUNT_MAX,
             ":home": [cls._template_home(home_id, name, biodome)],
         },
     )
示例#12
0
def carma(message):
    carma_to = message.reply_to_message.from_user.id
    carma_to_username = (message.reply_to_message.from_user.username
                         if message.reply_to_message.from_user.username
                         else carma_to)
    carma_to_name = (message.reply_to_message.from_user.first_name
                     if message.reply_to_message.from_user.first_name
                     else carma_to_username)
    to_lastname = message.reply_to_message.from_user.last_name
    if to_lastname:
        carma_to_name += ' ' + to_lastname
    carma_from = message.from_user.id
    carma_from_username = (message.from_user.username
                           if message.from_user.username
                           else carma_from)
    carma_from_name = (message.from_user.first_name
                       if message.from_user.first_name
                       else carma_from_username)
    from_lastname = message.from_user.last_name
    if from_lastname:
        carma_from_name += ' ' + from_lastname

    if carma_from == carma_to:
        return

    x = 0
    current_time = int(time())

    if message.text.lower() == '+' or match(r'^спасибо\W*$',
                                            message.text.lower()):
        x += 1
        change_text = 'увеличил'
    else:
        x -= 1
        change_text = 'уменьшил'

    select_from = db.db_select(
        chat_id=message.chat.id,
        user_id=carma_from,
        column=['date', 'block']
    )
    last_change = 0
    if select_from:
        if match(r'^relation.+', str(select_from)):
            db.db_create(message.chat.id)
            db.db_add(
                chat_id=message.chat.id,
                user_id=carma_from,
                user_name=carma_from_username,
                name=carma_from_name,
                chat_title=message.chat.title,
                date=current_time
            )
        elif select_from[0][1]:
            bot.send_message(message.chat.id,
                             'Вы не можете модифицировать карму')
            return
        else:
            last_change = select_from[0][0]
    if (current_time - last_change) < 10:
        return

    select_to = db.db_select(
        chat_id=message.chat.id,
        user_id=carma_to,
        column=['carma']
    )
    if select_to:
        x += select_to[0][0]
        db.db_update(
            chat_id=message.chat.id,
            user_id=carma_to,
            user_name=carma_to_username,
            name=carma_to_name,
            chat_title=message.chat.title,
            carma=x
        )
    else:
        db.db_add(
            chat_id=message.chat.id,
            user_id=carma_to,
            user_name=carma_to_username,
            name=carma_to_name,
            chat_title=message.chat.title,
            carma=x
        )

    if select_from:
        db.db_update_date(
            chat_id=message.chat.id,
            user_id=carma_from,
            user_name=carma_from_username,
            name=carma_from_name,
            chat_title=message.chat.title,
            date=current_time
        )
    else:
        db.db_add(
            chat_id=message.chat.id,
            user_id=carma_from,
            user_name=carma_from_username,
            name=carma_from_name,
            chat_title=message.chat.title,
            date=current_time
        )

    carma_params = [carma_to, carma_to_name,
                    carma_from, carma_from_name,
                    x, change_text]
    carma_change(bot, message.chat.id, carma_params)