Ejemplo n.º 1
0
def update_password(request, id):
    try:
        models.Employee.objects.get(id=request.user.id)
    except:
        return JsonResponse({'message': 'Доступ запрещен'})
    json_new_password = {}

    client = models.Clients.objects.get(id=id)
    password = User.objects.make_random_password(length=9,
                                                 allowed_chars='123456789')

    try:
        for device in client.select_clients_group.select_server.all():
            action_mikrotik = ActionMikroTik(device)
            action_mikrotik.update_password(client.login, password)
    except:
        json_new_password.update({
            'password': '******',
            'error': True
        })
    else:
        user = User.objects.get(id=client.user.id)
        user.set_password(password)
        user.save()
        json_new_password.update({'password': password})
        client.key = password
        client.save()

    return JsonResponse(json_new_password)
Ejemplo n.º 2
0
    def save(self,
             force_insert=False,
             force_update=False,
             using=None,
             update_fields=None):

        if self.add_rule_in_device:
            try:
                for device in Servers.objects.filter(
                        physical_network_id=self.select_physicalnetwork):
                    action_mikrotik = ActionMikroTik(device)
                    action_mikrotik.add_tarif(self.speed_up,
                                              self.speed_up_unit,
                                              self.speed_down,
                                              self.speed_down_unit)
                    return super().save(force_insert=False,
                                        force_update=False,
                                        using=None,
                                        update_fields=None)
            except:
                return False
        else:
            return super().save(force_insert=False,
                                force_update=False,
                                using=None,
                                update_fields=None)
Ejemplo n.º 3
0
def clients_off():
    off_clients = ' '
    emails = [
        admin.admin.user.email
        for admin in models.SendEmailAdmin.objects.filter(event='clients_off')
    ]
    for client in models.Clients.objects.filter(internet_status=False,
                                                deleted_user=False,
                                                exemption=False):
        off_clients += ' ' + client.login
        for device in client.select_clients_group.select_server.all():
            try:
                action_mikrotik = ActionMikroTik(device)
                action_mikrotik.off_client(client.ip_address)
            except:
                bl.send_email(
                    emails,
                    'app_admin/email_templates/server_is_not_available.html',
                    'Новый Месяц Client OFF',
                    date=datetime.date.today(),
                    server_title=device.title)

    bl.send_email(emails,
                  'app_admin/email_templates/new_month_client_off.html',
                  'Новый Месяц Client OFF',
                  date=datetime.date.today(),
                  off_clients=off_clients)
Ejemplo n.º 4
0
def pay_balance(client, admin, sum, temporary_pay, temporary_pay_model,
                report_pay_admin_model):
    json_balance = {}

    before_balance = client.balance

    client.balance = int(client.balance) + int(sum)

    old_end_used_date = client.end_used_date

    if (int(client.balance) >= 0):
        try:
            for device in client.select_clients_group.select_server.all():
                action_mikrotik = ActionMikroTik(device)
                action_mikrotik.on_client(client.ip_address)
                json_balance.update({'message': 'Счет пополнен'})
        except:
            json_balance.update({'message': 'Сервер не доступен'})
        else:
            client.internet_status = True

            now_date = datetime.date.today()
            if client.end_used_date <= now_date:

                if client.fix_work_period:
                    if client.fix_work_period:
                        new_end_used_date = now_date.replace(
                            day=1) + relativedelta(months=1)
                    else:
                        new_end_used_date = now_date + relativedelta(months=1)
                else:
                    if client.select_tarif.fix_work_period:
                        new_end_used_date = now_date.replace(
                            day=1) + relativedelta(months=1)
                    else:
                        new_end_used_date = now_date + relativedelta(months=1)
                client.end_used_date = new_end_used_date
            client.save()

    else:
        json_balance.update({'message': 'Недостаточно средств'})
        client.save()

    if int(temporary_pay) == 1:
        temporary_pay_model.objects.create(user=client, price=int(sum))
    # Добавляем запись в отчет о пополнениях
    report_pay_admin_model.objects.create(
        id_admin_select=admin.profile_employee,
        id_client_select=client,
        sum=sum,
        before_balance=before_balance,
        after_balance=client.balance,
        before_date_off=old_end_used_date)

    json_balance.update({'balance': client.balance})
    return json_balance
