display expired accounts
This commit is contained in:
		
							parent
							
								
									a236d86103
								
							
						
					
					
						commit
						9573a4f0ca
					
				
							
								
								
									
										7
									
								
								app.py
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								app.py
									
									
									
									
									
								
							@ -3,7 +3,7 @@ from flask import Flask, render_template, request, redirect, url_for, session, f
 | 
				
			|||||||
from flask_caching import Cache
 | 
					from flask_caching import Cache
 | 
				
			||||||
import requests.auth
 | 
					import requests.auth
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
from lib.datetime import filter_accounts_current_month
 | 
					from lib.datetime import filter_accounts_current_month, filter_accounts_expired
 | 
				
			||||||
from lib.reqs import get_urls, get_user_accounts, add_user_account, delete_user_account, get_user_accounts_count
 | 
					from lib.reqs import get_urls, get_user_accounts, add_user_account, delete_user_account, get_user_accounts_count
 | 
				
			||||||
from flask import send_from_directory
 | 
					from flask import send_from_directory
 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
@ -43,12 +43,15 @@ def index():
 | 
				
			|||||||
def home():
 | 
					def home():
 | 
				
			||||||
    base_url = app.config["BASE_URL"]  # Access base_url from the config
 | 
					    base_url = app.config["BASE_URL"]  # Access base_url from the config
 | 
				
			||||||
    all_accounts = get_user_accounts(base_url, session["auth_credentials"])
 | 
					    all_accounts = get_user_accounts(base_url, session["auth_credentials"])
 | 
				
			||||||
 | 
					    count = len(all_accounts)
 | 
				
			||||||
    current_month_accounts = filter_accounts_current_month(all_accounts)
 | 
					    current_month_accounts = filter_accounts_current_month(all_accounts)
 | 
				
			||||||
 | 
					    expired_accounts = filter_accounts_expired(all_accounts)
 | 
				
			||||||
    return render_template(
 | 
					    return render_template(
 | 
				
			||||||
        "home.html",
 | 
					        "home.html",
 | 
				
			||||||
        username=session["username"],
 | 
					        username=session["username"],
 | 
				
			||||||
        accounts=get_user_accounts_count(base_url, session["auth_credentials"]),
 | 
					        accounts=count,
 | 
				
			||||||
        current_month_accounts=current_month_accounts,
 | 
					        current_month_accounts=current_month_accounts,
 | 
				
			||||||
 | 
					        expired_accounts=expired_accounts,
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,15 +1,26 @@
 | 
				
			|||||||
from datetime import datetime
 | 
					from datetime import datetime
 | 
				
			||||||
from flask import render_template
 | 
					from typing import List, Dict
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def filter_accounts_current_month(accounts):
 | 
					def filter_accounts_current_month(accounts: List[Dict[str, int]]) -> List[Dict[str, int]]:
 | 
				
			||||||
    # Get the start and end of the current month
 | 
					    """Filter accounts whose expiry date falls within the current month.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Args:
 | 
				
			||||||
 | 
					        accounts (List[Dict[str, int]]): A list of account dictionaries, each containing 
 | 
				
			||||||
 | 
					            an 'expiaryDate' key with an epoch timestamp as its value.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Returns:
 | 
				
			||||||
 | 
					        List[Dict[str, int]]: A list of accounts expiring in the current month.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    # Get the start of the current month
 | 
				
			||||||
    now = datetime.now()
 | 
					    now = datetime.now()
 | 
				
			||||||
    start_of_month = datetime(now.year, now.month, 1)
 | 
					    start_of_month = datetime(now.year, now.month, 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Determine the start of the next month
 | 
				
			||||||
    if now.month == 12:
 | 
					    if now.month == 12:
 | 
				
			||||||
        # If current month is December, next month is January of the next year
 | 
					        # If the current month is December, next month is January of the next year
 | 
				
			||||||
        start_of_next_month = datetime(now.year + 1, 1, 1)
 | 
					        start_of_next_month = datetime(now.year + 1, 1, 1)
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        # Otherwise, next month is just the next month of the same year
 | 
					        # Otherwise, the next month is the following month of the same year
 | 
				
			||||||
        start_of_next_month = datetime(now.year, now.month + 1, 1)
 | 
					        start_of_next_month = datetime(now.year, now.month + 1, 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Convert start and end of the month to epoch timestamps
 | 
					    # Convert start and end of the month to epoch timestamps
 | 
				
			||||||
@ -23,3 +34,24 @@ def filter_accounts_current_month(accounts):
 | 
				
			|||||||
    ]
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return accounts_in_current_month
 | 
					    return accounts_in_current_month
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def filter_accounts_expired(accounts: List[Dict[str, int]]) -> List[Dict[str, int]]:
 | 
				
			||||||
 | 
					    """Filter accounts whose expiry date has passed.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Args:
 | 
				
			||||||
 | 
					        accounts (List[Dict[str, int]]): A list of account dictionaries, each containing 
 | 
				
			||||||
 | 
					            an 'expiaryDate' key with an epoch timestamp as its value.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Returns:
 | 
				
			||||||
 | 
					        List[Dict[str, int]]: A list of accounts that have expired.
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    # Get the current epoch timestamp
 | 
				
			||||||
 | 
					    current_timestamp = int(datetime.now().timestamp())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Filter accounts where the current date is greater than the expiryDate
 | 
				
			||||||
 | 
					    expired_accounts = [
 | 
				
			||||||
 | 
					        account for account in accounts
 | 
				
			||||||
 | 
					        if account['expiaryDate'] < current_timestamp
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return expired_accounts
 | 
				
			||||||
 | 
				
			|||||||
@ -58,6 +58,27 @@
 | 
				
			|||||||
            </tbody>
 | 
					            </tbody>
 | 
				
			||||||
        </table>
 | 
					        </table>
 | 
				
			||||||
    {% endif %}
 | 
					    {% endif %}
 | 
				
			||||||
 | 
					    {% if expired_accounts %}
 | 
				
			||||||
 | 
					        <h3>Expired Accounts</h3>
 | 
				
			||||||
 | 
					        <table class="table table-bordered table-striped">
 | 
				
			||||||
 | 
					            <thead class="thead-dark">
 | 
				
			||||||
 | 
					                <tr>
 | 
				
			||||||
 | 
					                    <th>Stream Name</th>
 | 
				
			||||||
 | 
					                    <th>Username</th>
 | 
				
			||||||
 | 
					                    <th>Expiry Date</th>
 | 
				
			||||||
 | 
					                </tr>
 | 
				
			||||||
 | 
					            </thead>
 | 
				
			||||||
 | 
					            <tbody>
 | 
				
			||||||
 | 
					                {% for account in expired_accounts %}
 | 
				
			||||||
 | 
					                    <tr>
 | 
				
			||||||
 | 
					                        <td>{{ account.stream }}</td>
 | 
				
			||||||
 | 
					                        <td>{{ account.username }}</td>
 | 
				
			||||||
 | 
					                        <td>{{ account.expiaryDate_rendered }}</td>
 | 
				
			||||||
 | 
					                    </tr>
 | 
				
			||||||
 | 
					                {% endfor %}
 | 
				
			||||||
 | 
					            </tbody>
 | 
				
			||||||
 | 
					        </table>
 | 
				
			||||||
 | 
					    {% endif %}    
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user