def service_kill_current_user(request): try: sm = BasicServiceManager(request) x = sm.get_target_user(False) if x.is_anonymous(): return send_error(request, _('unable to complete disconnect')) Utils.kill_user_by_request(x.pk, request) return HttpResponse('200') except Exception as e: logger.error(e.message or e.args) return send_error(request, _('system error'))
def service_float_get_client_template(request): try: bm = BasicServiceManager(request) res = Utils.get_client_float_templates(bm.get_target_user().pk).values('pk', 'ext', 'service__name', 'service_period', 'final_price', 'name') return HttpResponse(json.dumps(list(res))) except Exception as e: logger.error(e.message or e.args) return send_error(request, _('system error'))
def gen_invoice(uid, credit, days, lock_mode=False): """ Generate Invoice @param lock_mode: bool @param uid: user id @type uid: str|int @param credit: credit used to charge @type credit: int @param days: charged days @type days: int @return: Invoice id @rtype: int """ current_service = UserCurrentService.objects.get(user=uid) prices = Utils.get_service_price(current_service) package_price = prices.meg service_price = days * prices.day ti = TempInvoice() ti.credit = credit ti.credit_price = credit * package_price ti.days = days ti.days_price = service_price ti.user_id = uid ti.save() i = Invoice() ic = InvoiceService() ic.content_object = ti ic.service_type = 6 exp_date = date_add_days_aware(current_service.expire_date, days) ic.expire_date = exp_date ic.save() i.user_id = uid i.comment = _('temp charge') i.create_time = now() if lock_mode: user_debit = UserDebit.objects.filter(user=uid).first() if user_debit: i.debit_price = user_debit.amount else: i.debit_price = 0 else: i.debit_price = 0 i.dynamic_discount = 0 i.extra_data = 0 # i.price = (ti.days_price + ti.credit_price) i.tax = (ti.days_price + ti.credit_price) * float( read_config('invoice_tax', 0.09)) i.price = (ti.days_price + ti.credit_price) + i.tax i.service_text = _('temp charge invoice') i.service = ic i.save() return i.pk
def get_max_charges(uid): """ Get the max allowed changes for user @param uid: user id @type uid: str @return: (credit, days) @rtype: (int, int) """ state = UserServiceStatus(uid) data = TempChargeState.objects.filter( user=uid).first() # only 1 Hit to DB! cs = state.current_service is_float_mode = read_config('service_temp_float', '0') == '1' if cs: x = Utils.get_service_price(cs) if data: credit = data.credit if state.is_unlimited: credit = 0 days = data.days if not is_float_mode: temp_days = int(read_config('service_temp_time', 2)) temp_amount = int(read_config('service_temp_amount', 1024)) if days > temp_days: days = temp_days # TempChargeManagement.update_state(uid, 0, days, True, True) if credit > temp_amount: # TempChargeManagement.update_state(uid, credit, 0, True, True) credit = temp_amount else: if state.is_limited: credit = int(read_config('service_temp_amount', 1024)) else: credit = 0 days = int(read_config('service_temp_time', 2)) TempChargeManagement.update_state(uid, credit, days, True, True) if not state.account_expired: days = 0 if state.credit > 100: credit = 0 if data.total_count > 3: return 0, 0, 0, 0 return credit, days, x.day, x.package else: return 0, 0, 0, 0
def kill_user_by_request(user_id, request): from CRM.Core.ServiceManager import Utils Utils.kill_user_by_request(user_id, request)
def update(self, **kwargs): service = kwargs.get('service') current_service = kwargs.get('current_service') extra_data = kwargs.get('extra') if not isinstance(service, UserFloatTemplate): logger.error('UserFloatTemplate Type Expected Got %s' % type(service)) return self._error(1400, _('invalid float template type'), True) if not isinstance(current_service, UserCurrentService): logger.error('UserCurrentService Type Expected Got %s' % type(service)) return self._error(1401, _('invalid user current service type'), True) if not isinstance(extra_data, int) and not isinstance( extra_data, long): logger.error('int Type Expected got charge Got %s' % type(int)) return self._error(1402, _('invalid charge month type'), True) options = service.fk_float_template_template.all() if UserServiceState.objects.filter( option__var_name='transfer', user=current_service.user_id).first(): has_transfer = True else: has_transfer = False user_package = 0 ibs_service = 0 is_unlimited = 1 ip_pool = None ip_static = 0 from CRM.Core.ServiceManager import Utils from CRM.Core.TempChargeManagement import TempChargeManagement # Prevent Dependency Loop is_new_service = Utils.is_new_service(current_service.user_id, service) if extra_data > 0 and is_new_service: UserServiceState.objects.filter( user=current_service.user_id).delete() for o in options: option = o.option user_service_state = UserServiceState.objects.filter( option=option.pk, user=current_service.user_id).first() if not user_service_state: user_service_state = UserServiceState() user_service_state.option_id = option.pk user_service_state.value = o.value user_service_state.user_id = current_service.user_id user_service_state.current_value = o.value user_service_state.purchase_date = now() try: user_service_state.save() except Exception as e: logger.error(e.message or e.args) if option.var_name == 'package' or option.package > 0: user_package += o.value elif option.var_name == 'unlimited' or option.group_type == 2: is_unlimited = 2 elif option.pool_id is not None: ip_pool = option.pool elif option.fk_custom_option_group_map_option.filter( service=service.service.pk).exists(): ibs_service = option.fk_custom_option_group_map_option.filter( service=service.service.pk).all() elif option.var_name == 'ip': ip_static = True if not ibs_service: ibs_service = service.service.fk_basic_service_default_group_service.filter( group_type=is_unlimited).first().group else: ibs_service = ibs_service.filter( group__group_type=is_unlimited).first().group temp_package = 0 temp_days = 0 rate = TempChargeManagement.calculate_temp_rate( current_service.user_id) if user_package > 0: if extra_data == 0: tmp_can_transfer = True elif has_transfer and not is_new_service: tmp_can_transfer = True else: tmp_can_transfer = False fpd = FloatPackageDiscount.objects.filter( charge_amount=user_package).first() to_charge = user_package * 1024 * service.service_period if fpd is not None: to_charge += (fpd.extra_charge * (user_package * 1024)) / 100 pdu = FloatPackageDiscountUsage() pdu.user_id = current_service.user_id pdu.package_discount_id = fpd.pk pdu.save() if not self._update_package(int(to_charge), not tmp_can_transfer): logger.error('unable to charge service') return self._error(1403, _('unable to charge service'), True) temp_package = (int(user_package * 1024) * rate) / 100 if extra_data > 0: if ibs_service: charge_days = service.service_period * 30 if current_service.is_active: ds = FloatDiscount.objects.filter( charge_month=service.service_period).first() if ds: extra_days = ds.extra_charge charge_days += extra_days extra_package = (ds.extra_package * (int(user_package) * 1024)) / 100 if not self._update_package(int(extra_package)): logger.error( 'unable to add package discount for user %s ' % self.user_id) else: charge_days = int(read_config('service_test_time', 2)) user_tower = UserTower.objects.filter( user=current_service.user_id).first() if user_tower: if user_tower.tower.has_test: charge_days = int( read_config('service_tower_test', 30)) current_service.is_active = True if not self._update_user_group(ibs_service.ibs_name): logger.error('unable to change user group : %s' % self.user_id) return self._error(1410, _('unable to change user group'), True) if not self._update_expire_date(charge_days, is_new_service): logger.error('unable to change expire date : %s' % self.user_id) return self._error(1411, _('unable to change expire date'), True) temp_days = (int((service.service_period * 30)) * rate) / 100 ua = UserActiveTemplate.objects.filter( user=current_service.user_id).first() if not ua: ua = UserActiveTemplate() ua.user_id = current_service.user_id ua.template_id = service.pk ua.save() current_service.is_float = True current_service.service = ibs_service current_service.service_property = None current_service.save() # self._KILL_REQUEST = kwargs.get('request') # self._kill_user(current_service.user_id) if temp_package > 0 or temp_days > 0 and current_service.is_active: # Prevent Extra DB Hit TempChargeManagement.update_state( current_service.user_id, temp_package, temp_days, temp_days > 0 and temp_package > 0, True) else: logger.warning('Temp Update Was Skipped for %s' % self.user_id) if ip_static: configure_static_ip_address(current_service.user_id, service.service_period) if ip_pool: x0 = assign_ip_pool(user_id=current_service.user_id, pool_name=ip_pool.ibs_name) return self._error(0, _('operation done'))
def update(self, **kwargs): charge_month = kwargs.get('period', 1) # in Month charge_amount = kwargs.get('amount', 0) # in MB current_service = kwargs.get('current_service') service = kwargs.get('service', None) package = kwargs.get('package', None) if not isinstance(charge_amount, int) and not isinstance( charge_amount, long): logger.error('invalid charge amount while updating package') return self._error( 30301, _('invalid charge amount while updating package'), True) if not isinstance(charge_month, int) and not isinstance( charge_month, long): logger.error('invalid charge time while updating package') return self._error(30302, _('invalid charge time while updating package'), True) if not isinstance(current_service, UserCurrentService): logger.error('invalid user service data while updating package') return self._error( 30303, _('invalid user service data while updating package'), True) # if not isinstance(service, ServiceProperty): if self.service_type == 1: if not isinstance(service, ServiceProperty): logger.error( 'invalid ibs service property while updating package') return self._error( 30304, _('invalid ibs service property while updating package'), True) elif self.service_type == 2: if not isinstance(package, Traffic): logger.error('invalid charge amount while updating package') return self._error( 30304, _('invalid charge amount while updating package'), True) try: if charge_month < 1: charge_month = 1 reduce_one = False # is used to charge for services with more than 1 month purchases set_to_active = False if self.service_type == 1 and not charge_amount: logger.error('no amount to charge while updating package') return self._error( 30310, _('no amount to charge while updating package'), True) else: if not charge_amount: charge_amount = package.amount if not current_service.is_active: # Service is a test service! if charge_month > 1: reduce_one = True set_to_active = True if self.service_type == 1: from CRM.Core.ServiceManager import Utils extra_charge = Utils.get_next_charge_transfer( current_service.user_id) replace = True if service.pk == current_service.service_property_id: new_service = False else: new_service = True if charge_amount > 1: if reduce_one: amount = charge_amount * (charge_amount - 1) else: amount = charge_amount * charge_month if IBSServiceDiscount.objects.filter( service=service.pk, charge_days=charge_month).exists(): amount += IBSServiceDiscount.objects.get( service=service.pk, charge_days=charge_month).extra_traffic else: amount = 0 else: extra_charge = 0 replace = False new_service = False amount = charge_amount if PackageDiscount.objects.filter(package=package.pk, is_deleted=False).exists(): amount += PackageDiscount.objects.get( package=package.pk, is_deleted=False).extended_package if amount < 1: amount = 1 replace = True # While user in temp charge, we must set it to clear temp charge state add_credit = amount if set_to_active: replace = True if not new_service and extra_charge > 0 and not set_to_active: add_credit += extra_charge self._update_package(int(add_credit), replace) return self._error(0, _('package updated successfully'), False) except Exception as e: logger.error(e.message or e.args) return self._error(500, _('system error'), True)