def loggin_web(request): if request.method == 'POST': data = json.dumps(request.POST) form = json.loads(data) email = form.get('email') pwd = form.get('pwd') password = encryption(pwd) item = Loggin.objects.filter(email=email) if item.count() == 0: return JsonResponse({ 'status': 'false', 'msg': 'Email nhập không chính xác!' }) item = Loggin.objects.filter(email=email).filter(pwd=password) if item.count() == 0: return JsonResponse({ 'status': 'false', 'msg': 'Mật khẩu nhập không chính xác!' }) return JsonResponse({'status': 'true', 'msg': 'Đăng nhập thành công!'})
def Reset_Pwd_Customer_ByteSave(request, id): try: item = Customer_ByteSave.objects.get(id=id) if item != None: pwd = genpwd(12) item.bytesave_pwd = encryption(pwd) item.save() notification_customer_bytesave('[ByteSave]', item.bytesave_email, pwd, item.bytesave_email) return JsonResponse({ 'status': 'true', 'msg': 'Reset mật khẩu thành công và thông tin đã được gửi tới ' + item.bytesave_email }) except Exception as e: return JsonResponse({ 'status': 'false', 'msg': 'Reset mật khẩu không thành công!' })
def loggin_bytesave(request): if request.method == 'POST': try: data = json.dumps(request.POST) form = json.loads(data) email = form.get('email') pwd = encryption(form.get('pwd')) serial_number = form.get('serial_number') item = Customer_ByteSave.objects.filter( bytesave_email=email).filter(~Q(is_del=1)) if item.count() == 0: return JsonResponse({ 'status': 'false', 'msg': 'Email nhập không chính xác!' }) item = Customer_ByteSave.objects.filter( bytesave_email=email).filter( bytesave_pwd=pwd).filter(~Q(is_del=1)) if item.count() == 0: return JsonResponse({ 'status': 'false', 'msg': 'Mật khẩu nhập không chính xác!' }) #item = Agents.objects.filter(serial_number=serial_number).filter(~Q(is_del=1)) #if item.count() > 0: # item = Agents.objects.filter(~Q(is_del=1)).get(serial_number=serial_number) # item.is_logged = 1 # item.save() # return JsonResponse({'status': 'true', 'msg': 'Đăng nhập thành công!'}) item = Customer_ByteSave.objects.filter(~Q(is_del=1)).get( bytesave_email=email) #Kiểm tra chu kì: #get list chu kì có time hết hạn > time.now(): nếu count() = 0 thì return tài khaorn đã hết hạn item_cycles = bytesave_cycle.objects.filter(~Q(is_del=1)).filter( id_customer=item.id_customer).filter( bytesave_expiration_date__gte=Timer.get_timestamp_now()) if item_cycles.count() == 0: return JsonResponse({ 'status': 'false', 'msg': 'Tài khoản đã hết hạn sử dụng!', 'email_loggin': email }) #Tìm tổng số máy được sử dụng number_used = 0 for item_cycle in item_cycles: if item_cycle.bytesave_start_date > Timer.get_timestamp_now(): continue if item_cycle.bytesave_start_date <= Timer.get_timestamp_now(): number_used += item_cycle.bytesave_amount_used # nếu số mày =0 là tài khoản chưa đến ngày bắt đầu sử dụng if number_used == 0: return JsonResponse({ 'status': 'false', 'msg': 'Tài khoản chưa được kích hoạt!', 'email_loggin': email }) items_agents_logged = Agents.objects.filter( id_customer=item.id_customer).filter( is_logged=1).filter(~Q(is_del=1)) if number_used <= items_agents_logged.count(): return JsonResponse({ 'status': 'false', 'msg': 'Tài khoản đã hết phiên đăng nhập!', 'email_loggin': email }) items_agents = Agents.objects.filter( id_customer=item.id_customer).filter( serial_number=serial_number).filter(~Q(is_del=1)) # Kiểm tra đã có agent đăng nhập nào chưa.nếu chưa thì đi thêm mới agnet : if items_agents.count() == 0: os = form.get('os') ip_public = form.get('ip_public') ip_private = form.get('ip_private') name_computer = form.get('name_computer') create_agent_when_loggin(item.id, item.id_customer, os, serial_number, ip_public, ip_private, name_computer) return JsonResponse({ 'status': 'true', 'msg': 'Đăng nhập thành công!', 'email_loggin': email }) #item_cycles = bytesave_cycle.objects.filter(~Q(is_del=1)).filter(bytesave_amount_used) < float(Timer.get_timestamp_now())) check_serial_number_in_agents = Agents.objects.filter( serial_number=serial_number).filter( id_customer=item.id_customer).filter(~Q(is_del=1)).filter( is_logged=0).first() if check_serial_number_in_agents != None: # item_serial_number = Agents.objects.filter(~Q(is_del=1)).filter(is_logged=0).get(serial_number=serial_number) check_serial_number_in_agents.is_logged = 1 check_serial_number_in_agents.save() return JsonResponse({ 'status': 'true', 'msg': 'Đăng nhập thành công!', 'email_loggin': email }) # if items_agent.count() > item.bytesave_amount_used: # return JsonResponse({'status': 'false', 'msg': 'Tài khoản đã được sử dụng hết phiên đăng nhập!'}) # if item.bytesave_expiration_date != None: # if float(item.bytesave_expiration_date) < Timer.get_timestamp_now(): # return JsonResponse({'status': 'false', 'msg': 'Tài khoản đã hết hạn sử dụng!'}) # today = datetime.today() # if items_agent.count() ==0: # if item.bytesave_time_type == 0: # item.bytesave_expiration_date = str(datetime(today.year , today.month + item.bytesave_duration, 1).timestamp()) # else: # item.bytesave_expiration_date = str(datetime(today.year + item.bytesave_duration , today.month , 1).timestamp()) # item.bytesave_use_start_date = today.timestamp() # item.save() # os = form.get('os') # ip_public = form.get('ip_public') # ip_private = form.get('ip_private') # name_computer = form.get('name_computer') # create_agent_when_loggin(item.id,item.id_customer,os,serial_number,ip_public,ip_private,name_computer) return JsonResponse({ 'status': 'true', 'msg': 'Đăng nhập thành công!', 'email_loggin': email }) except Exception as e: return JsonResponse({ 'status': 'false', 'msg': 'Đăng nhập không thành công!' })
def load_data(request): if request.method == 'GET': try: data = [] countdata = 0 for item in Loggin.objects.all().filter((~Q(is_del=1))): countdata = 1 data.append({ 'id': item.id, 'name': item.name, 'email': item.email, 'type': item.type, }) countdata += 1 return JsonResponse({ 'status': 'true', 'countdata': countdata, 'data': data }) except Exception as e: return JsonResponse({ 'status': 'false', 'countdata': 0, 'data': [], 'string_error': e }) if request.method == 'POST': data = json.dumps(request.POST) form = json.loads(data) id = form.get('id', 0) if id == '0' or id == '': # thêm mới try: Loggin.create( **{ 'email': form.get('email'), 'name': form.get('name'), 'pwd': encryption(form.get('pwd')), 'type': form.get('type'), }) return JsonResponse({ 'status': 'true', 'msg': 'Thêm mới thành công!' }) except Exception as e: return JsonResponse({ 'status': 'false', 'msg': 'Thêm mới không thành công!', 'string_error': e }) else: # Chỉnh sửa try: item = Loggin.objects.get(id=int(id)) item.email = form.get('email') item.name = form.get('name') item.pwd = encryption(form.get('pwd')) item.type = form.get('type') item.time_update_at = Timer.get_timestamp_now() item.save() return JsonResponse({ 'status': 'true', 'msg': 'Chính sửa thành công!' }) except Exception as e: return JsonResponse({ 'status': 'false', 'msg': 'Chỉnh sửa không thành công!', 'string_error': e }) return None
def Load_data(request, id): if request.method == 'GET': try: data = [] countdata = 0 countdata_represent = 0 i = 0 if id == 0: for item in Customers.objects.all().filter( (~Q(is_del=1))).order_by('-id'): data_represent = [] i = i + 1 item_customer_bytesave = Customer_ByteSave.objects.filter( id_customer=item.id)[0] status = 0 # if item_customer_bytesave.bytesave_expiration_date != '' and item_customer_bytesave.bytesave_expiration_date != None: # if float(item_customer_bytesave.bytesave_expiration_date) < Timer.get_timestamp_now(): # status = 2 # else:status = 1 item_customer_represent = Customer_Represent.objects.filter( id_customer=item.id).filter((~Q(is_del=1))) if item_customer_represent.count() > 0: stt = 0 for item_represent in item_customer_represent: data_represent.append({ 'stt': stt + 1, 'id': item_represent.id, 'id_customer': item_represent.id_customer, 'name': item_represent.name, 'phone_number': item_represent.phone_number, 'position': item_represent.position, 'email': item_represent.email, 'type': item_represent.type, 'is_del': item_represent.is_del, 'time_create_at': item_represent.time_create_at, 'time_update_at': item_represent.time_update_at, }) countdata_represent += 1 stt += 1 data.append({ 'id': item.id, 'stt': i, 'id_loggin': item.id_loggin, 'id_version': item.id_version, 'customer_bytesave': { 'id': item_customer_bytesave.id, 'bytesave_email': item_customer_bytesave.bytesave_email if item_customer_bytesave.bytesave_email != None else '', 'bytesave_pwd': item_customer_bytesave.bytesave_pwd, # 'bytesave_amount_used': item_customer_bytesave.bytesave_amount_used, # 'bytesave_duration': item_customer_bytesave.bytesave_duration, 'bytesave_use_start_date': item_customer_bytesave.bytesave_use_start_date, # 'bytesave_time_type': item_customer_bytesave.bytesave_time_type, #'bytesave_expiration_date': Convert_timestamp( item_customer_bytesave.bytesave_expiration_date), }, 'name': item.name, 'email': item.email, 'type': item.type, 'phone_number': item.phone_number, 'city': item.city, 'address': item.address, 'tax_code': item.tax_code, 'website': item.website, 'fax': item.fax, 'legal_representative': item.legal_representative, 'scale': item.scale, 'field': item.field, 'is_upgrade_version': item.is_upgrade_version, 'time_create_at': item.time_create_at, 'time_update_at': item.time_update_at, 'customer_represent': data_represent, 'countdata_represent': countdata_represent, 'status': status, }) else: item = Customers.objects.get(id=id) data_represent = [] item_customer_bytesave = Customer_ByteSave.objects.filter( id_customer=item.id)[0] status = 0 # if item_customer_bytesave.bytesave_expiration_date != '' and item_customer_bytesave.bytesave_expiration_date != None: # a =Timer.get_timestamp_now # if float(item_customer_bytesave.bytesave_expiration_date) < Timer.get_timestamp_now(): # status = 2 # else: # status = 1 item_customer_represent = Customer_Represent.objects.filter( id_customer=item.id).filter((~Q(is_del=1))) if item_customer_represent.count() > 0: stt = 0 for item_represent in item_customer_represent: data_represent.append({ 'stt': stt + 1, 'id': item_represent.id, 'id_customer': item_represent.id_customer, 'name': item_represent.name, 'phone_number': item_represent.phone_number, 'position': item_represent.position, 'email': item_represent.email, 'type': item_represent.type, 'is_del': item_represent.is_del, 'time_create_at': item_represent.time_create_at, 'time_update_at': item_represent.time_update_at, }) countdata_represent += 1 stt += 1 item_bytesave_cycle = bytesave_cycle.objects.filter( id_customer=item.id).filter((~Q(is_del=1))) data_cycle = [] countdata_cycle = 0 if item_bytesave_cycle.count() > 0: stt = 0 for item_cycle in item_bytesave_cycle: data_cycle.append({ 'stt': stt + 1, 'id': item_cycle.id, 'id_customer': item_cycle.id_customer, 'bytesave_amount_used': item_cycle.bytesave_amount_used, 'bytesave_duration': str(item_cycle.bytesave_duration) + (' tháng' if item_cycle.bytesave_time_type == 0 else ' năm'), 'bytesave_time_type': item_cycle.bytesave_time_type, 'bytesave_start_date': Convert_timestamp(item_cycle.bytesave_start_date), 'bytesave_expiration_date': Convert_timestamp( item_cycle.bytesave_expiration_date), }) countdata_cycle += 1 stt += 1 date_start = Convert_timestamp( item_customer_bytesave.bytesave_use_start_date) date_start = datetime.strptime(date_start, "%d/%m/%Y") date_start = convert_string_date_format(date_start) date_start = datetime.strptime(date_start, "%Y-%m-%d") data.append({ 'stt': i, 'id': item.id, 'id_loggin': item.id_loggin, 'id_version': item.id_version, 'customer_bytesave': { 'id': item_customer_bytesave.id, 'bytesave_email': item_customer_bytesave.bytesave_email if item_customer_bytesave.bytesave_email != None else '', 'bytesave_pwd': item_customer_bytesave.bytesave_pwd, #'bytesave_amount_used': item_customer_bytesave.bytesave_amount_used, #'bytesave_duration': item_customer_bytesave.bytesave_duration, 'bytesave_use_start_date': date_start, #'bytesave_time_type': item_customer_bytesave.bytesave_time_type, #'bytesave_expiration_date': Convert_timestamp( item_customer_bytesave.bytesave_expiration_date), }, 'name': item.name, 'email': item.email, 'type': item.type, 'phone_number': item.phone_number, 'city': item.city, 'address': item.address, 'tax_code': item.tax_code, 'website': item.website, 'fax': item.fax, 'legal_representative': item.legal_representative, 'scale': item.scale, 'field': item.field, 'is_upgrade_version': item.is_upgrade_version, 'time_create_at': item.time_create_at, 'time_update_at': item.time_update_at, 'customer_represent': data_represent, 'countdata_represent': countdata_represent, 'data_cycle': data_cycle, 'countdata_cycle': countdata_cycle, 'status': status, }) countdata = Customers.objects.all().count() return JsonResponse({ 'status': 'true', 'countdata': countdata, 'data': data }) except Exception as e: return JsonResponse({ 'status': 'false', 'countdata': 0, 'data': [], 'string_error': e }) if request.method == 'POST': data = json.dumps(request.POST) form = json.loads(data) is_upgrade_version = 0 if form.get('is_upgrade_version') == 'on': is_upgrade_version = 1 id = form.get('id', 0) if id == '0': # thêm mới try: Customers.create( **{ 'id_loggin': 0, 'id_version': Versions.objects.filter(~Q(is_del=1)).first().id, 'name': form.get('name'), 'email': form.get('email'), 'type': form.get('type'), 'phone_number': form.get('phone_number'), 'city': form.get('city'), 'address': form.get('address'), 'tax_code': form.get('tax_code'), 'website': form.get('website'), 'fax': form.get('fax'), 'legal_representative': form.get('legal_representative'), 'scale': form.get('scale'), 'field': form.get('field'), 'is_upgrade_version': is_upgrade_version, }) id_customer_max = Customers.objects.all().order_by("-id")[0].id # str_time_start = form.get('date_start_used').replace('-', '/') # date = datetime.strptime(str_time_start, "%Y/%m/%d") pwd = genpwd(12) Customer_ByteSave.create( **{ 'id_customer': id_customer_max, 'bytesave_email': form.get('email'), 'bytesave_pwd': encryption(pwd), 'bytesave_use_start_date': Timer.get_timestamp_now(), # 'bytesave_amount_used': form.get('bytesave_amount_used'), # 'bytesave_duration': form.get('bytesave_duration'), # 'bytesave_time_type': form.get('bytesave_time_type'), # 'bytesave_expiration_date': form.get('bytesave_expiration_date'), }) notification_customer_bytesave('[ByteSave]', form.get('email'), pwd, form.get('email')) if int(form.get('CountRowNDD')) > 0: for i in range(1, int(form.get('CountRowNDD')) + 1): Customer_Represent.create( **{ 'id_customer': id_customer_max, 'name': form.get('represent_name' + str(i)), 'phone_number': form.get('represent_phone_number' + str(i)), 'position': form.get('represent_position' + str(i)), 'email': form.get('represent_email' + str(i)), 'type': form.get('represent_type' + str(i)), }) if int(form.get('CountRowService')) > 0: for i in range(1, int(form.get('CountRowService')) + 1): id_metric = Metric_Services.create( **{ 'id_customer': id_customer_max, 'id_service': form.get('id_service' + str(i)), 'information_connect': form.get('information_connect' + str(i)), 'max_storage': form.get('max_storage' + str(i)), 'username_account': form.get('username_account' + str(i)), 'type': form.get('represent_type' + str(i)), }) connect_bytesave.create( **{ 'name': 'Connection ' + form.get('max_storage' + str(i)), 'id_customer': id_customer_max, 'id_agent': 0, 'id_metric_service': id_metric, 'type': 0, }) if int(form.get('CountRowChuki')) > 0: for i in range(1, int(form.get('CountRowChuki')) + 1): # date_end = add_months(date, int(form.get('bytesave_duration' + str(i))) if form.get( # 'bytesave_time_type' + str(i)) == '0' else (int(form.get('bytesave_duration' + str(i))) * 12)) date_start = datetime.strptime( (form.get('date_start_used' + str(i))).replace( '-', '/'), '%d/%m/%Y') date_end = datetime.strptime( (form.get('date_end_used' + str(i))).replace( '-', '/'), '%d/%m/%Y') bytesave_cycle.create( **{ 'id_customer': id_customer_max, 'bytesave_amount_used': form.get('bytesave_amount_used' + str(i)), 'bytesave_duration': form.get('bytesave_duration' + str(i)), 'bytesave_time_type': form.get('bytesave_time_type' + str(i)), 'bytesave_start_date': date_start.timestamp(), 'bytesave_expiration_date': date_end.timestamp(), }) return JsonResponse({ 'status': 'true', 'msg': 'Thêm mới thành công!', 'id': id_customer_max }) except Exception as e: return JsonResponse({ 'status': 'false', 'msg': 'Thêm mới không thành công!', 'string_error': e }) else: try: item = Customers.objects.get(id=id) item.name = form.get('name') item.email = form.get('email') item.scale = form.get('scale') item.field = form.get('field') item.city = form.get('city') item.time_update_at = Timer.get_timestamp_now() item.is_upgrade_version = is_upgrade_version item.type = form.get('type') item.phone_number = form.get('phone_number') item.address = form.get('address') item.tax_code = form.get('tax_code') item.website = form.get('website') item.fax = form.get('fax') item.legal_representative = form.get('legal_representative') item.save() item_bytesave = Customer_ByteSave.objects.get(id_customer=id) item_bytesave.bytesave_email = form.get('email') item_bytesave.save() return JsonResponse({ 'status': 'true', 'msg': 'Chỉnh sửa thành công!' }) except Exception as e: return JsonResponse({ 'status': 'false', 'msg': 'Chỉnh sửa không thành công!', 'string_error': e }) return None