Skip to content

Commit

Permalink
Refactor user configuration logic in proxy models
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 committed Jan 25, 2024
1 parent 459984e commit c4ba0e2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions apps/proxy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,9 +542,14 @@ def to_node_config(self, node: ProxyNode):
return configs

def to_user_config(self, node: ProxyNode, user: User):
enable = node.enable and user.total_traffic > (
have_shared_traffic = user.total_traffic > (
user.download_traffic + user.upload_traffic
)
have_oc_traffic = False
oc = UserProxyNodeOccupancy.get_by_proxy_node_and_user(node, user)
if oc:
have_oc_traffic = not oc.out_of_usage()
enable = node.enable and (have_shared_traffic or have_oc_traffic)
return {
"user_id": user.id,
"password": user.proxy_password,
Expand Down Expand Up @@ -596,9 +601,15 @@ def to_node_config(self, node: ProxyNode):
return configs

def to_user_config(self, node: ProxyNode, user: User):
enable = node.enable and user.total_traffic > (
have_shared_traffic = user.total_traffic > (
user.download_traffic + user.upload_traffic
)
have_oc_traffic = False
oc = UserProxyNodeOccupancy.get_by_proxy_node_and_user(node, user)
if oc:
have_oc_traffic = not oc.out_of_usage()
enable = node.enable and (have_shared_traffic or have_oc_traffic)

return {
"user_id": user.id,
"password": user.proxy_password,
Expand Down Expand Up @@ -1045,11 +1056,14 @@ def get_occupied_node_ids(cls):
def get_node_occupancy_user_ids(cls, node: ProxyNode):
return cls._valid_occupancy_query().filter(proxy_node=node).values("user_id")

@classmethod
@classmethod
def get_user_occupied_node_ids(cls, user: User):
return cls._valid_occupancy_query().filter(user=user).values("proxy_node_id")

@classmethod
def get_by_proxy_node_and_user(cls, proxy_node: ProxyNode, user: User):
return cls.objects.filter(proxy_node=proxy_node, user=user).first()

@classmethod
def get_node_occupancies(cls, node: ProxyNode):
return cls._valid_occupancy_query().filter(proxy_node=node)
Expand Down

0 comments on commit c4ba0e2

Please sign in to comment.