Ejemplo n.º 5
0
    def get_context_data(self, **kwargs):
        context = super(ClientsConnectionList, self).get_context_data(**kwargs)

        client_group = models.ClientsGroups.objects.get(
            id=self.kwargs.get('pk'))

        for device in client_group.select_server.all():
            action_mikrotik = ActionMikroTik(device)

        context['ppp_active'] = action_mikrotik.get_ppp_active()

        return context
Ejemplo n.º 6
0
    def form_valid(self, form):
        client = models.Clients.objects.get(id=self.kwargs.get('pk'))
        try:
            for device in client.select_clients_group.select_server.all():
                action_mikrotik = ActionMikroTik(device)
                action_mikrotik.delete_user(client.login, client.ip_address)
        except:
            form._errors.setdefault("captcha", 'Устройство не доступно')
            return self.form_invalid(form)

        client.deleted_user = True
        client.save()
        return super(ClientDelete, self).form_valid(form)
Ejemplo n.º 7
0
def getStatusClient(request, client_id):
    try:
        request.user.profile_employee
    except:
        return JsonResponse({'ERROR': 'No Admin'})

    client = models.Clients.objects.get(id=client_id)

    json_status_info = {}

    try:
        arp_records = 0
        for device in client.select_clients_group.select_server.all():
            action_mikrotik = ActionMikroTik(device)
            if action_mikrotik.get_arp(client.ip_address):
                arp_records += 1

        ppp_connect = 0
        for device in client.select_clients_group.select_server.all():
            action_mikrotik = ActionMikroTik(device)
            if action_mikrotik.get_ppp(client.login):
                ppp_connect = action_mikrotik.get_ppp(client.login)

        json_status_info.update({'message': 'Данные получены'})
        if arp_records > 0:
            json_status_info.update({'arp': True})
        else:
            json_status_info.update({'arp': False})

        if ppp_connect != 0:
            json_status_info.update({'ppp': ppp_connect})
        else:
            json_status_info.update({'ppp': False})
    except:
        json_status_info.update({'message': 'Сервер не доступен'})

    return JsonResponse(json_status_info)
Ejemplo n.º 8
0
    def form_valid(self, form):
        if 'add' not in models.Employee.ROLE_METHODS[
                self.request.user.profile_employee.permissions_role]:
            return redirect(
                reverse_lazy('pythonix_admin:client_info',
                             kwargs={'pk': self.object.id}))

        client = models.Clients.objects.get(id=self.kwargs["pk"])
        if client.ip_address != form.cleaned_data['ip_address']:
            if models.Clients.objects.filter(
                    ip_address=form.cleaned_data['ip_address'],
                    select_clients_group=client.select_clients_group).count(
                    ) > 0:
                form._errors.setdefault("ip_address", 'ip address занят')
                return self.form_invalid(form)
            else:
                try:
                    for device in client.select_clients_group.select_server.all(
                    ):
                        action_mikrotik = ActionMikroTik(device)
                        action_mikrotik.update_ip(
                            client.login, client.ip_address,
                            form.cleaned_data['ip_address'])
                except:
                    form._errors.setdefault("login", 'Сервер не доступен')
                    return self.form_invalid(form)

        if client.login != form.cleaned_data['login']:
            if models.Clients.objects.filter(
                    login=form.cleaned_data['login']).count() > 0:
                form._errors.setdefault("login", 'Логин занят')
                return self.form_invalid(form)
            else:
                try:
                    for device in client.select_clients_group.select_server.all(
                    ):
                        action_mikrotik = ActionMikroTik(device)
                        action_mikrotik.update_login(
                            client.login, form.cleaned_data['login'])

                    #client.user.username = form.cleaned_data['login']
                    user = User.objects.get(id=client.user.id)
                    user.username = form.cleaned_data['login']
                    user.save()
                except:
                    form._errors.setdefault("login", 'Сервер не доступен')
                    return self.form_invalid(form)

        if form.cleaned_data['select_tarif'].id != client.select_tarif.id:
            try:
                for device in client.select_clients_group.select_server.all():
                    action_mikrotik = ActionMikroTik(device)
                    action_mikrotik.update_tariff(
                        client.login, form.cleaned_data['select_tarif'])
            except:
                form._errors.setdefault("login", 'Сервер не доступен')
                return self.form_invalid(form)

        self.success_url = '/pythonix_admin/client_info/' + self.kwargs[
            "pk"] + '/'
        return super(ClientInfoView, self).form_valid(form)
