def search(self): req = self.store sp = get_string(req.get('searchPhrase')) user_id = get_integer(req.get('u')) send_date = parse_date_from_str(req.get('sds')) send_type = get_integer(req.get('stp')) state = get_integer(req.get('st')) invoice_type = get_integer(req.get('t')) invoice_number = get_integer(req.get('in')) ibs_id = get_integer(req.get('i')) res = DedicatedInvoice.objects.all() if sp: res = res.filter(user__first_name__icontains=sp) if user_id: res = res.filter(user=user_id) if ibs_id: res = res.filter(user__fk_ibs_user_info_user__ibs_uid=ibs_id) if invoice_number: res = res.filter(system_invoice_number=invoice_number) if invoice_type: res = res.filter(invoice_type_id=invoice_type) if send_type: res = res.filter(send_type_id=send_type) if send_date: res = res.filter(send_date=send_date.date()) if state: res = res.filter(fk_dedicated_invoice_state_invoice__state=state) return res
def validate_input_data_type(data, value): # if not isinstance(data, unicode): # print type(data) # return None # if not isinstance(value, unicode): # print type(value) # return None data_type, key = data.split('__', 1) # caller = None res = None if data_type == 'int': if validate_integer(value): res = int(value) elif data_type == 'str': if validate_empty_str(value): res = value elif data_type == 'bool': x, y = validate_boolean(value) if x: res = y elif data_type == 'date': from CRM.Tools.DateParser import parse_date_from_str res = parse_date_from_str(value) if res is not None: return {key: res} return None
def search_telegram_users(get): if not isinstance(get, dict): return TelegramUser.objects.none() users = TelegramUser.objects.filter(is_deleted=False) ibs_id = get.get('ib') join_date_start = parse_date_from_str(get.get('sd')) join_date_end = parse_date_from_str(get.get('ed')) username = get.get('tu') if validate_integer(ibs_id): users = users.filter(user__fk_ibs_user_info_user__ibs_uid=ibs_id) if validate_empty_str(username): users = users.filter(username__iexact=username) if join_date_start: users = users.filter(register_date__gt=join_date_start.date()) if join_date_end: users = users.filter(register_date__lt=join_date_end.date()) return users
def add_new_poll(request): if request.method == 'POST': name = request.POST.get('n') description = request.POST.get('d') start_date = parse_date_from_str(request.POST.get('sd')) end_date = parse_date_from_str(request.POST.get('ed')) target_address = request.POST.get('tg') extra_package = request.POST.get('ep') extra_days = request.POST.get('es') edit = request.POST.get('e') if not validate_empty_str(name): return render(request, 'errors/CustomError.html', {'error_message': _('please enter name')}) if not validate_empty_str(description): return render(request, 'errors/CustomError.html', {'error_message': _('please enter description')}) if not validate_empty_str(target_address): return render(request, 'errors/CustomError.html', {'error_message': _('please enter target address')}) if not validate_integer(extra_days): return render(request, 'errors/CustomError.html', {'error_message': _('please enter extra days for service')}) if not validate_integer(extra_package): return render(request, 'errors/CustomError.html', {'error_message': _('please enter extra package amount')}) if request.user.has_perm('CRM.change_polls'): if validate_integer(edit): if Polls.objects.filter(pk=edit).exists(): p = Polls.objects.get(pk=edit) else: p = Polls() else: p = Polls() else: p = Polls() p.name = name p.description = description p.end_date = end_date p.start_date = start_date p.extra_days = int(extra_days) p.extra_package = int(extra_package) p.target_address = target_address p.save() return redirect(reverse(view_polls)) else: return redirect(reverse(view_polls))
def temp_charge_report(request): if request.method == 'GET': user = request.user granted = False if user.is_superuser or user.is_staff: granted = True sd = request.GET.get('sd') ed = request.GET.get('ed') if granted: ibs_id = request.GET.get('ib') user_id = request.GET.get('ui') rch = request.GET.get('rc') charges = FreeTrafficLog.objects.all() else: ibs_id = 'invalid' user_id = 'invalid' rch = 'invalid' charges = FreeTrafficLog.objects.filter(user=user.pk) if request.GET.get('a'): if validate_integer(ibs_id): charges = charges.filter( user__fk_ibs_user_info_user__ibs_uid=ibs_id) if validate_integer(user_id): charges = charges.filter(user=user_id) if validate_integer(rch): charges = charges.filter(recharger=rch) if validate_empty_str(sd): start_date = parse_date_from_str(sd) charges = charges.filter(datetime__gt=start_date) if validate_empty_str(ed): end_date = parse_date_from_str(ed) charges = charges.filter(datetime__lt=end_date) charges = charges.order_by('-datetime') res = init_pager(charges, 30, request.GET.get('nx'), 'charges', request=request) return render(request, 'finance/TempChargeReport.html', res) else: return render(request, 'errors/AccessDenied.html')
def build_params_by_get(get): if not get: return {} res = {} for k in get.iterkeys(): if get[k] != '-1' and get[k] is not None and get[k] != '': opd = detect_operator(get[k], k) if opd == 'js' or opd == 'je' or opd == 'ee' or opd == 'es': res[opd] = parse_date_from_str(get[k]) else: res[opd] = get[k] return res
def __search_ip_static_request__(get, rqs): if not isinstance(get, dict): return UserIPStatic.objects.none() ibs_id = get.get('tu') start_date = parse_date_from_str(get.get('esd')) end_date = parse_date_from_str(get.get('eed')) month = get.get('mn') ip_address = get.get('ip') is_free = get.get('isPaid') if validate_empty_str(ip_address): rqs = rqs.filter(ip__ip__contains=ip_address) if validate_integer(ibs_id): rqs = rqs.filter(user__fk_ibs_user_info_user__ibs_uid=ibs_id) if isinstance(start_date, datetime): rqs = rqs.filter(expire_date__gt=start_date.date(), is_free=False) if isinstance(end_date, datetime): rqs = rqs.filter(expire_date__lt=end_date.date(), is_free=False) if validate_integer(month): rqs = rqs.filter(service_period=month) b_res = validate_boolean(is_free) if b_res[0]: rqs = rqs.filter(is_free=b_res[1]) return rqs
def get_date(self, name, throw=False, only_date=False): """ Parse Persian Input Date in to Julian Standard datetime to save in DB :param only_date: only get the date object if found :param name: name of the param to read :param throw: raise error if param not validated :return: datetime """ res = parse_date_from_str(self.store.get(name)) if not res and throw: logger.warning('item not entered for %s : datetime %s' % (self.target, name)) self.error(_('no date selected'), True, name) elif res and only_date: return res.date() return res
def update_state(self): store = self.store x = self.get_single_ext(True) if DedicatedInvoiceState.objects.filter(invoice=x.pk).exists(): state = DedicatedInvoiceState.objects.get(invoice=x.pk) if state.state == 7: return else: state = DedicatedInvoiceState() state.invoice = x state.next_change = parse_date_from_str(store.get('cd')) state.state = get_integer(store.get('st')) state.save() hs = DedicatedInvoiceStateHistory() hs.invoice = x hs.state = get_integer(store.get('st')) hs.user = self.requester hs.extra_data = get_string(store.get('dt')) or '-' hs.save()
def create_invoice(self): d = self.get_single_ext(True) store = self.store if d.system_invoice_number > 0: raise RequestProcessException(_('invoice generation done before')) bank_ref = get_string(store.get('rf')) comment = get_string(store.get('c')) pay_time = parse_date_from_str(store.get('pd')) if not bank_ref: raise RequestProcessException(_('please enter bank ref code')) if not comment: comment = '' if not pay_time: pay_time = datetime.today() i = Invoice() ins = InvoiceService() ins.content_object = d ins.service_type = 5 ins.save() i.comment = comment i.create_time = datetime.today() i.debit_price = 0 i.dynamic_discount = 0 i.extra_data = 0 i.is_paid = True i.paid_online = False i.pay_time = pay_time i.price = d.price i.ref_number = bank_ref i.service = ins i.service_text = _('dedicate service') i.user_id = d.user_id i.comment = self.requester.username i.save() d.system_invoice_number = i.pk d.save() self.__update_state__(d, 7) return i.pk
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)
def __search_dashboard__(get, data): if not isinstance(get, dict): return Dashboard.objects.none() # assert isinstance(data, Dashboard.objects) group = get.get('tg') target_user_ibs = get.get('tu') target_user_id = get.get('tiu') job_start = parse_date_from_str(get.get('ssd')) job_end = parse_date_from_str(get.get('dsd')) title = get.get('t') state = get.get('jst') sender = get.get('snd') responsible = get.get('rpp') send_date_start = parse_date_from_str(get.get('sds')) send_date_end = parse_date_from_str(get.get('sde')) done_date_start = parse_date_from_str(get.get('dds')) done_date_end = parse_date_from_str(get.get('dde')) ref_from = get.get('srcGroup') ref_to = get.get('dstGroup') calendar_date = get.get('cal') current_group = get.get('sCG') target_text = get.get('searchPhrase') if validate_empty_str(target_text): data = data.filter(target_text__icontains=target_text) if validate_integer(target_user_ibs): data = data.filter( target_user__fk_ibs_user_info_user__ibs_uid=target_user_ibs) if validate_integer(sender): data = data.filter(sender=sender) elif validate_integer(target_user_id): data = data.filter(target_user=target_user_id) if validate_empty_str(title): data = data.filter(title__icontains=title) if validate_integer(group): data = data.filter( Q(group=group) | Q(fk_dashboard_reference_dashboard__target_group=group)) if isinstance(job_start, datetime): data = data.filter(create_date__gt=job_start.date(), create_date__lt=job_start.date() + timedelta(days=1)) if isinstance(job_end, datetime): data = data.filter(done_date__gt=job_end.date(), done_date__lt=job_end.date() + timedelta(days=1)) if validate_integer(state): data = data.filter(last_state=state) if validate_integer(responsible): data = data.filter(reader=responsible) if send_date_start: data = data.filter(create_date__gt=send_date_start.date()) if send_date_end: data = data.filter(create_date__lt=send_date_end.date()) if done_date_start: data = data.filter(done_date__gt=done_date_start.date()) if done_date_end: data = data.filter(done_date__lt=done_date_end.date()) if validate_integer(ref_from): data = data.filter( fk_dashboard_reference_dashboard__source_group=ref_from) if validate_integer(ref_to): data = data.filter( fk_dashboard_reference_dashboard__target_group=ref_to) if validate_empty_str(calendar_date): cal_split = calendar_date.split('/') cal_day = cal_split[2] cal_month = cal_split[1] cal_year = cal_split[0] data = data.filter(fk_calendar_dashboard__cal_day=cal_day, fk_calendar_dashboard__cal_month=cal_month, fk_calendar_dashboard__cal_year=cal_year) if validate_integer(current_group): data = data.filter( fk_dashboard_current_group_dashboard__group=current_group) return data
def __add_edit_service_properties(request): if request.method == 'POST': name = request.POST.get('name') description = request.POST.get('description') price = request.POST.get('price') init_pack = request.POST.get('initPackage') pack_price = request.POST.get('packPrice') period = request.POST.get('period') bw = request.POST.get('bw') is_visible = request.POST.get('isVisible') # is_vip = request.POST.get('isVIP') data_pk = request.POST.get('pk') selected_vip_group = request.POST.get('vpg') srv_pk = request.POST.get('srv') start_date = request.POST.get('sd') end_date = request.POST.get('ed') if not validate_integer(srv_pk): return redirect(reverse(show_all_service)) if not validate_empty_str(name): return render(request, 'errors/CustomError.html', {'error_message': _('please enter a name')}) if not validate_empty_str(description): return render(request, 'errors/CustomError.html', {'error_message': _('please enter description')}) if not validate_integer(price): return render(request, 'errors/CustomError.html', {'error_message': _('please enter a valid price')}) if not validate_integer(init_pack): return render( request, 'errors/CustomError.html', {'error_message': _('please enter a valid initial package')}) if not validate_integer(pack_price): return render( request, 'errors/CustomError.html', {'error_message': _('please enter a valid package price')}) if not validate_integer(period): return render(request, 'errors/CustomError.html', {'error_message': _('please enter a valid period')}) if not validate_empty_str(bw): return render( request, 'errors/CustomError.html', {'error_message': _('please enter a valid bandwidth')}) # if not validate_integer(selected_vip_group): # return render(request, 'errors/CustomError.html', {'error_message': _('please select a valid vip group')}) try: if not IBSService.objects.filter(pk=srv_pk).exists(): return redirect(reverse(show_all_service)) if validate_integer(data_pk): if not ServiceProperty.objects.filter(pk=data_pk).exists(): return redirect(reverse(show_all_service)) data_pr = ServiceProperty.objects.get(pk=data_pk) else: data_pr = ServiceProperty() data_pr.bandwidth = bw data_pr.base_price = int(price) data_pr.description = description data_pr.initial_package = int(init_pack) # data_pr.is_vip_property = is_vip is not None data_pr.is_visible = is_visible is not None data_pr.name = name data_pr.package_price = int(pack_price) data_pr.period = int(period) start_date = parse_date_from_str(start_date) # print 'start date is %s of type %s ' % (start_date, type(start_date)) if start_date: data_pr.start_date = start_date end_date = parse_date_from_str(end_date) if end_date: data_pr.end_date = end_date data_pr.save() if validate_integer(data_pk): if IBSServiceProperties.objects.filter( properties=data_pk).exists(): srv_pr = IBSServiceProperties.objects.get( properties=data_pk) else: srv_pr = IBSServiceProperties() else: srv_pr = IBSServiceProperties() srv_pr.properties = data_pr srv_pr.service = IBSService.objects.get(pk=srv_pk) srv_pr.save() if validate_integer(selected_vip_group): if not VIPGroups.objects.filter(pk=selected_vip_group).exists( ): # @TODO check for data pollution return redirect( reverse(show_all_service_properties) + '?s=%s' % data_pk) if VIPServices.objects.filter(service=data_pr.pk).exists(): vps = VIPServices.objects.get(service=data_pr.pk) else: vps = VIPServices() vps.group = VIPGroups.objects.get(pk=selected_vip_group) vps.service = data_pr vps.save() else: if VIPServices.objects.filter(service=data_pr.pk).exists(): VIPServices.objects.get(service=data_pr.pk).delete() fire_event(2303, srv_pr, None, request.user.pk) return redirect( reverse(show_all_service_properties) + '?s=' + srv_pk) except Exception as e: print e.message return render(request, 'errors/ServerError.html') else: return render(request, 'errors/ServerError.html')
def view_user_connections(request): user = request.user granted = False if user.is_staff or user.is_superuser: granted = True if request.method == 'GET': if granted: uid = request.GET.get('u') else: uid = user.pk if not uid: return redirect(reverse(show_user_service_summery) + '?u=%s' % uid) nx = request.GET.get('nx') if not validate_integer(nx): nx = 0 nx = int(nx) try: last_day = datetime.today() - timedelta(days=1) last_week = datetime.today() - timedelta(days=7) last_month = datetime.today() - timedelta(days=30) ibi = IBSUserInfo.objects.get(user=uid).ibs_uid ibs = IBSManager() if request.GET.get('a') == 'b': tmp_st = parse_date_from_str(request.GET.get('sd')) tmp_ed = parse_date_from_str(request.GET.get('ed')) if tmp_st: st = request.GET.get('sd') else: st = None if tmp_ed: ed = request.GET.get('ed') else: ed = None else: st = convert_date_no_day(last_day) ed = None if st or ed: per_page = 30 d = ibs.get_connection_logs(ibi, nx, nx + per_page, st, ed) data = d['report'] # print d total_bytes_in = d['total_in_bytes'] total_bytes_out = d['total_out_bytes'] back_link = nx - per_page next_link = nx + per_page total_rec = int(d['total_rows']) page = next_link / per_page # for z in data: # total_bytes_in += int(z['bytes_in']) # total_bytes_out += int(z['bytes_out']) # total_rec /= 20 total_used = total_bytes_out + total_bytes_in else: data = None next_link = 0 back_link = -1 total_rec = 0 total_bytes_in = 0 total_bytes_out = 0 per_page = 30 page = 1 total_used = 0 # print data return render( request, 'report/UserConnection.html', { 'data': data, 'next_link': next_link, 'back_link': back_link, 'total': total_rec, 'req': request, 'u': uid, 'download': total_bytes_in, 'upload': total_bytes_out, 'per_page': per_page, 'page': page, 'total_usage': total_used, 'last_day': last_day, 'last_week': last_week, 'last_month': last_month, }) except Exception as e: print e.message return render(request, 'errors/ServerError.html') else: return render(request, 'errors/AccessDenied.html')