def traffic_api(request): ''' 接受服务端的用户流量上报 JSON_DATA: { 'node_id': , 'data': [ { 'user_id': , 'u': , 'd': }, ... ] } RES_DATA: { 'user_id': 'ok', ... } ''' data = request.json node_id = data['node_id'] traffic_rec_list = data['data'] # 定义循环池 node_total_traffic = 0 trafficlog_model_list = [] log_time = int(time.time()) res_data = dict() for rec in traffic_rec_list: try: res = SSUser.objects.filter(pk=rec['user_id']).values_list( 'upload_traffic', 'download_traffic')[0] SSUser.objects.filter(pk=rec['user_id']).update( upload_traffic=(res[0] + rec['u']), download_traffic=(res[1] + rec['d']), last_use_time=log_time) traffic = traffic_format(rec['u'] + rec['d']) trafficlog_model_list.append( TrafficLog( node_id=node_id, user_id=rec['user_id'], traffic=traffic, download_traffic=rec['d'], upload_traffic=rec['u'], log_time=log_time)) node_total_traffic = node_total_traffic + rec['u'] + rec['d'] res_data[rec['user_id']] = 'ok' except Exception as e: res_data[rec['user_id']] = 'fail' # 节点流量记录 node = Node.objects.get(node_id=node_id) node.used_traffic += node_total_traffic node.save() # 个人流量记录 TrafficLog.objects.bulk_create(trafficlog_model_list) res = {'ret': 1, 'data': res_data} return JsonResponse(res)
def checkin(request): '''用户签到''' ss_user = request.user.ss_user if not ss_user.get_check_in(): # 距离上次签到时间大于一天 增加随机流量 ll = randint(settings.MIN_CHECKIN_TRAFFIC, settings.MAX_CHECKIN_TRAFFIC) ss_user.transfer_enable += ll ss_user.last_check_in_time = timezone.now() ss_user.save() data = { 'title': '签到成功!', 'subtitle': '获得{}流量!'.format(traffic_format(ll)), 'status': 'success', } else: data = { 'title': '签到失败!', 'subtitle': '距离上次签到不足一天', 'status': 'error', } return JsonResponse(data)
def totla_transfer(self): return traffic_format(self.transfer_enable)
def save(self, *args, **kwargs): self.human_total_traffic = traffic_format(self.total_traffic) self.human_used_traffic = traffic_format(self.used_traffic) if self.node_type == 1: self.custom_method = 0 super(Node, self).save(*args, **kwargs)
def get_transfer(self): '''返回用户的总流量''' return traffic_format(self.transfer_enable)
def human_used_traffic(self): return traffic_format(self.used_traffic)
def human_increased_traffic(self): return traffic_format(self.increased_traffic)
def total_transfer(self): '''增加的流量''' return traffic_format(self.transfer)
def human_used_traffic(self): """已用流量""" return traffic_format(self.used_traffic)
def human_used_traffic(self): '''已用流量''' return traffic_format(self.used_traffic)
def human_total_traffic(self): '''总流量''' return traffic_format(self.total_traffic)
def get_traffic(self): '''返回用户使用的总流量''' return traffic_format(self.download_traffic + self.upload_traffic)
def get_unused_traffic(self): '''返回用户的剩余流量''' return traffic_format( self.transfer_enable - self.upload_traffic - self.download_traffic)
def unused_traffic(self): return traffic_format(self.transfer_enable - self.upload_traffic - self.download_traffic)
def get_transfer(self): '''增加的流量''' return traffic_format(self.transfer)
def human_total_traffic(self): """总流量""" return traffic_format(self.total_traffic)
def save(self, *args, **kwargs): self.human_total_traffic = traffic_format(self.total_traffic) self.human_used_traffic = traffic_format(self.used_traffic) super(Node, self).save(*args, **kwargs)
def get_user_traffic(cls, node_id, user_id): logs = cls.objects.filter(node_id=node_id, user_id=user_id) return traffic_format(sum([l.used_traffic for l in logs]))
def total_traffic(self): return traffic_format(self.download_traffic + self.upload_traffic)
def human_total_traffic(self): return traffic_format(self.total_traffic)
def get_unused_traffic(self): '''返回用户的剩余流量''' return traffic_format(self.transfer_enable - self.upload_traffic - self.download_traffic)
def total_transfer(self): """增加的流量""" return traffic_format(self.transfer)
def human_remain_traffic(self): return traffic_format(self.total_traffic - self.used_traffic)