from django.utils.timezone import now import googlemaps from manage_subscriptions.models import PrincipalSubscription, SubscriptionStatus from django.conf import settings API_KEY = settings.GOOGLE_MAPS_API_KEY gmaps = googlemaps.Client(key=API_KEY) def get_active_subscription_id_for_principal(principal): # Filter subscriptions for the principal that are active and not cancelled active_subscriptions = PrincipalSubscription.objects.filter( principal=principal, status=SubscriptionStatus.ACTIVE, is_paid=True, cancelled=False, deleted=False, active=True, end_date__gte=now().date(), # Ensure the subscription hasn't expired ).order_by( "-end_date" ) # Order by end_date to get the most recent active subscription if active_subscriptions.exists(): # Return the ID of the most recent active subscription return active_subscriptions.first().id return None def get_location_info(latitude, longitude): reverse_geocode_result = gmaps.reverse_geocode((latitude, longitude)) if reverse_geocode_result: location = reverse_geocode_result[0] city = None state = None country = None for component in location.get("address_components", []): types = component.get("types", []) print("types: ", types) if "locality" in types: city = component.get("long_name") elif "administrative_area_level_1" in types: state = component.get("long_name") elif "country" in types: country = component.get("long_name") return {"city": city, "state": state, "country": country} else: return {}