Account type
This commit is contained in:
@@ -8,7 +8,7 @@ class TestMigrateWorkflow(TestMigrations):
|
||||
dependencies = (
|
||||
(
|
||||
"paperless_mail",
|
||||
"0027_mailaccount_expiration_mailaccount_refresh_token",
|
||||
"0027_mailaccount_expiration_mailaccount_account_type_and_more",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -431,7 +431,7 @@ def refresh_oauth_token(self, account: MailAccount) -> bool:
|
||||
self.log.error(f"Account {account}: No refresh token available.")
|
||||
return False
|
||||
|
||||
if "gmail" in account.imap_server:
|
||||
if account.account_type == MailAccount.MailAccountType.GMAIL:
|
||||
url = "https://accounts.google.com/o/oauth2/token"
|
||||
data = {
|
||||
"client_id": settings.GMAIL_OAUTH_CLIENT_ID,
|
||||
@@ -439,7 +439,7 @@ def refresh_oauth_token(self, account: MailAccount) -> bool:
|
||||
"refresh_token": account.refresh_token,
|
||||
"grant_type": "refresh_token",
|
||||
}
|
||||
elif "outlook" in account.imap_server:
|
||||
elif account.account_type == MailAccount.MailAccountType.OUTLOOK:
|
||||
url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
|
||||
data = {
|
||||
"client_id": settings.OUTLOOK_OAUTH_CLIENT_ID,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Generated by Django 5.1.1 on 2024-10-05 07:42
|
||||
# Generated by Django 5.1.1 on 2024-10-05 17:12
|
||||
|
||||
from django.db import migrations
|
||||
from django.db import models
|
||||
@@ -20,6 +20,15 @@ class Migration(migrations.Migration):
|
||||
verbose_name="expiration",
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="mailaccount",
|
||||
name="account_type",
|
||||
field=models.PositiveIntegerField(
|
||||
choices=[(1, "IMAP"), (2, "Gmail"), (3, "Outlook")],
|
||||
default=1,
|
||||
verbose_name="account type",
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="mailaccount",
|
||||
name="refresh_token",
|
||||
@@ -15,6 +15,11 @@ class MailAccount(document_models.ModelWithOwner):
|
||||
SSL = 2, _("Use SSL")
|
||||
STARTTLS = 3, _("Use STARTTLS")
|
||||
|
||||
class MailAccountType(models.IntegerChoices):
|
||||
IMAP = 1, _("IMAP")
|
||||
GMAIL = 2, _("Gmail")
|
||||
OUTLOOK = 3, _("Outlook")
|
||||
|
||||
name = models.CharField(_("name"), max_length=256, unique=True)
|
||||
|
||||
imap_server = models.CharField(_("IMAP server"), max_length=256)
|
||||
@@ -51,6 +56,12 @@ class MailAccount(document_models.ModelWithOwner):
|
||||
),
|
||||
)
|
||||
|
||||
account_type = models.PositiveIntegerField(
|
||||
_("account type"),
|
||||
choices=MailAccountType.choices,
|
||||
default=MailAccountType.IMAP,
|
||||
)
|
||||
|
||||
refresh_token = models.CharField(
|
||||
_("refresh token"),
|
||||
max_length=2048,
|
||||
|
||||
@@ -39,6 +39,9 @@ class MailAccountSerializer(OwnedObjectSerializer):
|
||||
"user_can_change",
|
||||
"permissions",
|
||||
"set_permissions",
|
||||
"account_type",
|
||||
"refresh_token",
|
||||
"expiration",
|
||||
]
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
|
||||
@@ -63,6 +63,7 @@ class MailAccountTestView(GenericAPIView):
|
||||
):
|
||||
existing_account = MailAccount.objects.get(pk=request.data["id"])
|
||||
serializer.validated_data["password"] = existing_account.password
|
||||
serializer.validated_data["account_type"] = existing_account.account_type
|
||||
serializer.validated_data["refresh_token"] = existing_account.refresh_token
|
||||
serializer.validated_data["expiration"] = existing_account.expiration
|
||||
|
||||
@@ -109,12 +110,14 @@ class OauthCallbackView(GenericAPIView):
|
||||
if scope is not None and "google" in scope:
|
||||
# Google
|
||||
# Gmail setup guide: https://postmansmtp.com/how-to-configure-post-smtp-with-gmailgsuite-using-oauth/
|
||||
account_type = MailAccount.AccountType.GMAIL
|
||||
imap_server = "imap.gmail.com"
|
||||
defaults = {
|
||||
"name": f"Gmail OAuth {datetime.now()}",
|
||||
"username": "",
|
||||
"imap_security": MailAccount.ImapSecurity.SSL,
|
||||
"imap_port": 993,
|
||||
"account_type": account_type,
|
||||
}
|
||||
|
||||
token_request_uri = "https://accounts.google.com/o/oauth2/token"
|
||||
@@ -124,12 +127,14 @@ class OauthCallbackView(GenericAPIView):
|
||||
elif scope is None:
|
||||
# Outlook
|
||||
# Outlok setup guide: https://medium.com/@manojkumardhakad/python-read-and-send-outlook-mail-using-oauth2-token-and-graph-api-53de606ecfa1
|
||||
account_type = MailAccount.AccountType.OUTLOOK
|
||||
imap_server = "outlook.office365.com"
|
||||
defaults = {
|
||||
"name": f"Outlook OAuth {datetime.now()}",
|
||||
"username": "",
|
||||
"imap_security": MailAccount.ImapSecurity.SSL,
|
||||
"imap_port": 993,
|
||||
"account_type": account_type,
|
||||
}
|
||||
|
||||
token_request_uri = (
|
||||
|
||||
Reference in New Issue
Block a user