Exemple #1
2
def addNew(request) :

    args = {}
    tt = TT()
    args.update(csrf(request))
    args['username'] = auth.get_user(request).username
    args['mform'] = MainTtForm
    args['aform'] = AdditionTtForm
    if request.POST :
        mform = MainTtForm(request.POST)
        aform = AdditionTtForm(request.POST)
        if mform.is_valid() and aform.is_valid():
            array_id_tt_saved = MainTtModel.objects.values('idTT')
            id_tt_saved = [i['idTT'] for i in array_id_tt_saved]
            try:
                get_idTT = int(request.POST.get('idTT'))
            except TypeError:
                args['error'] = 'Wrong! Must be integer'
                return render_to_response('addNew.html', args)
            if get_idTT in id_tt_saved:

                return HttpResponse('Wrong! This TT is exist')
            else:
                idTT = mform.save()
                addition = aform.save(commit=False)
                addition.tt = idTT
                addition.save()
                log = LogsModel()
                log.tt_id = idTT.id
                log.user = args['username']
                log.create_action(act=1, tt=str(idTT.idTT))
                log.save()
                if request.POST.get('city') not in tt.get_cities():
                    tt.set_sities(request.POST.get('city'))
                if request.POST.get('wiredProvider') not in tt.get_wired():
                    tt.set_wired(request.POST.get('wiredProvider'))
                if int(request.POST.get('idTT')) not in tt.get_id() :
                    tt.set_id(int(request.POST.get('idTT')))
                if request.POST.get('wireless') not in tt.get_wireless():
                    tt.set_wireless(request.POST.get('wireless'))

                return HttpResponse('success')
        else :
            args['cities'] = tt.get_cities()
            args['wireds'] = tt.get_wired()
            args['error'] = "Form isn't valid!!!"
            return HttpResponse("Form isn't valid!!!")

    else:
        args['cities'] = tt.get_cities()
        args['wireds'] = tt.get_wired()
        return render_to_response('addNew.html', args)
