def temp_charge(uid, add_days, credit, invoice_id=None): """ Attempt to charge user credit or time @param invoice_id: User Invoice ID @param credit: Credit amount to update @param add_days: Add Days to Expire date. Auto Calculated! @param uid: CRM User ID @return: True if done and False if Error """ from CRM.Core.ServiceManager import UserServiceStatus state = UserServiceStatus(uid) ibm = state.ibs_manager if state.is_limited: user_credit = state.credit if user_credit < 10: low_credit = True else: low_credit = False else: low_credit = True try: res0 = ibm.set_expire_date_by_uid(state.ibs_id, add_days) if state.is_unlimited: res1 = ibm.change_credit_by_uid(1, state.ibs_id, True) and ibm.change_credit_by_uid( 2, state.ibs_id, True) else: res1 = ibm.change_credit_by_uid(credit, state.ibs_id, low_credit) if invoice_id: update_charge_state(invoice_id, res0, _('add expire date')) update_charge_state(invoice_id, res1, _('adding credit')) return True, (res0, res1) except Exception as e: logger.error(e.message or e.args) return False, 2
def can_use_temp_charge(uid): temp_dat = TempChargeManagement.get_max_charges(uid) state = UserServiceStatus(uid) if not state.current_service: return False res = (temp_dat[0] > 0 or temp_dat[1] > 0) and state.active_service if not state.ibs_id: return False cr = state.credit is_expire = state.account_expired if ((cr < 100 and not state.is_unlimited) or is_expire) and res: return True elif state.is_unlimited and res and is_expire: return True return False
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 __create_invoice__(self): i = Invoice() service_invoice = InvoiceService() service_invoice.service_type = self.service_type service_discount = None if self.discount: service_discount = InvoiceDiscount() service_discount.content_object = self.discount service_discount.save() if self.service_type == 2: service_invoice.content_object = self.service i.service_text = self.service.name if self.discount: i.service_discount = service_discount i.extra_data = self.discount.extended_package elif self.service_type == 1: i.service_text = self.service.name + ' ' + self.get_extra_data( ).get('service_property').name service_invoice.content_object = self.get_extra_data().get( 'service_property') i.extra_data = self.kwargs.get('extra_month') elif self.service_type == 3: uip = UserIPStaticHistory() uip.user_id = self.kwargs.get('uid') uip.save() service_invoice.content_object = uip elif self.service_type == 4: i.service_text = self.service.name service_invoice.content_object = self.service i.create_time = now() i.price = self.final_price i.comment = '' i.user = User.objects.get(pk=self.kwargs.get('uid')) user_service_stat = UserServiceStatus(self.kwargs.get('uid')) if user_service_stat.current_service: if i.extra_data is not None: i.extra_data = int(i.extra_data) service_invoice.expire_date = date_add_days_aware( user_service_stat.current_service.expire_date, i.extra_data * 30) service_invoice.save() i.service = service_invoice i.service_discount = service_discount self.invoice = i
def __validate(self, user_id): credit = self.get_int('c') day = self.get_int('d') # ibs = IBSManager() state = UserServiceStatus(user_id) # ibs_user = IBSUserInfo.objects.filter(user=user_id).first() if not state.ibs_id: self.error(_('invalid user'), True) # current_credit = ibs.get_user_credit_by_user_id(ibs_user.ibs_uid) if state.credit < 100 and state.is_limited: if not credit: self.error(_('you are on low credit. you must select credit'), True) # cs = UserCurrentService.objects.filter(user=user_id).first() if state.account_expired: if not day: self.error( _('your account expired. you must select service charge'), True)
def unlock_account(self): user = self.get_target_user(True) locked = LockedUsers.objects.filter(user=user.pk).first() if locked: if locked.ibs_locked: ibs = IBSManager() ibi = IBSUserInfo.objects.get(user=user.pk).ibs_uid dt = datetime.today().date() - locked.lock_date.date() x = UserServiceStatus(user.pk) a = datetime.today().date( ) - x.current_service.expire_date.date() to_add = a.days if to_add < 0: to_add = a.days * (-1) if dt.days > 20: ibs.set_expire_date_by_uid(ibi, to_add, True) # fire_event(4237, user, None, self.requester.pk) ibs.unlock_user(ibi) locked.delete() user.is_active = True user.save() UnlockAccountEventHandler().fire(user, None, self.requester, True) return True
def buy_float_service(request): """ Buy Float Service for user @rtype: HttpResponse @param request: HttpRequest """ try: user = UserTemplateManager(request).get_target_user() auto_post = False can_build_new = False if not user.pk: user_pk = 0 is_test = True else: user_pk = user.pk is_test = False if request.user.is_staff: rx = '/factor/show/all/' else: rx = '/factor/pay/' recharge_mode = False status = UserServiceStatus(user_pk) if is_test: groups = CustomOptionGroup.objects.filter(is_deleted=False).order_by('view_order', '-pk') else: if request.user.is_authenticated() and not hasattr(request, 'ip_login'): if request.GET.get('nv!', None): can_build_new = True if status.can_recharge_float and not can_build_new: recharge_mode = True groups = status.active_float_recharge_groups # get_user_active_groups(user.pk) auto_post = True else: groups = CustomOptionGroup.objects.filter(is_deleted=False).order_by('view_order', '-pk') is_new_template = request.GET.get('nv!', None) is not None and not hasattr(request, 'ip_login') is_new_template = is_new_template and request.user.is_authenticated() if recharge_mode: service = status.active_float_service # get_user_ibs_service(user.pk) else: service = BasicService.objects.all() tower = user.fk_user_tower_user.first() if tower: max_bw = tower.tower.max_bw if max_bw > 0: service = service.filter(max_bw__lte=max_bw) elif not request.user.is_staff: service = service.filter(max_bw__lte=read_config('service_home_limit', 2048)) # else: # service = service_pk = None options = [] template_id = None edit_mode = False custom_options = {} # if not recharge_mode: tm = UserTemplateManager(request) tm.pk_name = 't' x = tm.get_single_ext(False) if x: service_pk = x.service.ext options = x.fk_float_template_template.all().values_list('option_id', flat=True) c_options = x.fk_float_template_template.filter(option__is_custom_value=True).values('option_id', 'value') for a in c_options: custom_options.update({a.get('option_id'): a.get('value')}) auto_post = True template_id = x.ext edit_mode = True just_confirm = (recharge_mode and (template_id is not None)) or \ ((template_id is not None) and not status.active_service) if just_confirm: service = BasicService.objects.filter(pk=x.service_id) return render(request, 'service/BuyFloatService.html', {'groups': groups, 'u': user_pk, 'redirect_address': rx, 'template_service': service_pk, 'template_options': options, 'recharge': recharge_mode, 'auto_post': auto_post, 'edit_mode': edit_mode, 'template_id': template_id, 'is_new_template': is_new_template, 'just_confirm': just_confirm, 'template_custom_options': custom_options, 'services': service.order_by('service_index')}) except Exception as e: logger.error(e.message or e.args) return send_error(request, _('user can not access this service'))