求大神能把这个类中多余的部分给去掉,我觉得这里有些代码是没用,求大神来看看
# encoding: utf-8import json
import platform
import uuid
from hashlib import md5
from django.contrib import messages
from django.http import HttpResponse
from django.utils.translation import ugettext as _
from seaserv import ccnet_threaded_rpc, seafserv_threaded_rpc, get_emailusers,seafile_api
from seahub.base.models import UserLastLogin
import base64
from M2Crypto import X509, RSA
class i2_lic(object):
code = ''
count_ldapusers = 0
count_dbusers = 0
count_ldapquota = 0
count_dbquota = 0
user_count = 0
quota_space = 0
max_user_count = -1
max_quota_space = -1
license_type = 0
product_type = 0
serial_number = 0
maintenance_period = 0
register_name = 0
hardware_code = ''
emailusers_count = 0
def __init__(self):
self.code = ''
def load_code(self):
try:
f = open(settings.LICENSE_TXT)
msg = f.read( )
f.close( )
self.parserLicense(msg)
except:
self.maintenance_period = 0
self.license_type = 'unknown'
self.register_name = 'unknown'
self.serial_number = 'unknown'
self.max_user_count = -1
self.max_quota_space = -1
self.product_type = 'unknown'
def get_info(self):
self.emailusers_count = ccnet_threaded_rpc.count_emailusers()
count_ldapusers = 0
count_ldapquota = 0
try:
users = get_emailusers('LDAP', -1, -1)
last_logins = UserLastLogin.objects.filter(username__in=[x.email for x in users])
for user in users:
for last_login in last_logins:
if last_login.username == user.email:
count_ldapusers = count_ldapusers + 1
count_ldapquota = count_ldapquota + seafile_api.get_user_quota(user.email)
'''messages.success(request, _(u'Successfully get current ldap user quota count = %s') % count_ldapquota)
HttpResponse(json.dumps({'success': True}), content_type=content_type)'''
self.count_ldapusers = count_ldapusers
self.count_ldapquota = count_ldapquota
except:
self.count_ldapusers = 0
self.count_ldapquota = 0
count_dbusers = 0
try:
count_dbusers = ccnet_threaded_rpc.count_db_emailusers()
self.count_dbusers = count_dbusers
except:
self.count_dbusers = 0
self.user_count = count_ldapusers + count_dbusers
count_dbquota = 0
try:
users = get_emailusers('DB', -1, -1)
for user in users:
org = ccnet_threaded_rpc.get_orgs_by_user(user.email)
if not org:
count_dbquota = count_dbquota + seafile_api.get_user_quota(user.email)
else:
user.org = org[0]
org_id = user.org.org_id
count_dbquota = count_dbquota + seafserv_threaded_rpc.get_org_user_quota(org_id, user.email)
'''messages.success(request, _(u'Successfully get current db quota count = %s') % count_dbquota)
HttpResponse(json.dumps({'success': True}), content_type=content_type)'''
self.count_dbquota = count_dbquota
except:
self.count_dbquota = 0
self.quota_space = self.count_ldapquota + self.count_dbquota
def get_max_user_count(self):
return self.max_user_count
def get_max_quota_space(self):
return self.max_quota_space
def get_user_count(self):
return self.user_count
def get_quota_space(self):
return self.quota_space
def load_code_print(self, request):
content_type = 'application/json; charset=utf-8'
messages.success(request, _(u'Successfully get max user count is %s') % self.max_user_count)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get max user quota is %s GB') % self.max_quota_space)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
def get_info_print(self, request):
content_type = 'application/json; charset=utf-8'
messages.success(request, _(u'Successfully get user count = %s') % self.emailusers_count)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get current ldap user count = %s') % self.count_ldapusers)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get current db user count = %s') % self.count_dbusers)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get all current user count = %s') % self.user_count)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get all user count = %s') % self.emailusers_count)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get current ldap user quota count = %s') % self.count_ldapquota)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get current db user quota count = %s') % self.count_dbquota)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get all current user quota count = %s') % self.quota_space)
return HttpResponse(json.dumps({'success': True}), content_type=content_type)
def isWindowsSystem():
return 'Windows' in platform.system()
def isLinuxSystem():
return 'Linux' in platform.system()
def getOsType():
if isWindowsSystem():
return 1
else:
return 2
def getHardwareCode(self):
m = md5()
mac=uuid.UUID(int = uuid.getnode()).hex[-12:]
macStr = ":".join([mac[e:e+2] for e in range(0,11,2)]).upper()
m.update(macStr)
return m.hexdigest().upper()
def parserLicense(self,encCode):
data = base64.b64decode(encCode)
cert = X509.load_cert(settings.I2_CRT)
puk = cert.get_pubkey().get_rsa()
self.code = puk.public_decrypt(data, RSA.pkcs1_padding)
i2codes = self.code
i2code_list = i2codes.split(',')
self.maintenance_period = i2code_list[3]
self.hardware_code = i2code_list[4]
self.license_type = i2code_list[5]
self.register_name = i2code_list[7]
self.serial_number = i2code_list[8]
self.max_user_count = i2code_list[10]
self.max_quota_space = i2code_list[12]
self.product_type = i2code_list[17]
我觉得以上代码里面包含的这些代码是没用,该去掉的,getxx()函数在这里毫无意义
def get_max_user_count(self): return self.max_user_count
def get_max_quota_space(self):
return self.max_quota_space
def get_user_count(self):
return self.user_count
def get_quota_space(self):
return self.quota_space
def load_code_print(self, request):
content_type = 'application/json; charset=utf-8'
messages.success(request, _(u'Successfully get max user count is %s') % self.max_user_count)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get max user quota is %s GB') % self.max_quota_space)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
def get_info_print(self, request):
content_type = 'application/json; charset=utf-8'
messages.success(request, _(u'Successfully get user count = %s') % self.emailusers_count)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get current ldap user count = %s') % self.count_ldapusers)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get current db user count = %s') % self.count_dbusers)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get all current user count = %s') % self.user_count)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get all user count = %s') % self.emailusers_count)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get current ldap user quota count = %s') % self.count_ldapquota)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get current db user quota count = %s') % self.count_dbquota)
HttpResponse(json.dumps({'success': True}), content_type=content_type)
messages.success(request, _(u'Successfully get all current user quota count = %s') % self.quota_space)
return HttpResponse(json.dumps({'success': True}), content_type=content_type)
def user_set_quota(request, email): if request.method != 'POST':
raise Http404
content_type = 'application/json; charset=utf-8'
result = {}
f = SetUserQuotaForm(request.POST)
if f.is_valid():
email = f.cleaned_data['email']
quota_mb = f.cleaned_data['quota']
quota = quota_mb * (1 << 20)
i2 = i2_lic()
i2.load_code()
i2.get_info()
max_quota = i2.get_max_quota_space()
if max_quota == -1:
result['error'] = _(u'Failed to set quota: get license quota fail')
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
if i2.get_quota_space() + quota > int(max_quota)*1024*1024*1024:
result['error'] = _(u'Failed to set quota: exceed license allowed(%s GB)') % max_quota
return HttpResponse(json.dumps(result), status=400, content_type=content_type)
这里圈起来的是调用lic.py中的getxxx(),如果lic.py没有getxxx(),那怎么访问lic.py中类的属性呢?
难道只能这样加getxxx()
回答:
我覺得就去掉! 的確沒有意義!
這些 getter 並沒有實現任何封裝, 以 Python 的觀點而言(甚至以其他語言而言也是, 因為如同 @prolifes 大所講的, 他連屬性都非 private), 這樣做沒什麼道理
我回答過的問題: Python-QA
以上是 求大神能把这个类中多余的部分给去掉,我觉得这里有些代码是没用,求大神来看看 的全部内容, 来源链接: utcz.com/a/160991.html