from allauth.account.signals import email_confirmed, user_signed_up
from django.core.mail import send_mail
from django.dispatch import receiver


@receiver(email_confirmed)
def make_first_user_admin(email_address, **kwargs):
    """Grant access to first user."""
    from django.contrib.auth.models import User
    users = User.objects.all()
    if len(users) > 1:
        return
    else:
        # if this is the first user, grant them access to everything
        first_user = users[0]
        first_user.is_staff = True
        first_user.is_superuser = True
        first_user.save()
        from allauth.account.models import EmailAddress
        first_user_email = EmailAddress.objects.get(email=first_user.email)
        first_user_email.verified = True
        first_user_email.save()


@receiver(user_signed_up)
def email_admin_on_signup(request, user, **kwargs):
    """Email office on new user sign up."""
    body = (
        "New User Signed Up: {}\n\n"
        "Please go to the admin page to approve their account.\n"
        "If you do not approve their account, they will be unable "
        "to access apostello."
    )
    body = body.format(str(user))
    from site_config.models import SiteConfiguration
    to_ = [SiteConfiguration.get_solo().office_email]
    send_mail("[apostello] New User", body, '', to_, )

@@ -1,71 +1,19 @@
-from allauth.account.signals import email_confirmed, user_signed_up
+import logging%0A
 %0Afro
@@ -86,16 +86,126 @@
 ceiver%0A%0A
+from allauth.account.signals import email_confirmed, user_signed_up%0A%0Alogger = logging.getLogger('apostello')%0A%0A
 %0A@receiv
@@ -1287,17 +1287,16 @@
   to_ = 
-%5B
 SiteConf
@@ -1328,18 +1328,59 @@
 ce_email
-%5D
 %0A
+    try:%0A        if len(to_) %3E 0:%0A        
     send
@@ -1423,11 +1423,158 @@
 '', 
+%5B
 to_
+%5D
 , )%0A
+    except Exception:%0A        logger.error(%0A            'Error sending email to office on new user sign up',%0A            exc_info=True%0A        )%0A

fix error when no office email is defined