diff options
author | Hongyuan Ma | 2018-08-01 09:52:06 +0000 |
---|---|---|
committer | Hongyuan Ma | 2018-08-01 09:52:06 +0000 |
commit | 64387ae778f23ab6500a0d584d379b40b0f70b78 (patch) | |
tree | 5b826cb66bbd0dc8a1742814192590e0a6140c82 | |
parent | 44833e6ac55c9fd44d197078802bc72960a0a3ba (diff) |
add approve function in machine model
-rw-r--r-- | web/apps/test_records/views.py | 2 | ||||
-rw-r--r-- | web/apps/users/admin.py | 23 | ||||
-rw-r--r-- | web/apps/users/models.py | 31 | ||||
-rw-r--r-- | web/pgperffarm/settings.py | 11 |
4 files changed, 59 insertions, 8 deletions
diff --git a/web/apps/test_records/views.py b/web/apps/test_records/views.py index 0b6c776..1e094b7 100644 --- a/web/apps/test_records/views.py +++ b/web/apps/test_records/views.py @@ -5,8 +5,6 @@ import django_filters import shortuuid from django.contrib.auth.hashers import make_password -from django.db.models import Count -# from requests import request from rest_framework.pagination import PageNumberPagination from exception import TestDataUploadError diff --git a/web/apps/users/admin.py b/web/apps/users/admin.py index 2ade70f..16fc2e8 100644 --- a/web/apps/users/admin.py +++ b/web/apps/users/admin.py @@ -5,7 +5,7 @@ from django.contrib import admin # Register your models here. from serializer import UserMachineSerializer -from .models import UserMachine,Alias +from .models import UserMachine class UserMachineAdmin(admin.ModelAdmin): list_display = ('alias', 'machine_sn', 'state') @@ -13,15 +13,26 @@ class UserMachineAdmin(admin.ModelAdmin): actions = ['approve_machine'] def approve_machine(self, request, queryset): + + total = 0 + error = 0 + success =0 for machine in queryset: - alias = Alias.objects.filter(is_used=False).order_by('?')[:1] - #todo - serializer = UserMachineSerializer(machine) - serializer.save() + + ret = machine.approve_machine() + ''' + ret = {"is_success": True, "alias": 'alias', "secrct": 'machine_secret', } + ''' + if ret.is_success: + success += 1 + else: + error += 1 + + total +=1 # rows_updated = queryset.update(state=1) # message_bit = "%s machine(s)" % rows_updated - # self.message_user(request, "%s have been approved." % message_bit) + 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 95fa3e0..8f40dd8 100644 --- a/web/apps/users/models.py +++ b/web/apps/users/models.py @@ -1,4 +1,8 @@ from datetime import datetime +import shortuuid + +import hashlib +from django.contrib.auth.hashers import make_password from django.utils import timezone from django.db import models from django.contrib.auth.models import AbstractUser @@ -59,3 +63,30 @@ class UserMachine(models.Model): def __str__(self): return self.alias.__str__() + ' (' + self.os_name + ' ' + self.os_version + '' + self.comp_name + ' ' + self.comp_version + ')' + + def approve_machine(self): + "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": ''} + from django.db import transaction + with transaction.atomic(): + alias.is_used=True + alias.save() + + self.alias = alias + self.state = 1 + if not self.machine_sn: + self.machine_sn = shortuuid.ShortUUID().random(length=16) + + if not self.machine_secret: + machine_str = self.alias.name + self.os_name + self.os_version + self.comp_name + self.comp_version + self.machine_sn + + m = hashlib.md5() + m.update(make_password(str(machine_str), 'pg_perf_farm')) + self.machine_secret = m.hexdigest() + + self.save() + + user_email = + return {"is_success": True, "alias": self.alias, "secrct": self.machine_secret, "email":}
\ No newline at end of file diff --git a/web/pgperffarm/settings.py b/web/pgperffarm/settings.py index 26fbe39..fad5ec7 100644 --- a/web/pgperffarm/settings.py +++ b/web/pgperffarm/settings.py @@ -85,6 +85,17 @@ TEMPLATES = [ }, ] +# PASSWORD_HASHERS = ( +# 'django.contrib.auth.hashers.MD5PasswordHasher', +# 'django.contrib.auth.hashers.PBKDF2PasswordHasher', +# 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', +# 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', +# 'django.contrib.auth.hashers.BCryptPasswordHasher', +# 'django.contrib.auth.hashers.SHA1PasswordHasher', +# +# 'django.contrib.auth.hashers.CryptPasswordHasher', +# ) + WSGI_APPLICATION = 'pgperffarm.wsgi.application' # Database |