[email] Make clear where hood.name/hood.id is used
This commit is contained in:
parent
5e93549591
commit
7866b2c4d1
kibicara
|
@ -35,7 +35,7 @@ def main():
|
||||||
body = {
|
body = {
|
||||||
'text': text,
|
'text': text,
|
||||||
'to': mail['To'].lower(),
|
'to': mail['To'].lower(),
|
||||||
'author': mail['From'].lower(),
|
'author': mail.get_unixfrom(),
|
||||||
'secret': Email.secret,
|
'secret': Email.secret,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,8 @@ from kibicara.platforms.email.bot import spawner
|
||||||
from kibicara.platforms.email.model import Email, EmailRecipients
|
from kibicara.platforms.email.model import Email, EmailRecipients
|
||||||
from kibicara.platformapi import Message
|
from kibicara.platformapi import Message
|
||||||
from kibicara.config import config
|
from kibicara.config import config
|
||||||
from kibicara.webapi.hoods import get_hood
|
|
||||||
from kibicara.email import send_email
|
from kibicara.email import send_email
|
||||||
|
from kibicara.model import Hood
|
||||||
from ormantic.exceptions import NoMatch
|
from ormantic.exceptions import NoMatch
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from sqlite3 import IntegrityError
|
from sqlite3 import IntegrityError
|
||||||
|
@ -23,13 +23,15 @@ class BodyMessage(BaseModel):
|
||||||
|
|
||||||
|
|
||||||
class Recipient(BaseModel):
|
class Recipient(BaseModel):
|
||||||
hood: int
|
hood_name: str
|
||||||
email: str
|
email: str
|
||||||
|
|
||||||
|
|
||||||
async def get_email_bot(to, hood=Depends(get_hood)):
|
async def get_email_bot(to):
|
||||||
|
hood_name = to.split('@')[0]
|
||||||
|
hood = await Hood.objects.get(name=hood_name)
|
||||||
try:
|
try:
|
||||||
return await Email.objects.get(hood=to)
|
return await Email.objects.get(hood=hood.id)
|
||||||
except NoMatch:
|
except NoMatch:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
@ -59,30 +61,31 @@ async def test_create(response: Response, hood=Depends(get_hood)):
|
||||||
|
|
||||||
@router.post('/recipient/')
|
@router.post('/recipient/')
|
||||||
async def email_recipient_create(recipient: Recipient):
|
async def email_recipient_create(recipient: Recipient):
|
||||||
token = jwt.encode({
|
token = jwt.encode(
|
||||||
'email': recipient.email,
|
{'email': recipient.email, 'hood_name': recipient.hood_name,}, Email.secret
|
||||||
'hood': recipient.hood,
|
).decode('ascii')
|
||||||
}, Email.secret).decode('ascii')
|
|
||||||
confirm_link = config['root_url'] + "api/email/recipient/confirm/" + token
|
confirm_link = config['root_url'] + "api/email/recipient/confirm/" + token
|
||||||
hood_name = await get_hood(recipient.hood)
|
send_email(
|
||||||
send_email(recipient.email,
|
recipient.email,
|
||||||
"Subscribe to Kibicara " + hood_name,
|
"Subscribe to Kibicara " + recipient.hood_name,
|
||||||
sender=hood_name,
|
sender=recipient.hood_name,
|
||||||
body="To confirm your subscription, follow this link: " + confirm_link)
|
body="To confirm your subscription, follow this link: " + confirm_link,
|
||||||
|
)
|
||||||
return status.HTTP_200_OK
|
return status.HTTP_200_OK
|
||||||
|
|
||||||
|
|
||||||
@router.post('/recipient/confirm/<token>')
|
@router.post('/recipient/confirm/<token>')
|
||||||
async def email_recipient_confirm(token):
|
async def email_recipient_confirm(token):
|
||||||
json = jwt.decode(token, Email.secret)
|
json = jwt.decode(token, Email.secret)
|
||||||
|
hood = await Hood.objects.get(name=json['hood_name'])
|
||||||
try:
|
try:
|
||||||
await EmailRecipients.objects.create(hood=json['hood'], email=json['email'])
|
await EmailRecipients.objects.create(hood=hood.id, email=json['email'])
|
||||||
return status.HTTP_201_CREATED
|
return status.HTTP_201_CREATED
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
|
||||||
|
|
||||||
# delete EmailRecipient
|
|
||||||
|
|
||||||
|
# delete EmailRecipient
|
||||||
|
|
||||||
|
|
||||||
@router.post('/messages/')
|
@router.post('/messages/')
|
||||||
|
|
Loading…
Reference in a new issue