-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_callbacks.py
182 lines (168 loc) · 5.56 KB
/
delete_callbacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import logging
import re
from telegram import (
InlineKeyboardButton,
InlineKeyboardMarkup,
Update,
constants,
)
from telegram.ext import (
ConversationHandler,
ContextTypes,
)
from db_connection import uc
from bson.objectid import ObjectId
from dashboard_callbacks import format_cred_query
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
# for monitoring activities
logger = logging.getLogger(__name__)
async def del_cred(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""delete retrieved credential"""
query = update.callback_query
context.user_data["del_cred_id"] = re.split("_", query.data, 1)[1]
user = query.from_user
logger.info(f"{user.first_name} is deleteing cred")
for i in context.user_data["credentials"]:
if str(i["_id"]) == context.user_data["del_cred_id"]:
context.user_data["del_cred"] = i
break
await query.answer()
keyboard = [
[
InlineKeyboardButton(
"Yes, I am sure",
callback_data=f"do_del_{context.user_data['del_cred_id']}",
),
InlineKeyboardButton(
"No, Stop",
callback_data=f"return_del_{context.user_data['del_cred_id']}",
),
],
[
InlineKeyboardButton(
"<< BACK",
callback_data=f"return_del_{context.user_data['del_cred_id']}",
),
],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
f"You are about to delete <b>{context.user_data['del_cred']['name']}</b> credential",
reply_markup=reply_markup,
parse_mode=constants.ParseMode.HTML,
)
return ConversationHandler.END
async def do_del_cred(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""do deletion of retrieved credential"""
query = update.callback_query
context.user_data["del_cred_id"] = re.split("_", query.data, 2)[2]
user = query.from_user
logger.info(f"{user.first_name} is accepting to deleteing cred")
for i in context.user_data["credentials"]:
if str(i["_id"]) == context.user_data["del_cred_id"]:
context.user_data["del_cred"] = i
break
await query.answer()
uc.update_one(
{"user_id": f"{context._user_id}"},
{
"$pull": {
"credentials": {
"_id": ObjectId(context.user_data["del_cred_id"]),
},
},
},
)
keyboard = [
[
InlineKeyboardButton(
"Dashboard",
callback_data="dash",
),
],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
f"Credential {context.user_data['del_cred']['name']} successfully deleted.",
reply_markup=reply_markup,
)
context.user_data.clear()
return ConversationHandler.END
async def return_del_cred(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""return almost deleted credential"""
query = update.callback_query
context.user_data["del_cred_id"] = re.split("_", query.data, 2)[2]
user = query.from_user
logger.info(f"{user.first_name} aborted deleteing cred")
for i in context.user_data["credentials"]:
if str(i["_id"]) == context.user_data["del_cred_id"]:
context.user_data["del_cred"] = i
break
await query.answer()
if context.user_data["del_cred"]["name"] in context.user_data["favourites"]:
keyboard_fav = [
[
InlineKeyboardButton(
"EDIT",
callback_data=f"edit_{context.user_data['del_cred_id']}",
),
InlineKeyboardButton(
"DELETE",
callback_data=f"del_{context.user_data['del_cred_id']}",
),
],
[
InlineKeyboardButton(
"REMOVE FROM FAV",
callback_data="defav_cred",
),
],
[
InlineKeyboardButton(
"DONE",
callback_data="done",
),
],
]
reply_markup = InlineKeyboardMarkup(keyboard_fav)
await query.edit_message_text(
format_cred_query(context.user_data["del_cred"]),
reply_markup=reply_markup,
parse_mode=constants.ParseMode.HTML,
)
return ConversationHandler.END
else:
keyboard = [
[
InlineKeyboardButton(
"EDIT",
callback_data=f"edit_{context.user_data['del_cred_id']}",
),
InlineKeyboardButton(
"DELETE",
callback_data=f"del_{context.user_data['del_cred_id']}",
),
],
[
InlineKeyboardButton(
"ADD TO FAV",
callback_data="fav_cred",
),
],
[
InlineKeyboardButton(
"DONE",
callback_data="done",
),
],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
format_cred_query(context.user_data["del_cred"]),
reply_markup=reply_markup,
parse_mode=constants.ParseMode.HTML,
)
return ConversationHandler.END