Skip to content

Commit

Permalink
Refactor ProxyNode filtering logic and add status column to node_occu…
Browse files Browse the repository at this point in the history
…pancy.html
  • Loading branch information
Ehco1996 committed Dec 22, 2023
1 parent e4e6430 commit d88481e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
21 changes: 15 additions & 6 deletions apps/proxy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,20 @@ def get_user_active_nodes(cls, user):
base_query = cls.get_active_nodes()
query = base_query.filter(level__lte=user.level)
# 2. filter out nodes that has been occupied by other users
occupied_node_ids = UserProxyNodeOccupancy.get_occupied_node_ids()
query = query.exclude(id__in=occupied_node_ids)
occupied_node_ids = [
i["proxy_node_id"] for i in UserProxyNodeOccupancy.get_occupied_node_ids()
]
not_occupied_node_ids = [
i["id"] for i in query.exclude(id__in=occupied_node_ids).values("id")
]
# 3. add nodes that has been occupied by this user
user_occupied_node_ids = UserProxyNodeOccupancy.get_user_occupied_node_ids(user)
query = query | base_query.filter(id__in=user_occupied_node_ids)
return query
user_occupied_node_ids = [
i["proxy_node_id"]
for i in UserProxyNodeOccupancy.get_user_occupied_node_ids(user)
]
return cls.objects.filter(id__in=not_occupied_node_ids) | cls.objects.filter(
id__in=user_occupied_node_ids
)

@classmethod
def calc_total_traffic(cls):
Expand Down Expand Up @@ -1016,7 +1024,8 @@ def check_and_incr_traffic(cls, user_id, proxy_node_id, traffic):

@classmethod
def get_user_occupancies(cls, user: User):
return cls._valid_occupancy_query().filter(user=user)
# no mater out of usage or not, return all occupancies
return cls.objects.filter(user=user)

def human_total_traffic(self):
return utils.traffic_format(self.total_traffic)
Expand Down
8 changes: 8 additions & 0 deletions templates/web/node_occupancy.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ <h4 class="title is-4">已购买</h4>
<th>已用流量</th>
<th>总流量</th>
<th>进度</th>
<th>状态</th>
</tr>
</thead>
<tbody>
Expand All @@ -96,6 +97,13 @@ <h4 class="title is-4">已购买</h4>
<progress class="progress {{ o.progress_color }}" value="{{ o.used_traffic }}"
max="{{ o.total_traffic }}"></progress>{{ o.used_percentage }}%
</td>
<td>
{% if o.out_of_usage %}
失效
{% else %}
正常
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
Expand Down

0 comments on commit d88481e

Please sign in to comment.