from django.db import models
# Create your models here.
-from users.models import UserProfile, UserMachine
+from users.models import UserMachine
class TestBranch(models.Model):
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',)
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)'
# Create your models here.
+# from .serializer import JWTUserProfileSerializer
+
class UserProfile(AbstractUser):
"""
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")
"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
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
--- /dev/null
+from django.core.mail import send_mail as core_send_mail
+from django.core.mail import EmailMultiAlternatives
+import threading
+
+class EmailThread(threading.Thread):
+ def __init__(self, subject, body, from_email, recipient_list, fail_silently, html):
+ self.subject = subject
+ self.body = body
+ self.recipient_list = recipient_list
+ self.from_email = from_email
+ self.fail_silently = fail_silently
+ self.html = html
+ threading.Thread.__init__(self)
+
+ def run (self):
+ msg = EmailMultiAlternatives(self.subject, self.body, self.from_email, self.recipient_list)
+ if self.html:
+ msg.attach_alternative(self.html, "text/html")
+ msg.send(self.fail_silently)
+
+def send_mail(subject, body, from_email, recipient_list, fail_silently=False, html=None, *args, **kwargs):
+ EmailThread(subject, body, from_email, recipient_list, fail_silently, html).start()
+
+
--- /dev/null
+from django.db import models
+
+# Create your models here.
--- /dev/null
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
'test_records',
'crispy_forms',
'user_operation',
+ 'asynchronous_send_mail'
)
MIDDLEWARE_CLASSES = (
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=7200),
'JWT_AUTH_HEADER_PREFIX': 'Token',
}
+
+EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
+EMAIL_USE_SSL = True
+EMAIL_HOST = 'smtp.163.com'
+EMAIL_PORT = 465
+EMAIL_HOST_USER = ''
+EMAIL_HOST_PASSWORD = '' # individual password
+DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
}
PGAUTH_REDIRECT=''
-PGAUTH_KEY=''
\ No newline at end of file
+PGAUTH_KEY=''
+
+PORJECT_PATH = '/var/www/web/pgperffarm' # 'D:\GitSpace\pgperffarm\web\pgperffarm'
+PGAUTH_REDIRECT=''
+PGAUTH_KEY=''
+
+EMAIL_HOST_USER = ''
+EMAIL_HOST_PASSWORD = '' # individual smtp password
\ No newline at end of file