Exemple #2
1
def migrate_tt(request, tt_id) :

    def error_dry() :

        args.update(csrf(request))
        args['username'] = auth.get_user(request).username
        main_model = MainTtModel.objects.get(idTT=tt_id)
        addition_model = AdditionInfoTtModel.objects.get(tt_id=main_model.__dict__['id'])
        args['main'] = main_model
        args['addition'] = addition_model
        args['migrate'] = MigrateForm()

    args = {}

    if request.POST :
        form = MigrateForm(request.POST)
        if form.is_valid() :
            mikrotik = request.POST.get('mikrotik' , None)
            new_tt = request.POST.get('new_tt', None)
            try:
                int(new_tt)
            except:
                args['error'] = 'New TT number nust be integer not %s' %new_tt
                error_dry()

                return render_to_response('ttInfo.html', args)

            if new_tt is not None :
                if form.migrate_save(new=new_tt, old=tt_id) :
                    log = LogsModel()
                    log.user = auth.get_user(request).username
                    new_tt_log = LogsModel()
                    new_tt_log.user = auth.get_user(request).username
                    log.tt_id = MainTtModel.objects.get(idTT=tt_id).id
                    new_tt_log.tt_id = MainTtModel.objects.get(idTT=tt_id).id

                    if mikrotik != None :

                        migrate_mikr = Mikrotik(old=int(tt_id), new=int(new_tt))
                        try:
                            migrate_mikr.connect_ssh(host=SshConnect.make_domain(tt_id)) 
                            migrate_mikr.connect_sftp(host=SshConnect.make_domain(tt_id))
                        except :
                            args['error'] = 'Server cannot connection to TT%s' %(str(tt_id))
                            error_dry()

                            return render_to_response('ttInfo.html', args)

                        backup_old_name = migrate_mikr.make_name()
                        migrate_mikr.easy_execute(command=Mikrotik.get_commands('create_backup')(backup_old_name))
                        exist_files = migrate_mikr.get_files_model(migrate_mikr.id_old)
                        if exist_files.backup : 
                            print('file exist')
                            os.remove(exist_files.backup.path)
                            migrate_mikr.sftp_get(backup_old_name, exist_files.backup.path)
                        else:
                            print('file dont exist')
                            backup_name = migrate_mikr.create_file_name(migrate_mikr.identify_old)
                            upload_path = migrate_mikr.create_file_path(backup_name)
                            migrate_mikr.sftp_get(backup_old_name, upload_path)
                            
                        new_files = migrate_mikr.get_files_model(migrate_mikr.id_new)

                        if new_files.crt.name and new_files.key.name :
                            upload_crt_name = os.path.basename(new_files.crt.name)
                            upload_key_name = os.path.basename(new_files.key.name)
                            cert_old = migrate_mikr.get_certficete()
                            migrate_mikr.sftp_put(new_files.crt.path, upload_crt_name)
                            migrate_mikr.sftp_put(new_files.key.path, upload_key_name)
                            sleep(4)
                            migrate_mikr.easy_execute(Mikrotik.get_commands('create_certificate')(upload_crt_name))
                            sleep(1)
                            migrate_mikr.easy_execute(Mikrotik.get_commands('create_certificate')(upload_key_name))
                            cert_new = migrate_mikr.get_certficete()
                            cert_add = migrate_mikr.verif_new_cert(cert_old, cert_new)
                            if cert_add:
                                ovpn_iface = migrate_mikr.ovpn_client_interface()
                                if len(ovpn_iface) != 1 :
                                    args['error'] = 'TT%s has %s interfaces!!!' %(migrate_mikr.identify_old, ovpn_iface)
                                    error_dry()

                                    return render_to_response('ttInfo.html', args)
                                
                                migrate_mikr.easy_execute(Mikrotik.get_commands('set_identity')(migrate_mikr.identify_new))
                                migrate_mikr.easy_execute(Mikrotik.get_commands('set_ovpn_cert')(ovpn_iface[0], cert_add),
                                                          wait=False)
                                print('before sleep')
                                sleep(2)
                                print('sleep')
                                migrate_mikr.__del__()
                                print('del')
                            else:
                                args['error'] = 'New certificate has not been created!!!'
                                error_dry()

                                return render_to_response('ttInfo.html', args)

                        else:
                            error = []
                            if not new_files.crt.name :
                                error.append('CRT')
                            if not new_files.key.name :
                                error.append('KEY')
                            args['error'] = error.__str__()
                            error_dry()

                            return render_to_response('ttInfo.html', args)

                        log.create_action(act=3, tt=str(tt_id), migrate_to=str(new_tt), mikrotik=True)
                        new_tt_log.create_action(act=4, tt=str(new_tt), migrate_from=str(tt_id), mikrotik=True)

                    else:
                        print('log create True')
                        log.create_action(act=3, tt=str(tt_id), migrate_to=str(new_tt), mikrotik=False)
                        new_tt_log.create_action(act=4, tt=str(new_tt), migrate_from=str(tt_id), mikrotik=False)
                    log.save()
                    new_tt_log.tt_id = MainTtModel.objects.get(idTT=new_tt).id
                    new_tt_log.save()

                    return redirect('/list/tt_info/%s/' %new_tt)

                else:
                    args['error'] = 'Error in creation new TT%s' %new_tt
                    error_dry()

                    return render_to_response('ttInfo.html', args)
            else:
                args['error'] = 'New TT%s is not correct' %new_tt
                error_dry()

                return render_to_response('ttInfo.html', args)
        else:
            args['error'] = 'Request must be post'
            error_dry()

            return render_to_response('ttInfo.html', args)
Exemple #3
0
def update_tt(request) :

    if request.POST :
        tt = TT()
        try:
            ttid = int(request.POST.get('idTT'))
        except :
            return HttpResponse('ajaxError')

        parameter = {}

        if ttid in tt.idTT :
            transform = {'city' : 'city',
                         'addr' : 'address',
                         'phone' : 'phone',
                         'wireless' : 'wireless',
                         'wired' : 'wiredProvider',
                         'contract' : 'contractId',
                         'support' : 'supportCont1'}
            col = request.POST.get('column')
            val = request.POST.get('value')
            tt_info = MainTtModel.objects.get(idTT=ttid)
            parameter[transform[col]] = [tt_info.__dict__[transform[col]]]
            tt_info.__dict__[transform[col]] = val
            tt_info.save()
            log = LogsModel()
            log.user = auth.get_user(request).username
            log.tt_id = tt_info.id
            parameter[transform[col]].append(val)
            log.create_action(act=2, tt=str(ttid), parameters=parameter)
            log.save()

            if col == 'city' :
                if val not in tt.get_cities() :
                    tt.set_sities(val)
            if col == 'wireless' :
                if val not in tt.get_wireless() :
                    tt.set_wireless(val)
            if col == 'wired' :
                if val not in tt.get_wired() :
                    tt.set_wired(val)

            return HttpResponse('success')
        else:
            return HttpResponse('id is not in DB')
    else:
        return HttpResponse('ajaxError')
