diff options
| author | Hongyuan Ma | 2018-08-01 13:51:57 +0000 |
|---|---|---|
| committer | Hongyuan Ma | 2018-08-01 13:51:57 +0000 |
| commit | 4a1de5e0c2e05793ac849c3439b4d0f517f3f06c (patch) | |
| tree | 635a09cc730ff1d15b7fb7ad0816c7c917db5171 /web/apps | |
| parent | 64387ae778f23ab6500a0d584d379b40b0f70b78 (diff) | |
add mail sender in admin
Diffstat (limited to 'web/apps')
| -rw-r--r-- | web/apps/test_records/models.py | 2 | ||||
| -rw-r--r-- | web/apps/users/admin.py | 33 | ||||
| -rw-r--r-- | web/apps/users/models.py | 13 |
3 files changed, 35 insertions, 13 deletions
diff --git a/web/apps/test_records/models.py b/web/apps/test_records/models.py index 6bb1d3d..2d9e2ad 100644 --- a/web/apps/test_records/models.py +++ b/web/apps/test_records/models.py @@ -3,7 +3,7 @@ from django.utils import timezone from django.db import models # Create your models here. -from users.models import UserProfile, UserMachine +from users.models import UserMachine class TestBranch(models.Model): diff --git a/web/apps/users/admin.py b/web/apps/users/admin.py index 16fc2e8..8d675c2 100644 --- a/web/apps/users/admin.py +++ b/web/apps/users/admin.py @@ -2,11 +2,13 @@ from __future__ import unicode_literals from django.contrib import admin - +from asynchronous_send_mail import send_mail +from django.conf import settings # Register your models here. from serializer import UserMachineSerializer from .models import UserMachine + class UserMachineAdmin(admin.ModelAdmin): list_display = ('alias', 'machine_sn', 'state') list_filter = ('state',) @@ -16,23 +18,38 @@ class UserMachineAdmin(admin.ModelAdmin): total = 0 error = 0 - success =0 + success = 0 for machine in queryset: ret = machine.approve_machine() - ''' - ret = {"is_success": True, "alias": 'alias', "secrct": 'machine_secret', } - ''' - if ret.is_success: + # ret = {"is_success": True, "alias": 'alias', "secrct": 'machine_secret', "email":user_email} + + if ret['is_success']: success += 1 + # send email to notice user + content = "The machine you have applied for has been approved.\n\ +Here is the information about it: \n \ +\n \ +alias: %s\n \ +secret: %s\n \ +\n \ +Regards,\n \ +PG PERF FARM" % (ret['alias'], ret['secret']) + # ret['alias'] + ': ' + ret['secret'] + + send_mail('[PG PERF FARM]Machine Approval Notice', content, settings.EMAIL_HOST_USER, [ret['email']], + fail_silently=False) + else: error += 1 - total +=1 + total += 1 # rows_updated = queryset.update(state=1) # message_bit = "%s machine(s)" % rows_updated - self.message_user(request, "Total: %s ,Success: %s ,Error: %s. Please make sure there are enough unused aliases" % (total,success,error)) + self.message_user(request, + "Total: %s ,Success: %s ,Error: %s. Please make sure there are enough unused aliases." % ( + total, success, error)) approve_machine.short_description = u'Approve Machine(Modify the state to active, generate machine_sn, machine_secret, and assign an alias)' diff --git a/web/apps/users/models.py b/web/apps/users/models.py index 8f40dd8..4e05a47 100644 --- a/web/apps/users/models.py +++ b/web/apps/users/models.py @@ -9,6 +9,8 @@ from django.contrib.auth.models import AbstractUser # Create your models here. +# from .serializer import JWTUserProfileSerializer + class UserProfile(AbstractUser): """ @@ -42,7 +44,7 @@ class UserMachine(models.Model): machine_sn = models.CharField(max_length=16, verbose_name="machine sn") machine_secret = models.CharField(max_length=32, verbose_name="machine secret") machine_owner = models.ForeignKey(UserProfile) - alias = models.ForeignKey(Alias,blank=True ,verbose_name="alias", help_text="alias") + alias = models.ForeignKey(Alias,blank=True, default=None, verbose_name="alias", help_text="alias") os_name = models.CharField(max_length=32, verbose_name="operation system name") os_version = models.CharField(max_length=32, verbose_name="operation system version") comp_name = models.CharField(max_length=32, verbose_name="compiler name") @@ -68,7 +70,7 @@ class UserMachine(models.Model): "Approve Machine(Modify the state to active, generate machine_sn, machine_secret, and assign an alias)" alias = Alias.objects.filter(is_used=False).order_by('?').first() if not alias: - return {"is_success": False, "alias": '', "secrct": ''} + return {"is_success": False, "alias": '', "secret": '', "email":''} from django.db import transaction with transaction.atomic(): alias.is_used=True @@ -88,5 +90,8 @@ class UserMachine(models.Model): self.save() - user_email = - return {"is_success": True, "alias": self.alias, "secrct": self.machine_secret, "email":}
\ No newline at end of file + + # serializer = JWTUserProfileSerializer(user) + print(self.machine_owner.email) + user_email = self.machine_owner.email + return {"is_success": True, "alias": self.alias.name, "secret": self.machine_secret, "email":user_email}
\ No newline at end of file |
