1👍
This problem looks interesting. Let me give my try,
I can see there are only three conditions that need to be performed. So let make a dictionary out of it:
con = {
"Equals": "==",
"Contain": "in",
"NotContain": "not in"
}
I can see you have the required values ready in hand here:
#Get all value from
ParameterGet = str(request.POST.get('Parameter', None))
ConditionGet = str(request.POST.get('Condition', None))
valuetomatch = str(request.POST["valuetomatch"])
Let’s take the for loop
to iterate the table alert policies
rows for iteration,
con = {
"Equals": "==",
"Contain": "in",
"NotContain": "not in"
}
for each_row in rows:
***your other logic goes here***
ParameterGet = str(request.POST.get('Parameter', None))
ConditionGet = str(request.POST.get('Condition', None))
valuetomatch = str(request.POST["valuetomatch"])
text = "body.decode('utf-8').rstrip()"
current_condition = "(valuetomatch "+ con[ConditionGet] + " " + ParameterGet.lower() + ")"
if eval(current_condition):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(EMAIL_ACCOUNT, PASSWORD)
msg = "subject not contain!"
server.sendmail(EMAIL_ACCOUNT, "xxx@gmail.com", msg)
server.quit()
else:
print("no email");
OUTPUT:
>>> con = {
"Equals": "==",
"Contain": "in",
"NotContain": "not in"
}
>>> valuetomatch = "hi"
>>> ConditionGet = "Contain"
>>> ParameterGet = "Subject"
>>> subject = "hi"
>>> current_condition = "(valuetomatch "+ con[ConditionGet] + " " + ParameterGet.lower() + ")"
>>> current_condition
'(valuetomatch in subject)'
>>> eval(current_condition)
True
Check eval for reference.
0👍
You could try something like this in server side:
# What you get from frontend
arr=[["Subject","Equals","test"],["Text","Contain","Testing 1 2 3"],["Text","Contain","rhrhj"]]
email_message = email.message_from_string(raw_email_string)
subject = str(email.header.make_header(email.header.decode_header(email_message['Subject'])))
# Dict with mapping between patterns and objects
to_compare = {
'Subject': subject,
'Text': email_message,
}
# Returns right object between email_message or subject
def get_compare(target, to_compare):
try:
return to_compare[target]
except:
raise Exception(f"{target} not found in comparable elements.")
# Loop over arr, perform checks and do actions
for alert in arr:
if arr[1] == 'Equals':
if get_compare(arr[0]) == arr[3]:
# Do something
elif arr[1] == 'Contain':
if arr[3] in get_compare(arr[0]):
# Do something
elif arr[1] == 'NotContain':
if arr[3] not in get_compare(arr[0]):
# Do something
else:
raise Exception(f"{arr[1]} does not belong to recognize operators.")
- [Answered ]-Django dynamic url from calendar
- [Answered ]-How to show current object inside template used with class based FormView
- [Answered ]-How to output a graph from Matplotlib in Django templates?
- [Answered ]-How to add/modify an attribute to a Django class based view from a Decorator?
Source:stackexchange.com