Exemple #4
0
def update_item(request, tt_id) :

    args = {}
    parameter = {}
    if request.POST :

        try:
            main = MainTtModel.objects.get(idTT=int(tt_id))
        except:
            args['error'] = 'TT is not in MainModel'
            responce = JsonResponse(args)

            return HttpResponse(responce)

        item_column = request.POST.get('column', None)
        item_value = request.POST.get('value', '')

        if (set({item_column}) & set(main.__dict__)) :

            tt = TT()
            parameter[item_column] = [main.__dict__[item_column]]
            main.__dict__[item_column] = item_value
            parameter[item_column].append(item_value)
            args['success'] = item_value
            responce = JsonResponse(args)
            main.save()
            tt.verification(item_column, item_value)
            log = LogsModel()
            log.tt_id = main.id
            log.user = auth.get_user(request).username
            log.create_action(act=2, tt=str(tt_id), parameters=parameter)
            log.save()

            return HttpResponse(responce)

        try:
            addition = AdditionInfoTtModel.objects.get(tt_id=main.__dict__['id'])
        except:
            args['error'] = 'TT is not in AdditionInfoTtModel'
            responce = JsonResponse(args)

            return HttpResponse(responce)

        if (set({item_column}) & set(addition.__dict__)) :

            parameter[item_column] = [addition.__dict__[item_column]]
            addition.__dict__[item_column] = item_value
            parameter[item_column].append(item_value)
            args['success'] = item_value
            print(item_column, item_value)
            addition.save()
            responce = JsonResponse(args)
            log = LogsModel()
            log.tt_id = main.id
            log.user = auth.get_user(request).username
            log.create_action(act=2, tt=str(tt_id), parameters=parameter)
            log.save()

            return HttpResponse(responce)

        else:
            args['error'] = 'Some bug call dev'

            return HttpResponse(args['error'])

    else:
        args['error'] = 'Request must be POST'

        return HttpResponse(args['error'])
Exemple #5
0
def tt_info (request, tt_id) :

    args = {}
    args.update(csrf(request))
    args['username'] = auth.get_user(request).username
    main_model = MainTtModel.objects.get(idTT=tt_id)
    addition_model = AdditionInfoTtModel.objects.get(tt_id=main_model.__dict__['id'])
    args['main'] = main_model
    args['addition'] = addition_model
    args['migrate'] = MigrateForm()

    if request.POST or request.FILES:

        if request.FILES :
            print(request.FILES)
            CRT = request.FILES.get('upload-crt', None)
            KEY = request.FILES.get('upload-key', None)
            BACKUP = request.FILES.get('upload-backup', None)
            try:
                files_tt = FilesModel.objects.get(tt_id=main_model.__dict__['id'])

                if CRT != None :
                    if CRT.name[-4 :] == '.crt':
                        if files_tt.crt.name :
                            files_tt.delete(row='crt')
                        files_tt.crt = CRT

                if KEY != None :
                    if KEY.name[-4 :] == '.key':
                        if files_tt.key.name :
                            files_tt.delete(row='key')
                        files_tt.key = KEY

                if BACKUP != None :
                    if BACKUP.name[-7 :] == '.backup':
                        if files_tt.backup.name :
                            files_tt.delete(row='backup')
                        files_tt.backup = BACKUP

            except ObjectDoesNotExist :
                files_tt = FilesModel(crt=CRT, key=KEY, backup=BACKUP, tt_id=main_model.__dict__['id'])

            files_tt.save()

        if request.POST :

            print(request.POST)
            json_main = request.POST.get('main', '')
            main = simplejson.loads(json_main)
            parameter = {}

            if main :
                tt = TT()
                for key in main :
                    parameter[key] = [main_model.__dict__[key]]
                    main_model.__dict__[key] = main[key]
                    parameter[key].append(main[key])
                    tt.verification(key, main[key])
                main_model.save()

            json_addition = request.POST.get('addition' , '')
            addition = simplejson.loads(json_addition)
            if addition :
                for key in addition :
                    parameter[key] = [addition_model.__dict__[key]]
                    addition_model.__dict__[key] = addition[key]
                    parameter[key].append(addition[key])
                addition_model.save()

            log = LogsModel()
            log.tt_id = main_model.id
            log.user = args['username']
            log.create_action(act=2, tt=str(tt_id), parameters=parameter)
            log.save()

        return HttpResponse('success')

    else:

        return render_to_response('ttInfo.html', args)