From 97acc52dfd24ac27bde4edccf43dae8242beaf41 Mon Sep 17 00:00:00 2001 From: Janhvi Patil Date: Mon, 8 Apr 2024 20:22:10 +0530 Subject: [PATCH] chore: run pre-commit --- raven/api/chat.py | 24 +-- raven/api/raven_poll.py | 9 +- .../raven_scheduler_event.py | 139 +++++++------ .../doctype/raven_webhook/raven_webhook.py | 186 ++++++++---------- 4 files changed, 167 insertions(+), 191 deletions(-) diff --git a/raven/api/chat.py b/raven/api/chat.py index 519dc0343..68ea867fc 100644 --- a/raven/api/chat.py +++ b/raven/api/chat.py @@ -19,18 +19,18 @@ def get_channel_members(channel_id): if frappe.get_cached_value("Raven Channel", channel_id, "type") == "Open": # select all users, if channel member exists, get is_admin member_query = ( - frappe.qb.from_(user) - .join(channel_member, JoinType.left) - .on((user.name == channel_member.user_id) & (channel_member.channel_id == channel_id)) - .select( - user.name, - user.full_name, - user.user_image, - user.first_name, - user.type, - channel_member.is_admin, - ) - .orderby(channel_member.creation, order=Order.desc) + frappe.qb.from_(user) + .join(channel_member, JoinType.left) + .on((user.name == channel_member.user_id) & (channel_member.channel_id == channel_id)) + .select( + user.name, + user.full_name, + user.user_image, + user.first_name, + user.type, + channel_member.is_admin, + ) + .orderby(channel_member.creation, order=Order.desc) ) else: member_query = ( diff --git a/raven/api/raven_poll.py b/raven/api/raven_poll.py index bb82dda4d..dc395f41c 100644 --- a/raven/api/raven_poll.py +++ b/raven/api/raven_poll.py @@ -120,11 +120,10 @@ def retract_vote(poll_id): # delete all votes by the user for the poll (this takes care of the case where the user has voted for multiple options in the same poll) user = frappe.session.user votes = frappe.get_all( - "Raven Poll Vote", - filters={"poll_id": poll_id, "user_id": user}, - fields=["name"]) + "Raven Poll Vote", filters={"poll_id": poll_id, "user_id": user}, fields=["name"] + ) if not votes: frappe.throw(_("You have not voted for any option in this poll.")) - else: + else: for vote in votes: - frappe.delete_doc("Raven Poll Vote", vote.name) \ No newline at end of file + frappe.delete_doc("Raven Poll Vote", vote.name) diff --git a/raven/raven_integrations/doctype/raven_scheduler_event/raven_scheduler_event.py b/raven/raven_integrations/doctype/raven_scheduler_event/raven_scheduler_event.py index 127fc204f..a204e9ae7 100644 --- a/raven/raven_integrations/doctype/raven_scheduler_event/raven_scheduler_event.py +++ b/raven/raven_integrations/doctype/raven_scheduler_event/raven_scheduler_event.py @@ -6,83 +6,82 @@ class RavenSchedulerEvent(Document): - # begin: auto-generated types - # This code is auto-generated. Do not modify anything in this block. + # begin: auto-generated types + # This code is auto-generated. Do not modify anything in this block. - from typing import TYPE_CHECKING + from typing import TYPE_CHECKING - if TYPE_CHECKING: - from frappe.types import DF + if TYPE_CHECKING: + from frappe.types import DF - bot: DF.Link - channel: DF.Link - content: DF.SmallText - cron_expression: DF.Data | None - disabled: DF.Check - dm: DF.Link | None - event_frequency: DF.Literal["Every Day", - "Every Day of the week", "Date of the month", "Cron"] - event_name: DF.Data - scheduler_event_id: DF.Link | None - send_to: DF.Literal["Channel", "DM"] - # end: auto-generated types + bot: DF.Link + channel: DF.Link + content: DF.SmallText + cron_expression: DF.Data | None + disabled: DF.Check + dm: DF.Link | None + event_frequency: DF.Literal["Every Day", "Every Day of the week", "Date of the month", "Cron"] + event_name: DF.Data + scheduler_event_id: DF.Link | None + send_to: DF.Literal["Channel", "DM"] + # end: auto-generated types - def before_save(self): - ''' - 1. If the 'scheduler_event_id' is not set, create a Server Script of type 'Scheduler Event' and set the 'scheduler_event_id' to the name of the Server Script. - ''' - if not self.scheduler_event_id: - self.scheduler_event_id = self.create_scheduler_event() - else: - server_script = frappe.get_doc( - 'Server Script', self.scheduler_event_id) - server_script.cron_format = self.cron_expression - server_script.script = self.get_scheduler_event_script() - server_script.save() + def before_save(self): + """ + 1. If the 'scheduler_event_id' is not set, create a Server Script of type 'Scheduler Event' and set the 'scheduler_event_id' to the name of the Server Script. + """ + if not self.scheduler_event_id: + self.scheduler_event_id = self.create_scheduler_event() + else: + server_script = frappe.get_doc("Server Script", self.scheduler_event_id) + server_script.cron_format = self.cron_expression + server_script.script = self.get_scheduler_event_script() + server_script.save() - def on_update(self): - ''' - 1. If the 'scheduler_event_id' is set, and the 'disabled' field is updated, update the 'disabled' field of the Server Script of type 'Scheduler Event' with the name 'scheduler_event_id'. - ''' - if self.scheduler_event_id: - server_script = frappe.get_doc( - 'Server Script', self.scheduler_event_id) - server_script.disabled = self.disabled - server_script.save() + def on_update(self): + """ + 1. If the 'scheduler_event_id' is set, and the 'disabled' field is updated, update the 'disabled' field of the Server Script of type 'Scheduler Event' with the name 'scheduler_event_id'. + """ + if self.scheduler_event_id: + server_script = frappe.get_doc("Server Script", self.scheduler_event_id) + server_script.disabled = self.disabled + server_script.save() - def on_trash(self): - ''' - 1. If the 'scheduler_event_id' is set, delete the Server Script of type 'Scheduler Event' with the name 'scheduler_event_id'. - ''' - if self.scheduler_event_id: - frappe.delete_doc('Server Script', self.scheduler_event_id) + def on_trash(self): + """ + 1. If the 'scheduler_event_id' is set, delete the Server Script of type 'Scheduler Event' with the name 'scheduler_event_id'. + """ + if self.scheduler_event_id: + frappe.delete_doc("Server Script", self.scheduler_event_id) - def create_scheduler_event(self): - ''' - Create a Server Script of type 'Scheduler Event' and set the 'scheduler_event_id' to the name of the Server Script. - ''' - server_script = frappe.get_doc({ - 'doctype': 'Server Script', - 'script_type': 'Scheduler Event', - 'name': self.event_name, - 'disabled': 0, - 'event_frequency': 'Cron', - 'cron_format': self.cron_expression, - 'script': self.get_scheduler_event_script() - }) - server_script.insert() - return server_script.name + def create_scheduler_event(self): + """ + Create a Server Script of type 'Scheduler Event' and set the 'scheduler_event_id' to the name of the Server Script. + """ + server_script = frappe.get_doc( + { + "doctype": "Server Script", + "script_type": "Scheduler Event", + "name": self.event_name, + "disabled": 0, + "event_frequency": "Cron", + "cron_format": self.cron_expression, + "script": self.get_scheduler_event_script(), + } + ) + server_script.insert() + return server_script.name - def get_scheduler_event_script(self): - ''' - Get the script for the Scheduler Event - ''' - # bot = frappe.get_doc('Raven Bot', self.bot) - # bot.send_message(self.channel, {'text': self.content}) - # return code snippet with bot & content as values - message = {'text': self.content} - script = f''' + def get_scheduler_event_script(self): + """ + Get the script for the Scheduler Event + """ + # bot = frappe.get_doc('Raven Bot', self.bot) + # bot.send_message(self.channel, {'text': self.content}) + # return code snippet with bot & content as values + message = {"text": self.content} + script = f""" bot = frappe.get_doc('Raven Bot', '{self.bot}')\n bot.send_message('{self.channel}', {message}) -''' - return script +""" + return script diff --git a/raven/raven_integrations/doctype/raven_webhook/raven_webhook.py b/raven/raven_integrations/doctype/raven_webhook/raven_webhook.py index 862a5c49a..28d453f4e 100644 --- a/raven/raven_integrations/doctype/raven_webhook/raven_webhook.py +++ b/raven/raven_integrations/doctype/raven_webhook/raven_webhook.py @@ -17,8 +17,7 @@ class RavenWebhook(Document): from frappe.types import DF channel_id: DF.Link | None - channel_type: DF.Literal["", "Public", - "Private", "Open", "DM", "Self Message"] + channel_type: DF.Literal["", "Public", "Private", "Open", "DM", "Self Message"] condition: DF.SmallText | None conditions_on: DF.Literal["", "Channel", "User", "Channel Type", "Custom"] enable_security: DF.Check @@ -32,29 +31,36 @@ class RavenWebhook(Document): webhook_data: DF.Table[WebhookData] webhook_headers: DF.Table[WebhookHeader] webhook_secret: DF.Password | None - webhook_trigger: DF.Literal["Message Sent", "Message Edited", "Message Deleted", "Message Reacted On", - "Channel Created", "Channel Deleted", "Channel Member Added", "Channel Member Deleted", "User Added", "User Deleted"] + webhook_trigger: DF.Literal[ + "Message Sent", + "Message Edited", + "Message Deleted", + "Message Reacted On", + "Channel Created", + "Channel Deleted", + "Channel Member Added", + "Channel Member Deleted", + "User Added", + "User Deleted", + ] # end: auto-generated types def validate(self): # 1. Check if webhook name is unique # 2. Check if webhook_data and webhook_headers are unique - - # 1. Check if webhook name is unique - webhook = frappe.get_all('Raven Webhook', filters={ - 'webhook_name': self.name}) + webhook = frappe.get_all("Raven Webhook", filters={"webhook_name": self.name}) if webhook: - frappe.throw('Webhook name already exists') + frappe.throw("Webhook name already exists") # 2. Check if webhook_data and webhook_headers are unique webhook_data_keys = [data.key for data in self.webhook_data] webhook_header_keys = [data.key for data in self.webhook_headers] if len(webhook_data_keys) != len(set(webhook_data_keys)): - frappe.throw('Webhook Data keys should be unique') + frappe.throw("Webhook Data keys should be unique") if len(webhook_header_keys) != len(set(webhook_header_keys)): - frappe.throw('Webhook Headers keys should be unique') + frappe.throw("Webhook Headers keys should be unique") def before_save(self): # 1. Check if webhook ID is exists @@ -70,26 +76,25 @@ def before_save(self): # 3. Create the webhook self.create_webhook() - def on_trash(self): # Delete the webhook if self.webhook: - frappe.db.delete('Webhook', self.webhook) + frappe.db.delete("Webhook", self.webhook) def create_webhook(self): # Create a new webhook doctype, event = self.get_doctype_and_event() conditions = self.get_conditions() - webhook_doc = frappe.new_doc('Webhook') + webhook_doc = frappe.new_doc("Webhook") webhook_doc.name = self.name webhook_doc.request_url = self.request_url webhook_doc.is_dynamic_url = self.is_dynamic_url webhook_doc.timeout = self.timeout webhook_doc.enable_security = self.enable_security webhook_doc.webhook_secret = self.webhook_secret - webhook_doc.request_method = 'POST' - webhook_doc.request_structure = 'Form URL-Encoded' + webhook_doc.request_method = "POST" + webhook_doc.request_structure = "Form URL-Encoded" self.set_webhook_data_and_headers(webhook_doc) webhook_doc.webhook_doctype = doctype webhook_doc.webhook_docevent = event @@ -101,7 +106,7 @@ def update_webhook(self): # Update the webhook conditions = self.get_conditions() - webhook_doc = frappe.get_doc('Webhook', self.webhook) + webhook_doc = frappe.get_doc("Webhook", self.webhook) webhook_doc.request_url = self.request_url webhook_doc.is_dynamic_url = self.is_dynamic_url webhook_doc.timeout = self.timeout @@ -115,8 +120,8 @@ def set_webhook_data_and_headers(self, webhook_doc): # Set the webhook data and headers # get the existing webhook data and headers - webhook_header = webhook_doc.get('webhook_headers', []) - webhook_data = webhook_doc.get('webhook_data', []) + webhook_header = webhook_doc.get("webhook_headers", []) + webhook_data = webhook_doc.get("webhook_data", []) # get the existing webhook data and headers keys webhook_data_keys = [data.key for data in self.webhook_data] @@ -127,81 +132,54 @@ def set_webhook_data_and_headers(self, webhook_doc): # and append the new webhook data and headers for data in self.webhook_data: if data.key not in webhook_data_keys: - webhook_doc.append('webhook_data', { - 'key': data.key, - 'fieldname': data.fieldname, - }) + webhook_doc.append( + "webhook_data", + { + "key": data.key, + "fieldname": data.fieldname, + }, + ) for data in self.webhook_headers: if data.key not in webhook_header_keys: - webhook_doc.append('webhook_headers', { - 'key': data.key, - 'value': data.value, - }) + webhook_doc.append( + "webhook_headers", + { + "key": data.key, + "value": data.value, + }, + ) def get_doctype_and_event(self): doctypes_and_events = [ { - 'label': 'Message Sent', - 'doctype': 'Raven Message', - 'event': 'after_insert', - }, - { - - 'label': 'Message Edited', - 'doctype': 'Raven Message', - 'event': 'on_update' - }, - { - - 'label': 'Message Deleted', - 'doctype': 'Raven Message', - 'event': 'on_trash' - }, - { - - 'label': 'Message Reacted On', - 'doctype': 'Raven Message Reaction', - 'event': 'after_insert' - }, - { - - 'label': 'Channel Created', - 'doctype': 'Raven Channel', - 'event': 'after_insert' + "label": "Message Sent", + "doctype": "Raven Message", + "event": "after_insert", }, + {"label": "Message Edited", "doctype": "Raven Message", "event": "on_update"}, + {"label": "Message Deleted", "doctype": "Raven Message", "event": "on_trash"}, + {"label": "Message Reacted On", "doctype": "Raven Message Reaction", "event": "after_insert"}, + {"label": "Channel Created", "doctype": "Raven Channel", "event": "after_insert"}, + {"label": "Channel Deleted", "doctype": "Raven Channel", "event": "on_trash"}, { - - 'label': 'Channel Deleted', - 'doctype': 'Raven Channel', - 'event': 'on_trash' + "label": "Member Added to the Channel", + "doctype": "Raven Channel Member", + "event": "after_insert", }, { - 'label': 'Member Added to the Channel', - 'doctype': 'Raven Channel Member', - 'event': 'after_insert' + "label": "Member Deleted from the Channel", + "doctype": "Raven Channel Member", + "event": "on_trash", }, - { - 'label': 'Member Deleted from the Channel', - 'doctype': 'Raven Channel Member', - 'event': 'on_trash' - }, - { - 'label': 'User Added', - 'doctype': 'Raven User', - 'event': 'after_insert' - }, - { - 'label': 'User Deleted', - 'doctype': 'Raven User', - 'event': 'on_trash' - } + {"label": "User Added", "doctype": "Raven User", "event": "after_insert"}, + {"label": "User Deleted", "doctype": "Raven User", "event": "on_trash"}, ] doctype, event = None, None for doctype_and_event in doctypes_and_events: - if self.webhook_trigger == doctype_and_event['label']: - doctype = doctype_and_event['doctype'] - event = doctype_and_event['event'] + if self.webhook_trigger == doctype_and_event["label"]: + doctype = doctype_and_event["doctype"] + event = doctype_and_event["event"] break return doctype, event @@ -209,42 +187,42 @@ def get_conditions(self): # Get the conditions for the webhook doctype, event = self.get_doctype_and_event() if self.trigger_webhook_on_condition: - if self.conditions_on == 'Channel': - if doctype == 'Raven Channel': + if self.conditions_on == "Channel": + if doctype == "Raven Channel": # return 'doc.name == self.channel_id' return f'doc.name == "{self.channel_id}"' - elif doctype == 'Raven Channel Member': + elif doctype == "Raven Channel Member": return f'doc.channel_id == "{self.channel_id}"' - elif doctype == 'Raven Message': + elif doctype == "Raven Message": return f'doc.channel_id == "{self.channel_id}"' - elif doctype == 'Raven Message Reaction': - frappe.throw('Message Reaction cannot be triggered on Channel') - elif doctype == 'Raven User': - frappe.throw('Raven User cannot be triggered on Channel') - - elif self.conditions_on == 'User': - if doctype == 'Raven Channel': - frappe.throw('Channel cannot be triggered on User') - elif doctype == 'Raven Channel Member': + elif doctype == "Raven Message Reaction": + frappe.throw("Message Reaction cannot be triggered on Channel") + elif doctype == "Raven User": + frappe.throw("Raven User cannot be triggered on Channel") + + elif self.conditions_on == "User": + if doctype == "Raven Channel": + frappe.throw("Channel cannot be triggered on User") + elif doctype == "Raven Channel Member": return f'doc.user_id == "{self.user}"' - elif doctype == 'Raven Message': + elif doctype == "Raven Message": return f'doc.owner == "{self.user}"' - elif doctype == 'Raven Message Reaction': + elif doctype == "Raven Message Reaction": return f'doc.owner == "{self.user}"' - elif self.conditions_on == 'Channel Type': - if doctype == 'Raven Channel': - if self.channel_type in ['Public', 'Private', 'Open']: + elif self.conditions_on == "Channel Type": + if doctype == "Raven Channel": + if self.channel_type in ["Public", "Private", "Open"]: return f'doc.type == "{self.channel_type}"' - elif self.channel_type == 'DM': - return f'doc.is_direct_message == 1' - elif self.channel_type == 'Self Message': - return f'doc.is_self_message == 1' + elif self.channel_type == "DM": + return f"doc.is_direct_message == 1" + elif self.channel_type == "Self Message": + return f"doc.is_self_message == 1" else: - frappe.throw('Invalid Channel Type') + frappe.throw("Invalid Channel Type") else: - frappe.throw('Channel Type cannot be triggered on other doctypes') - elif self.conditions_on == 'Custom': + frappe.throw("Channel Type cannot be triggered on other doctypes") + elif self.conditions_on == "Custom": return self.condition return None