Example #1
0
def add_user_to_tower(user_id, tower):
    if User.objects.filter(fk_ibs_user_info_user=user_id).exists():
        ibm = IBSManager()
        ibu = IBSUserManager(ibm)
        ibu.change_user_custom_field(
            User.objects.get(pk=user_id).fk_ibs_user_info_user.get().ibs_uid,
            'building', tower.ibs_name)
Example #2
0
def configure_static_ip_address(user_id, period=1):
    try:
        ibm = IBSManager()
        ibu = IBSUserManager(ibm)
        user = User.objects.get(pk=user_id)
        free_pool = IPPool.objects.filter(
            Q(fk_user_ip_static_ip=None)
            | Q(fk_user_ip_static_ip__isnull=False,
                fk_user_ip_static_ip__is_deleted=True)).first()
        if UserIPStatic.objects.filter(
                user=user_id,
                release_date__gte=datetime.today().date(),
                is_reserved=True).exclude(release_date__isnull=False).exists():
            uip = UserIPStatic.objects.get(user=user_id, is_reserved=True)
        elif UserIPStatic.objects.filter(
                user=user_id).exists():  # else if this user has any ip ...
            uip = UserIPStatic.objects.get(user=user_id)
            if uip.ip_id is None:  # if user ip is null then assign
                uip.ip = free_pool
        else:  # else user has never purchased one
            uip = UserIPStatic()
            uip.user = user
            uip.ip = free_pool
            uip.service_period = period
        if ibu.assign_ip_static(user.fk_ibs_user_info_user.get().ibs_uid,
                                uip.ip.ip):
            exp_date = uip.expire_date
            if exp_date:
                # Fix Timezone problem while comparing the time
                if exp_date.tzinfo:
                    if exp_date >= datetime.today().utcnow().replace(
                            tzinfo=utc):
                        new_exp_date = exp_date + timedelta(days=period * 30)
                    else:
                        new_exp_date = datetime.today() + timedelta(
                            days=period * 30)
                else:
                    if exp_date >= datetime.today():
                        new_exp_date = exp_date + timedelta(days=period * 30)
                    else:
                        new_exp_date = datetime.today() + timedelta(
                            days=period * 30)
            elif uip.is_free:
                new_exp_date = None
            else:
                new_exp_date = datetime.today() + timedelta(days=period * 30)
            uip.expire_date = new_exp_date
            uip.is_reserved = False
            uip.release_date = None
            uip.start_date = datetime.today()
            uip.save()
            return True, 67
        # send_to_dashboard(5052, invoice.user_id, 1)
        return False, 68
    except Exception as e:
        # send_to_dashboard(5052, invoice.user_id, 1)
        print '[UNEXPECTED] Error on assigning static ip for invoice %s : %s' % (
            user_id, e.message)
        return False, 68
Example #3
0
def de_configure_user_static_ip(user_id):
    ibm = IBSManager()
    ibu = IBSUserManager(ibm)
    user_ip = UserIPStatic.objects.get(user=user_id)
    if ibu.de_configure_user_ip(
            user_ip.user.fk_ibs_user_info_user.get().ibs_uid):
        user_ip.is_reserved = True
        user_ip.release_date = datetime.today() + timedelta(days=30)
        # user_ip.service_period = 0
        user_ip.save()
        return True
    return False
Example #4
0
 def __assign_user_towers(self):
     users = UserTower.objects.all()
     self.stdout.write('%s Users to check...' % users.count())
     for u in users:
         try:
             self.stdout.write('Setting %s' % u.pk)
             ibs_id = u.user.fk_ibs_user_info_user.get().ibs_uid
             tower = u.tower.name
             ibs = IBSManager()
             ibu = IBSUserManager(ibs)
             ibu.change_user_custom_field(ibs_id, 'building', tower)
         except Exception:
             self.stderr.write('Error on setting %s' % u.user.pk)
Example #5
0
 def __export_ip_address(self):
     ips = UserIPStatic.objects.filter(is_deleted=False,
                                       is_reserved=False,
                                       ip__isnull=False)
     for i in ips:
         ibs = IBSManager()
         ibm = IBSUserManager(ibs)
         ibs_info = IBSUserInfo.objects.filter(user=i.user_id).first()
         if not ibs_info:
             continue
         assert isinstance(ibs_info, IBSUserInfo)
         print('%s has %s' %
               (ibm.get_user_ip_static(ibs_info.ibs_uid), ibs_info.ibs_uid))
