admin.py 4.37 KB
Newer Older
cahrens committed
1 2 3 4
"""
django admin page for the course creators table
"""

5
from course_creators.models import CourseCreator, update_creator_state, send_user_notification, send_admin_notification
6 7
from course_creators.views import update_course_creator_group

Diana Huang committed
8
from ratelimitbackend import admin
9
from django.conf import settings
10
from django.dispatch import receiver
David Baumgold committed
11
from edxmako.shortcuts import render_to_string
12 13
from django.core.mail import send_mail
from smtplib import SMTPException
14 15 16 17

import logging

log = logging.getLogger("studio.coursecreatoradmin")
cahrens committed
18 19


20 21 22 23 24 25 26
def get_email(obj):
    """ Returns the email address for a user """
    return obj.user.email

get_email.short_description = 'email'


cahrens committed
27
class CourseCreatorAdmin(admin.ModelAdmin):
cahrens committed
28 29 30 31
    """
    Admin for the course creator table.
    """

cahrens committed
32
    # Fields to display on the overview page.
33 34
    list_display = ['username', get_email, 'state', 'state_changed', 'note']
    readonly_fields = ['username', 'state_changed']
cahrens committed
35 36 37
    # Controls the order on the edit form (without this, read-only fields appear at the end).
    fieldsets = (
        (None, {
38
            'fields': ['username', 'state', 'state_changed', 'note']
cahrens committed
39 40 41
        }),
    )
    # Fields that filtering support
42
    list_filter = ['state', 'state_changed']
43
    # Fields that search supports.
44
    search_fields = ['user__username', 'user__email', 'state', 'note']
cahrens committed
45 46
    # Turn off the action bar (we have no bulk actions)
    actions = None
cahrens committed
47

48 49 50 51 52 53 54 55 56 57
    def username(self, inst):
        """
        Returns the username for a given user.

        Implemented to make sorting by username instead of by user object.
        """
        return inst.user.username

    username.admin_order_field = 'user__username'

cahrens committed
58 59 60 61 62 63 64 65 66
    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def has_change_permission(self, request, obj=None):
        return request.user.is_staff

cahrens committed
67 68 69 70 71
    def save_model(self, request, obj, form, change):
        # Store who is making the request.
        obj.admin = request.user
        obj.save()

cahrens committed
72 73

admin.site.register(CourseCreator, CourseCreatorAdmin)
74 75 76 77 78 79 80


@receiver(update_creator_state, sender=CourseCreator)
def update_creator_group_callback(sender, **kwargs):
    """
    Callback for when the model's creator status has changed.
    """
81 82 83 84
    user = kwargs['user']
    updated_state = kwargs['state']
    update_course_creator_group(kwargs['caller'], user, updated_state == CourseCreator.GRANTED)

85 86 87

@receiver(send_user_notification, sender=CourseCreator)
def send_user_notification_callback(sender, **kwargs):
88 89 90
    """
    Callback for notifying user about course creator status change.
    """
91 92 93
    user = kwargs['user']
    updated_state = kwargs['state']

94
    studio_request_email = settings.FEATURES.get('STUDIO_REQUEST_EMAIL', '')
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    context = {'studio_request_email': studio_request_email}

    subject = render_to_string('emails/course_creator_subject.txt', context)
    subject = ''.join(subject.splitlines())
    if updated_state == CourseCreator.GRANTED:
        message_template = 'emails/course_creator_granted.txt'
    elif updated_state == CourseCreator.DENIED:
        message_template = 'emails/course_creator_denied.txt'
    else:
        # changed to unrequested or pending
        message_template = 'emails/course_creator_revoked.txt'
    message = render_to_string(message_template, context)

    try:
        user.email_user(subject, message, studio_request_email)
    except:
        log.warning("Unable to send course creator status e-mail to %s", user.email)
112 113 114 115 116 117 118 119 120


@receiver(send_admin_notification, sender=CourseCreator)
def send_admin_notification_callback(sender, **kwargs):
    """
    Callback for notifying admin of a user in the 'pending' state.
    """
    user = kwargs['user']

121
    studio_request_email = settings.FEATURES.get('STUDIO_REQUEST_EMAIL', '')
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    context = {'user_name': user.username, 'user_email': user.email}

    subject = render_to_string('emails/course_creator_admin_subject.txt', context)
    subject = ''.join(subject.splitlines())
    message = render_to_string('emails/course_creator_admin_user_pending.txt', context)

    try:
        send_mail(
            subject,
            message,
            studio_request_email,
            [studio_request_email],
            fail_silently=False
        )
    except SMTPException:
        log.warning("Failure sending 'pending state' e-mail for %s to %s", user.email, studio_request_email)