Ejemplo n.º 9
0
def deferred_actions_with_customers():
    ('off', 'Отключить'),
    ('on', 'Включить'),
    ('del', 'Удалить'),
    ('create', 'Создать'),
    ('del', 'Удалить'),
    for action in models.DeferredActionsWithClient.objects.filter(
            status=False, scheduled_implementation_date__isnull=True):
        print(action.id, action.scheduled_implementation_date)

        for device in action.client.select_clients_group.select_server.all():

            if action.action == 'create':
                try:
                    action_mikrotik = ActionMikroTik(device)
                    action_mikrotik.create_user(action.client.login,
                                                action.client.key,
                                                action.client.ip_address,
                                                action.client.select_tarif)
                    action.status = True
                    action.save()

                    emails = [
                        admin.admin.user.email
                        for admin in models.SendEmailAdmin.objects.filter(
                            event='deferred_actions')
                    ]
                    #msg = EmailMessage('deferred_actions',
                    #                   deferred_actions_text_messages(action.action, action.client.login, 'work'),
                    #                   settings.DEFAULT_FROM_EMAIL, emails)
                    #msg.content_subtype = "html"
                    #msg.send()

                except:
                    emails = [
                        admin.admin.user.email
                        for admin in models.SendEmailAdmin.objects.filter(
                            event='deferred_actions')
                    ]
                    #msg = EmailMessage('deferred_actions', deferred_actions_text_messages(action.action, action.client.login, 'error'),
                    #                   settings.DEFAULT_FROM_EMAIL, emails)
                    #msg.content_subtype = "html"
                    #msg.send()

            if action.action == 'on':
                try:
                    action_mikrotik = ActionMikroTik(device)
                    action_mikrotik.on_client(action.client.ip_address)
                    action.status = True
                    action.save()

                    emails = [
                        admin.admin.user.email
                        for admin in models.SendEmailAdmin.objects.filter(
                            event='deferred_actions')
                    ]
                    #msg = EmailMessage('deferred_actions',
                    #                   deferred_actions_text_messages(action.action, action.client.login, 'work'),
                    #                   settings.DEFAULT_FROM_EMAIL, emails)
                    #msg.content_subtype = "html"
                    #msg.send()
                except:
                    emails = [
                        admin.admin.user.email
                        for admin in models.SendEmailAdmin.objects.filter(
                            event='deferred_actions')
                    ]
                    #msg = EmailMessage('deferred_actions',
                    #                   deferred_actions_text_messages(action.action, action.client.login, 'error'),
                    #                   settings.DEFAULT_FROM_EMAIL, emails)
                    #msg.content_subtype = "html"
                    #msg.send()

            if action.action == 'off':
                try:
                    action_mikrotik = ActionMikroTik(device)
                    action_mikrotik.off_client(action.client.ip_address)
                    action.status = True
                    action.save()

                    emails = [
                        admin.admin.user.email
                        for admin in models.SendEmailAdmin.objects.filter(
                            event='deferred_actions')
                    ]
                    #msg = EmailMessage('deferred_actions',
                    #                   deferred_actions_text_messages(action.action, action.client.login, 'worm'),
                    #                   settings.DEFAULT_FROM_EMAIL, emails)
                    #msg.content_subtype = "html"
                    #msg.send()
                except:
                    emails = [
                        admin.admin.user.email
                        for admin in models.SendEmailAdmin.objects.filter(
                            event='deferred_actions')
                    ]
                    #msg = EmailMessage('deferred_actions',
                    #                   deferred_actions_text_messages(action.action, action.client.login, 'error'),
                    #                   settings.DEFAULT_FROM_EMAIL, emails)
                    #msg.content_subtype = "html"
                    #msg.send()

            if action.action == 'del':
                try:
                    action_mikrotik = ActionMikroTik(device)
                    action_mikrotik.delete_user(action.client.login,
                                                action.client.ip_address)
                    action.status = True
                    action.save()

                    emails = [
                        admin.admin.user.email
                        for admin in models.SendEmailAdmin.objects.filter(
                            event='deferred_actions')
                    ]
                    #msg = EmailMessage('deferred_actions',
                    #                   deferred_actions_text_messages(action.action, action.client.login, 'work'),
                    #                   settings.DEFAULT_FROM_EMAIL, emails)
                    #msg.content_subtype = "html"
                    #msg.send()
                except:
                    emails = [
                        admin.admin.user.email
                        for admin in models.SendEmailAdmin.objects.filter(
                            event='deferred_actions')
                    ]