Example #6
0
def assign_user_to_tower(request):
    ia = check_ajax(request)
    if request.method == 'GET':
        tower_id = request.GET.get('t')
        user_id = request.GET.get('u')
        if not validate_integer(tower_id):
            if ia:
                return HttpResponseBadRequest(_('invalid tower id'))
            return render(request, 'errors/CustomError.html',
                          {'error_message': _('invalid tower id')})
        if not validate_integer(user_id):
            if ia:
                return HttpResponseBadRequest(_('invalid user id'))
            return render(request, 'errors/CustomError.html',
                          {'error_message': _('invalid user id')})
        if not User.objects.filter(pk=user_id).exists():
            if ia:
                return HttpResponseBadRequest(_('no such user found'))
            return render(request, 'errors/CustomError.html',
                          {'error_message': _('no such user found')})
        if not Tower.objects.filter(pk=tower_id).exists():
            if ia:
                return HttpResponseBadRequest(_('no such tower'))
            return render(request, 'errors/CustomError.html',
                          {'error_message': _('no such tower')})
        if UserTower.objects.filter(user=user_id).exists():
            ut = UserTower.objects.get(user=user_id)
        else:
            ut = UserTower()
            ut.user_id = int(user_id)
        ut.tower_id = int(tower_id)
        ut.save()
        # print ut.tower.ibs_name
        if User.objects.filter(fk_ibs_user_info_user=user_id).exists():
            ibm = IBSManager()
            ibu = IBSUserManager(ibm)
            ibu.change_user_custom_field(
                User.objects.get(
                    pk=user_id).fk_ibs_user_info_user.get().ibs_uid,
                'building', ut.tower.ibs_name)
        if ia:
            return HttpResponse('200')
        return redirect(reverse(view_towers))
    if ia:
        return HttpResponseBadRequest(_('invalid method'))
    return redirect(reverse(view_towers))
Example #7
0
def import_ip_statics_job():
    cache.set("IBS_IMPORTING", 1)
    try:
        ibm = IBSManager()
        ibu = IBSUserManager(ibm)
        ibs_users = IBSUserInfo.objects.all()
        for u in ibs_users:
            ip = ibu.get_user_ip_static(u.ibs_uid)
            if validate_ip(ip):
                if IPPool.objects.filter(ip=ip).exists():
                    pool = IPPool.objects.get(ip=ip)
                else:
                    pool = IPPool()
                    pool.ip = ip
                    pool.save()
                    print '[IP-STATIC] ADDRESS ADDED : %s' % ip
                if not UserIPStatic.objects.filter(user=u.user_id).exists():
                    user_ip = UserIPStatic()
                    user_ip.release_date = datetime.today() + timedelta(days=30)
                    user_ip.expire_date = datetime.today() + timedelta(days=15)
                    user_ip.start_date = datetime.today()
                    user_ip.service_period = 1
                else:
                    user_ip = UserIPStatic.objects.get(user=u.user_id)
                user_ip.ip = pool
                user_ip.user = User.objects.get(pk=u.user_id)
                user_ip.is_reserved = False
                user_ip.request_time = datetime.today()
                user_ip.save()
                # if StaticIPRequest.objects.filter(user=u.user_id).exists():
                #     rq = StaticIPRequest.objects.get(user=u.user_id)
                # else:
                #     rq = StaticIPRequest()
                #     rq.end_date = datetime.today() + timedelta(days=15)
                #     rq.request_time = datetime.today()
                #     rq.service_period = 1
                #     rq.start_date = datetime.today()
                # rq.user = User.objects.get(pk=u.user_id)
                # rq.address = ip
                # rq.save()
                print '[IP-STATIC] IP ASSIGNED TO USER %s %s' % (ip, u.user.username)
        if exists('/var/CRM/Docs/None/IP.xlsx'):
            wb = load_workbook('/var/CRM/Docs/None/IP.xlsx')
            act = wb.get_active_sheet()
            for i in range(2, act.max_row):
                row_id1 = 'A%s' % i
                row_id2 = 'B%s' % i
                uid = act[row_id1].value
                if not validate_integer(uid):
                    break
                exp = act[row_id2].value
                date0 = exp.split('.')
                date1 = '13%s-%s-%s' % (date0[2], date0[1], date0[0])
                # print(date1)
                cor_date = parse_date_from_str(date1)
                # print cor_date
                # cor_date.hour = 12
                # cor_date.minute = 0
                if UserIPStatic.objects.filter(user__fk_ibs_user_info_user__ibs_uid=int(uid)).exists():
                    user_ip_data = UserIPStatic.objects.get(user__fk_ibs_user_info_user__ibs_uid=int(uid))
                    user_ip_data.expire_date = cor_date
                    user_ip_data.save()
                    # StaticIPRequest.end_date
                    # req = StaticIPRequest.objects.filter(user__fk_ibs_user_info_user__ibs_uid=int(uid)).last()
                    # req.end_date = cor_date
                    # req.save()
    except Exception as e:
        print e.message
    finally:
        cache.set("IBS_IMPORTING", 0)