Skip to content

Command contact_owners: add support to filter by usernames #9882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 23, 2023

Conversation

humitos
Copy link
Member

@humitos humitos commented Jan 10, 2023

Add --usernames argument to be able to filter by usernames when sending contacting users.

This can be used as:

django-admin contact_owners --notification notification.md --sticky --usernames usernames.txt
humitos
santos
anthony
Read the Docs is going to force email verification to be able to login accounts in the following weeks.
We've noticed **your account doesn't have any email verified**.
Please, go to [your email settings](/accounts/email/) and verify your email now.

In this case, it will send a sticky notification with that content to these 3 users.

Example

Screenshot_2023-01-10_12-28-22

Required by #9865
Fixes https://github.com/readthedocs/meta/issues/104

Add `--usernames` argument to be able to filter by usernames when sending
contacting users.

This can be used as:

```
django-admin contact_owners --notification notification.md --sticky --usernames usernames.txt
```

```
humitos
santos
anthony
```

```
Read the Docs is going to force email verification to be able to login accounts in the following weeks.
We've noticed **your account doesn't have any email verified**.
Please, go to [your email settings](/accounts/email/) and verify your email now.
```

In this case, it will send a sticky notification with that content to these 3 users.

Required by #9865
@humitos humitos requested a review from a team as a code owner January 10, 2023 11:39
@humitos humitos requested a review from stsewd January 10, 2023 11:39
@stsewd
Copy link
Member

stsewd commented Jan 17, 2023

Not really sure if it's worth trying to support every use case on this command, I've been using the contact_users function from the django shell instead, this allows you more control over the users and context of the messsage, for example:

"""
Script to email owners of organizations
that have domains already on Cloudflare.

We are asking more about their setup.

Email at https://pad.riseup.net.....
"""
from pprint import pprint

import requests
from django.contrib.auth.models import User

from readthedocs.core.utils.contact import contact_users
from readthedocs.projects.models import Domain

pending_domains = [
    "example.com"
]


def get_email():
    url = 'https://pad.riseup.net/p/..../export/txt'
    content = requests.get(url).text.split('\n')
    email_subject = content[0].strip()
    email_content = '\n'.join(content[1:]).strip()
    return email_subject, email_content


def get_users():
    return User.objects.filter(
        owner_organizations__projects__domains__domain__in=pending_domains,
    ).distinct()


def get_context(user):
    domains = Domain.objects.filter(
        project__organizations__owners=user,
        domain__in=pending_domains,
    ).distinct()
    return {
        'pending_domains': domains,
    }


def email_owners(dryrun=True):
    email_subject, email_content = get_email()
    response = contact_users(
        users=get_users(),
        email_subject=email_subject,
        email_content=email_content,
        from_email="Read the Docs <[email protected]>",
        context_function=get_context,
        dryrun=dryrun,
    )
    pprint(response)

@humitos
Copy link
Member Author

humitos commented Jan 18, 2023

Gotcha! 👍🏼 I'm fine merging the changes in this PR into the script since it's not changing its core behavior, but just adding another generic way to get the emails that have to be contacted (--usernames). Next time, if there are some extra logic required for my script, I will create another file.

@ericholscher
Copy link
Member

I think adding this makes sense, given that it's a pretty common use case. Probably worth saving Santos' example somewhere, and linking to it in a comment from this code.

@humitos
Copy link
Member Author

humitos commented Jan 23, 2023

@ericholscher Done! Added a new commit that extends the class docstring pointing to the demo script.

@humitos humitos enabled auto-merge (squash) January 23, 2023 15:28
@humitos humitos merged commit 3c71088 into main Jan 23, 2023
@humitos humitos deleted the humitos/contact-owners-username-filter branch January 23, 2023 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants