def do_destroying_comp(request): try: post = request.POST id = post['id'] comp = Computing.objects.get(id=id) message = Message(comp.note) message.append({ 'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': get_comp_destroying_log() }) note = message.tostring() packed_update_computing(request, id, { 'status': DESTROYING_KEY, 'note': note }, log=get_comp_destroying_log()) send_user_mail(comp.account, 'Aglaia Item Notify', get_comp_destroying_mail()) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message(request, 'Destroy computing failed: ' + e.__str__())
def do_approve_return(request): try: post = request.POST id = post['id'] note = post['note'] comp = Computing.objects.get(id=id) if not comp.status == RETURNING_KEY: return show_denied_message(request) if note == '': note = get_comp_reted_log() message = Message(comp.note) message.append({'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': note}) note = message.tostring() packed_update_computing( request, id, { 'status': RETURNED_KEY, 'note': note}, log=get_comp_reted_log()) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Something wrong when approve return: ' + e.__str__())
def do_disapprove_modify(request): try: post = request.POST id = post['id'] note = post['note'] comp = Computing.objects.get(id=id) if not comp.status == MODIFY_APPLY_KEY: return show_denied_message(request) if note == '': note = get_comp_rej_modf_log() message = Message(comp.note) message.append({'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': note}) note = message.tostring() packed_update_computing( request, id, { 'status': BORROWED_KEY, 'note': note}, log=get_comp_rej_modf_log()) send_notify_mail(request, CompRejectModfMail, comp=comp) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Something wrong when disapprove modify: ' + e.__str__())
def do_disapprove_modify(request): try: post = request.POST id = post['id'] note = post['note'] comp = Computing.objects.get(id=id) if not comp.status == MODIFY_APPLY_KEY: return show_denied_message(request) if note == '': note = get_comp_rej_modf_log() message = Message(comp.note) message.append({ 'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': note }) note = message.tostring() packed_update_computing(request, id, { 'status': BORROWED_KEY, 'note': note }, log=get_comp_rej_modf_log()) send_notify_mail(request, CompRejectModfMail, comp=comp) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Something wrong when disapprove modify: ' + e.__str__())
def do_approve_return(request): try: post = request.POST id = post['id'] note = post['note'] comp = Computing.objects.get(id=id) if not comp.status == RETURNING_KEY: return show_denied_message(request) if note == '': note = get_comp_reted_log() message = Message(comp.note) message.append({ 'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': note }) note = message.tostring() packed_update_computing(request, id, { 'status': RETURNED_KEY, 'note': note }, log=get_comp_reted_log()) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Something wrong when approve return: ' + e.__str__())
def do_destroying_comp(request): try: post = request.POST id = post['id'] comp = Computing.objects.get(id=id) message = Message(comp.note) message.append({'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': get_comp_destroying_log()}) note = message.tostring() packed_update_computing( request, id, { 'status': DESTROYING_KEY, 'note': note}, log=get_comp_destroying_log()) send_user_mail( comp.account, 'Aglaia Item Notify', get_comp_destroying_mail()) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Destroy computing failed: ' + e.__str__())
def do_modif_request(request): try: post = request.POST id = post['id'] comp = Computing.objects.get(id=id) if not comp.status == BORROWED_KEY: return HttpResponse('denied') if post['reason'] == '': post['reason'] = get_comp_modif_log() message = Message(comp.note) message.append({ 'direction': 'Recv', 'info_type': '', 'user_name': request.user.username, 'text': post['reason'] }) note = message.tostring() packed_update_computing(request, id, { 'status': MODIFY_APPLY_KEY, 'note': note }, log=get_comp_modif_log()) send_notify_mail(request, CompModfApplyMail, comp=comp) return HttpResponse('ok') except: return HttpResponse('denied')
def do_modif_request(request): try: post = request.POST id = post['id'] comp = Computing.objects.get(id=id) if not comp.status == BORROWED_KEY: return HttpResponse('denied') if post['reason'] == '': post['reason'] = get_comp_modif_log() message = Message(comp.note) message.append({'direction': 'Recv', 'info_type': '', 'user_name': request.user.username, 'text': post['reason']}) note = message.tostring() packed_update_computing( request, id, { 'status': MODIFY_APPLY_KEY, 'note': note}, log=get_comp_modif_log()) send_notify_mail(request, CompModfApplyMail, comp=comp) return HttpResponse('ok') except: return HttpResponse('denied')
def do_return_request(request): try: id = request.POST['id'] comp = Computing.objects.get(id=id) if not (comp.status == DESTROYING_KEY or comp.status == BORROWED_KEY or comp.status == MODIFY_APPLY_KEY): return HttpResponse('denied') message = Message() message.append({ 'direction': 'Recv', 'info_type': '', 'user_name': request.user.username, 'text': get_comp_ret_log() }) packed_update_computing(request, id, { 'status': RETURNING_KEY, 'note': message.tostring() }, log=get_comp_ret_log()) send_notify_mail(request, CompReturnMail, comp=comp) return HttpResponseRedirect(reverse('goods.views.show_borrow')) # return HttpResponse('ok') except: return HttpResponse('denied')
def setUp(self): self.message = Message(max_num=5) self.assertEqual( self.message.getTime()['Recv_Readed_Time'] < time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(time.time())), True) self.assertEqual( self.message.getTime()['Send_Readed_Time'] < time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(time.time())), True)
def do_borrow_request(request): post = request.POST comp = {} try: p_nm = no_excp_post(request, 'package') if p_nm != 'none': get_pack_prop(comp, p_nm) else: if post['type'] == 'real': comp['pc_type'] = PHYSICAL_MACHINE_KEY else: comp['pc_type'] = VIRTUAL_MACHINE_KEY comp['cpu'] = post['cpu'] comp['memory'] = post['memory'] comp['disk'] = post['disk'] if post['disk_type'] == 'SSD': comp['disk_type'] = SSD_KEY else: comp['disk_type'] = MACHINE_KEY comp['os'] = post['os'] comp['pack_name'] = 'user_defined' comp['expire_time'] = datetime.now() comp['login'] = post['login'] comp['password'] = post['initial_password'] comp['status'] = VERIFYING_KEY comp['account'] = Account.objects.get(user=request.user) comp['address'] = UNKNOWN_ADDR comp['note'] = post['reason'] comp['flag'] = post['flag'] comp['data_content'] = post['data_content'] if comp['note'] == '': comp['note'] = get_comp_request_log() message = Message() message.append({ 'direction': 'Recv', 'info_type': '', 'user_name': request.user.username, 'text': comp['note'] }) comp['note'] = message.tostring() if 'name' in post: comp['name'] = post['name'] else: comp['name'] = '' c = packed_create_computing(request, [comp], log=get_comp_request_log()) send_notify_mail(request, CompRequestMail, comp=c[0]) return HttpResponse('ok') except Exception as e: return HttpResponse('denied')
def do_borrow_request(request): post = request.POST comp = {} try: p_nm = no_excp_post(request, 'package') if p_nm != 'none': get_pack_prop(comp, p_nm) else: if post['type'] == 'real': comp['pc_type'] = PHYSICAL_MACHINE_KEY else: comp['pc_type'] = VIRTUAL_MACHINE_KEY comp['cpu'] = post['cpu'] comp['memory'] = post['memory'] comp['disk'] = post['disk'] if post['disk_type'] == 'SSD': comp['disk_type'] = SSD_KEY else: comp['disk_type'] = MACHINE_KEY comp['os'] = post['os'] comp['pack_name'] = 'user_defined' comp['expire_time'] = datetime.now() comp['login'] = post['login'] comp['password'] = post['initial_password'] comp['status'] = VERIFYING_KEY comp['account'] = Account.objects.get(user=request.user) comp['address'] = UNKNOWN_ADDR comp['note'] = post['reason'] comp['flag'] = post['flag'] comp['data_content'] = post['data_content'] if comp['note'] == '': comp['note'] = get_comp_request_log() message = Message() message.append({'direction': 'Recv', 'info_type': '', 'user_name': request.user.username, 'text': comp['note']}) comp['note'] = message.tostring() if 'name' in post: comp['name'] = post['name'] else: comp['name'] = '' c = packed_create_computing( request, [comp], log=get_comp_request_log()) send_notify_mail(request, CompRequestMail, comp=c[0]) return HttpResponse('ok') except Exception as e: return HttpResponse('denied')
def do_approve_modify(request): try: post = request.POST id = post['id'] comp = Computing.objects.get(id=id) if not comp.status == MODIFY_APPLY_KEY: return show_denied_message(request) note = post['note'] if note == '': note = get_comp_modfed_log() message = Message(comp.note) message.append({ 'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': note }) note = message.tostring() dic = {} dic['status'] = BORROWED_KEY dic['note'] = note if post['type'] == 'real': dic['pc_type'] = PHYSICAL_MACHINE_KEY else: dic['pc_type'] = VIRTUAL_MACHINE_KEY if post['disktype'] == 'SSD': dic['disk_type'] = SSD_KEY else: dic['disk_type'] = MACHINE_KEY dic['cpu'] = post['cpu'] dic['memory'] = post['memory'] dic['disk'] = post['disk'] dic['address'] = post['ip'] dic['os'] = post['os'] dic['data_content'] = post['data_content'] # dic['login'] = post['login'] # dic['password'] = post['initial_password'] packed_update_computing(request, id, dic, log=get_comp_modfed_log()) send_notify_mail(request, CompModfedMail, comp=comp) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Something wrong when approve modify: ' + e.__str__())
def do_approve_modify(request): try: post = request.POST id = post['id'] comp = Computing.objects.get(id=id) if not comp.status == MODIFY_APPLY_KEY: return show_denied_message(request) note = post['note'] if note == '': note = get_comp_modfed_log() message = Message(comp.note) message.append({'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': note}) note = message.tostring() dic = {} dic['status'] = BORROWED_KEY dic['note'] = note if post['type'] == 'real': dic['pc_type'] = PHYSICAL_MACHINE_KEY else: dic['pc_type'] = VIRTUAL_MACHINE_KEY if post['disktype'] == 'SSD': dic['disk_type'] = SSD_KEY else: dic['disk_type'] = MACHINE_KEY dic['cpu'] = post['cpu'] dic['memory'] = post['memory'] dic['disk'] = post['disk'] dic['address'] = post['ip'] dic['os'] = post['os'] dic['data_content'] = post['data_content'] # dic['login'] = post['login'] # dic['password'] = post['initial_password'] packed_update_computing(request, id, dic, log=get_comp_modfed_log()) send_notify_mail(request, CompModfedMail, comp=comp) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Something wrong when approve modify: ' + e.__str__())
def test_tostring_origin(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5'}) tmp = Message(origin=self.message.tostring().decode()) self.assertEqual(self.message.tostring(), tmp.tostring())
def do_approve_borrow(request): try: post = request.POST id = post['id'] comp = Computing.objects.get(id=id) login = post['login'] pw = post['initial_password'] note = post['note'] sn = post['sn'] addr = post['ip'] if not comp.status == VERIFYING_KEY: return show_denied_message(request) if note == '': note = get_comp_approve_log() message = Message(comp.note) message.append({ 'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': note }) note = message.tostring() packed_update_computing(request, id, { 'status': BORROWED_KEY, 'note': note, 'sn': sn, 'login': login, 'password': pw, 'address': addr }, log=get_comp_approve_log()) send_notify_mail(request, CompApproveMail, comp=comp) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Something wrong when approve computing resource: ' + e.__str__())
def do_return_request(request): try: id = request.POST['id'] comp = Computing.objects.get(id=id) if not (comp.status == DESTROYING_KEY or comp.status == BORROWED_KEY or comp.status == MODIFY_APPLY_KEY): return HttpResponse('denied') message = Message() message.append({'direction': 'Recv', 'info_type': '', 'user_name': request.user.username, 'text': get_comp_ret_log()}) packed_update_computing( request, id, { 'status': RETURNING_KEY, 'note': message.tostring()}, log=get_comp_ret_log()) send_notify_mail(request, CompReturnMail, comp=comp) return HttpResponseRedirect(reverse('goods.views.show_borrow')) # return HttpResponse('ok') except: return HttpResponse('denied')
def test_pretty_print(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5' }) tmp = Message(origin=self.message.pretty_print().decode()) self.assertEqual(tmp.index(3)['direction'], 'Send') self.assertEqual(tmp.index(3)['info_type'], 'bbb') self.assertEqual(tmp.index(3)['info_data'], '222') self.assertEqual(tmp.index(3)['user_name'], '4') self.assertEqual(tmp.index(3)['text'], 'text4')
def test_pretty_print(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5'}) tmp = Message(origin=self.message.pretty_print().decode()) self.assertEqual(tmp.index(3)['direction'], 'Send') self.assertEqual(tmp.index(3)['info_type'], 'bbb') self.assertEqual(tmp.index(3)['info_data'], '222') self.assertEqual(tmp.index(3)['user_name'], '4') self.assertEqual(tmp.index(3)['text'], 'text4')
def setUp(self): self.message = Message(max_num=5) self.assertEqual( self.message.getTime()['Recv_Readed_Time'] < time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( time.time())), True) self.assertEqual( self.message.getTime()['Send_Readed_Time'] < time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( time.time())), True)
def test_tostring_origin(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5' }) tmp = Message(origin=self.message.tostring().decode()) self.assertEqual(self.message.tostring(), tmp.tostring())
def do_approve_borrow(request): try: post = request.POST id = post['id'] comp = Computing.objects.get(id=id) login = post['login'] pw = post['initial_password'] note = post['note'] sn = post['sn'] addr = post['ip'] if not comp.status == VERIFYING_KEY: return show_denied_message(request) if note == '': note = get_comp_approve_log() message = Message(comp.note) message.append({'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': note}) note = message.tostring() packed_update_computing(request, id, {'status': BORROWED_KEY, 'note': note, 'sn': sn, 'login': login, 'password': pw, 'address': addr}, log=get_comp_approve_log()) send_notify_mail(request, CompApproveMail, comp=comp) return HttpResponseRedirect( reverse('computing.views.show_comp_verify')) except Exception as e: return show_message( request, 'Something wrong when approve computing resource: ' + e.__str__())
def create_borrow(account, sn, status, note=Message().tostring()): single = None try: single = Single.objects.get(sn=sn) except: raise Exception("Invalid SN number") try: borrow = Borrow(single=single, status=status, note=note, account=account) borrow.save() except: raise Exception("Error in borrow create") return borrow
def get_context_computing(comp): dc = {} dc['id'] = comp.id dc['name'] = comp.name dc['status'] = comp.get_status_display() dc['package_name'] = comp.pack_name dc['type'] = comp.get_pc_type_display() dc['cpu'] = comp.cpu dc['memory'] = comp.memory dc['disk'] = comp.disk dc['disktype'] = comp.get_disk_type_display() dc['ip'] = comp.address dc['os'] = comp.os dc['login'] = comp.login dc['initial_password'] = comp.password # dc['expire_time'] = comp.expire_time.strftime('%Y-%m-%d') dc['name'] = comp.account.real_name dc['flag'] = comp.flag dc['sn'] = comp.sn dc['data_content'] = comp.data_content dc['note'] = Message(comp.note).last()['text'] return dc
class MessageCenterTestCase(TestCase): def setUp(self): self.message = Message(max_num=5) self.assertEqual( self.message.getTime()['Recv_Readed_Time'] < time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(time.time())), True) self.assertEqual( self.message.getTime()['Send_Readed_Time'] < time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(time.time())), True) def test_init(self): self.assertEqual(self.message.root.get('max_num'), '5') self.assertEqual(len(self.message.root), 0) self.assertRaises(Exception, Message, 'test') def test_append(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5' }) self.assertEqual(len(self.message.root), 5) self.assertEqual(self.message.root[0].tag, 'message') self.assertEqual(self.message.root[0].get('direction'), 'Recv') self.assertEqual(self.message.root[0].get('info_type'), 'test') self.assertEqual(self.message.root[0].find('user_name').text, '1') self.assertEqual(self.message.root[0].find('text').text, 'text1') self.assertEqual(self.message.root[0].find('info_data').text, 'TEST') def test_append_KeyError(self): arg = { 'direction': '123', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' } self.assertRaises(KeyError, self.message.append, arg) def test_append_Exception(self): arg = { 'direction1': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' } self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type1': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' } self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type': 'test', 'info_data1': 'TEST', 'user_name': '1', 'text': 'text1' } self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name1': '1', 'text': 'text1' } self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text1': 'text1' } self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type': 'test', 'user_name': '1', 'text': 'text1' } self.assertRaises(Exception, self.message.append, arg) def test_append_max_num(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '6', 'text': 'text6' }) self.assertEqual(len(self.message.root), 5) self.assertEqual(self.message.root[0].tag, 'message') self.assertEqual(self.message.root[0].get('direction'), 'Send') self.assertEqual(self.message.root[0].get('info_type'), 'aaa') self.assertEqual(self.message.root[0].find('user_name').text, '2') self.assertEqual(self.message.root[0].find('text').text, 'text2') self.assertEqual(self.message.root[0].find('info_data').text, '111') self.assertRaises(Exception, self.message.index, 6) def test_index(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5' }) self.assertEqual(self.message.index(3)['direction'], 'Send') self.assertEqual(self.message.index(3)['info_type'], 'bbb') self.assertEqual(self.message.index(3)['info_data'], '222') self.assertEqual(self.message.index(3)['user_name'], '4') self.assertEqual(self.message.index(3)['text'], 'text4') def test_last(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) last = self.message.last() self.assertEqual(self.message.last()['direction'], 'Send') self.assertEqual(self.message.last()['info_type'], 'bbb') self.assertEqual(self.message.last()['info_data'], '222') self.assertEqual(self.message.last()['user_name'], '3') self.assertEqual(self.message.last()['text'], 'text3') def test_tostring_origin(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5' }) tmp = Message(origin=self.message.tostring().decode()) self.assertEqual(self.message.tostring(), tmp.tostring()) def test_pretty_print(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5' }) tmp = Message(origin=self.message.pretty_print().decode()) self.assertEqual(tmp.index(3)['direction'], 'Send') self.assertEqual(tmp.index(3)['info_type'], 'bbb') self.assertEqual(tmp.index(3)['info_data'], '222') self.assertEqual(tmp.index(3)['user_name'], '4') self.assertEqual(tmp.index(3)['text'], 'text4') def test_sizeof(self): self.message.append({ 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1' }) self.message.append({ 'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3' }) self.message.append({ 'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4' }) self.message.append({ 'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5' }) self.assertEqual(self.message.__sizeof__(), 5)
class MessageCenterTestCase(TestCase): def setUp(self): self.message = Message(max_num=5) self.assertEqual( self.message.getTime()['Recv_Readed_Time'] < time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( time.time())), True) self.assertEqual( self.message.getTime()['Send_Readed_Time'] < time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( time.time())), True) def test_init(self): self.assertEqual(self.message.root.get('max_num'), '5') self.assertEqual(len(self.message.root), 0) self.assertRaises(Exception, Message, 'test') def test_append(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5'}) self.assertEqual(len(self.message.root), 5) self.assertEqual(self.message.root[0].tag, 'message') self.assertEqual(self.message.root[0].get('direction'), 'Recv') self.assertEqual(self.message.root[0].get('info_type'), 'test') self.assertEqual(self.message.root[0].find('user_name').text, '1') self.assertEqual(self.message.root[0].find('text').text, 'text1') self.assertEqual(self.message.root[0].find('info_data').text, 'TEST') def test_append_KeyError(self): arg = { 'direction': '123', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'} self.assertRaises(KeyError, self.message.append, arg) def test_append_Exception(self): arg = { 'direction1': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'} self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type1': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'} self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type': 'test', 'info_data1': 'TEST', 'user_name': '1', 'text': 'text1'} self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name1': '1', 'text': 'text1'} self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text1': 'text1'} self.assertRaises(Exception, self.message.append, arg) arg = { 'direction': 'Recv', 'info_type': 'test', 'user_name': '1', 'text': 'text1'} self.assertRaises(Exception, self.message.append, arg) def test_append_max_num(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '6', 'text': 'text6'}) self.assertEqual(len(self.message.root), 5) self.assertEqual(self.message.root[0].tag, 'message') self.assertEqual(self.message.root[0].get('direction'), 'Send') self.assertEqual(self.message.root[0].get('info_type'), 'aaa') self.assertEqual(self.message.root[0].find('user_name').text, '2') self.assertEqual(self.message.root[0].find('text').text, 'text2') self.assertEqual(self.message.root[0].find('info_data').text, '111') self.assertRaises(Exception, self.message.index, 6) def test_index(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5'}) self.assertEqual(self.message.index(3)['direction'], 'Send') self.assertEqual(self.message.index(3)['info_type'], 'bbb') self.assertEqual(self.message.index(3)['info_data'], '222') self.assertEqual(self.message.index(3)['user_name'], '4') self.assertEqual(self.message.index(3)['text'], 'text4') def test_last(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) last = self.message.last() self.assertEqual(self.message.last()['direction'], 'Send') self.assertEqual(self.message.last()['info_type'], 'bbb') self.assertEqual(self.message.last()['info_data'], '222') self.assertEqual(self.message.last()['user_name'], '3') self.assertEqual(self.message.last()['text'], 'text3') def test_tostring_origin(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5'}) tmp = Message(origin=self.message.tostring().decode()) self.assertEqual(self.message.tostring(), tmp.tostring()) def test_pretty_print(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5'}) tmp = Message(origin=self.message.pretty_print().decode()) self.assertEqual(tmp.index(3)['direction'], 'Send') self.assertEqual(tmp.index(3)['info_type'], 'bbb') self.assertEqual(tmp.index(3)['info_data'], '222') self.assertEqual(tmp.index(3)['user_name'], '4') self.assertEqual(tmp.index(3)['text'], 'text4') def test_sizeof(self): self.message.append( {'direction': 'Recv', 'info_type': 'test', 'info_data': 'TEST', 'user_name': '1', 'text': 'text1'}) self.message.append( {'direction': 'Send', 'info_type': 'aaa', 'info_data': '111', 'user_name': '2', 'text': 'text2'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '3', 'text': 'text3'}) self.message.append( {'direction': 'Send', 'info_type': 'bbb', 'info_data': '222', 'user_name': '4', 'text': 'text4'}) self.message.append( {'direction': 'Send', 'info_type': '', 'user_name': '5', 'text': 'text5'}) self.assertEqual(self.message.__sizeof__(), 5)
def import_computing(request): try: mutex.acquire() if request.POST['type'] == 'create': expire_time = datetime.datetime.strptime( request.POST['expire_time'], '%Y-%m-%d') account = Account.objects.all().filter( user__username=request.POST['user'])[0] STATUS_CHOICES = { VERIFYING: VERIFYING_KEY, VERIFY_FAIL: VERIFY_FAIL_KEY, VERIFY_SUCCESS: VERIFY_SUCCESS_KEY, BORROWED: BORROWED_KEY, MODIFY_APPLY: MODIFY_APPLY_KEY, RETURNING: RETURNING_KEY, RETURNED: RETURNED_KEY, } TYPE_CHOICES = { '实体机': PHYSICAL_MACHINE_KEY, '虚拟机': VIRTUAL_MACHINE_KEY} DISK_CHOICES = {MACHINE: MACHINE_KEY, SSD: SSD_KEY} status = STATUS_CHOICES[request.POST['status']] pc_type = TYPE_CHOICES[request.POST['pc_type']] disk_type = DISK_CHOICES[request.POST['disk_type']] message = Message() message.append({'direction': 'Send', 'info_type': '', 'user_name': request.user.username, 'text': '管理员创建'}) computing = Computing( pc_type=pc_type, cpu=request.POST['cpu'], memory=int( float( request.POST['memory'])), disk=int( float( request.POST['disk'])), disk_type=disk_type, os=request.POST['os'], sn=request.POST['sn'], expire_time=expire_time, login=request.POST['login'], password=request.POST['password'], status=status, account=account, note=message.tostring(), address=request.POST['ip'], flag=request.POST['flag'], name=request.POST['name'], pack_name=request.POST['pack_name'], data_content=request.POST['data_content']) computing.save() if request.POST['type'] == 'change': computing = Computing.objects.all().filter( sn=request.POST['sn'])[0] note = computing.note computing.delete() expire_time = datetime.datetime.strptime( request.POST['expire_time'], '%Y-%m-%d') account = Account.objects.all().filter( user__username=request.POST['user'])[0] STATUS_CHOICES = { VERIFYING: VERIFYING_KEY, VERIFY_FAIL: VERIFY_FAIL_KEY, VERIFY_SUCCESS: VERIFY_SUCCESS_KEY, BORROWED: BORROWED_KEY, MODIFY_APPLY: MODIFY_APPLY_KEY, RETURNING: RETURNING_KEY, RETURNED: RETURNED_KEY, } TYPE_CHOICES = { '实体机': PHYSICAL_MACHINE_KEY, '虚拟机': VIRTUAL_MACHINE_KEY} DISK_CHOICES = {MACHINE: MACHINE_KEY, SSD: SSD_KEY} status = STATUS_CHOICES[request.POST['status']] pc_type = TYPE_CHOICES[request.POST['pc_type']] disk_type = DISK_CHOICES[request.POST['disk_type']] computing = Computing( pc_type=pc_type, cpu=request.POST['cpu'], memory=int( float( request.POST['memory'])), disk=int( float( request.POST['disk'])), disk_type=disk_type, os=request.POST['os'], sn=request.POST['sn'], expire_time=expire_time, login=request.POST['login'], password=request.POST['password'], status=status, account=account, note=note, address=request.POST['ip'], flag=request.POST['flag'], name=request.POST['name'], pack_name=request.POST['pack_name'], data_content=request.POST['data_content']) computing.save() mutex.release() return HttpResponse('Success') except Exception as e: mutex.release() return HttpResponse('Error ' + e.__str__())