mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-23 23:48:56 +01:00
Use sql datetime comparison for detecting validity of subscription renewal date
- Update the unsubscribe endpoint to use query params - Use subscription id to process unsubscribe endpoint, rather than the customer id
This commit is contained in:
parent
98cf095b65
commit
609d358b1a
4 changed files with 16 additions and 18 deletions
|
@ -2,7 +2,7 @@ from typing import Type, TypeVar, List
|
|||
from datetime import date, datetime, timedelta
|
||||
import secrets
|
||||
from typing import Type, TypeVar, List
|
||||
from datetime import date
|
||||
from datetime import date, timezone
|
||||
|
||||
from django.db import models
|
||||
from django.contrib.sessions.backends.db import SessionStore
|
||||
|
@ -116,12 +116,9 @@ async def set_user_subscribed(email: str, type="standard") -> KhojUser:
|
|||
|
||||
|
||||
def is_user_subscribed(email: str, type="standard") -> bool:
|
||||
user = KhojUser.objects.filter(email=email, subscription_type=type).first()
|
||||
if user and user.subscription_renewal_date:
|
||||
is_subscribed = user.subscription_renewal_date > date.today()
|
||||
return is_subscribed
|
||||
else:
|
||||
return False
|
||||
return KhojUser.objects.filter(
|
||||
email=email, subscription_type=type, subscription_renewal_date__gte=datetime.now(tz=timezone.utc)
|
||||
).exists()
|
||||
|
||||
|
||||
async def get_user_by_token(token: dict) -> KhojUser:
|
||||
|
|
|
@ -184,8 +184,7 @@
|
|||
</a>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="card-action-row"
|
||||
style="display: {% if not current_model_state.github %}none{% endif %}">
|
||||
<div class="card-action-row">
|
||||
<button class="card-button" onclick="unsubscribe()">
|
||||
Unsubscribe
|
||||
</button>
|
||||
|
@ -259,14 +258,11 @@
|
|||
};
|
||||
|
||||
function unsubscribe() {
|
||||
fetch('/api/subscription', {
|
||||
fetch('/api/subscription?email=' + '{{username}}', {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
"email": "{{ username }}"
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -776,10 +776,13 @@ async def subscribe(request: Request):
|
|||
|
||||
@api.delete("/subscription")
|
||||
@requires(["authenticated"])
|
||||
async def unsubscribe(request: Request, user_email: str):
|
||||
customer = stripe.Customer.list(email=user_email).data
|
||||
async def unsubscribe(request: Request, email: str):
|
||||
customer = stripe.Customer.list(email=email).data
|
||||
if not is_none_or_empty(customer):
|
||||
stripe.Subscription.modify(customer[0].id, cancel_at_period_end=True)
|
||||
customer_id = customer[0].id
|
||||
for subscription in stripe.Subscription.list(customer=customer_id):
|
||||
stripe.Subscription.modify(subscription.id, cancel_at_period_end=True)
|
||||
|
||||
success = True
|
||||
else:
|
||||
success = False
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# System Packages
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
import json
|
||||
import os
|
||||
|
||||
|
@ -119,7 +119,9 @@ def config_page(request: Request):
|
|||
user: KhojUser = request.user.object
|
||||
user_picture = request.session.get("user", {}).get("picture")
|
||||
user_is_subscribed = is_user_subscribed(user.email)
|
||||
days_to_renewal = (user.subscription_renewal_date - datetime.now()).days if user.subscription_renewal_date else 0
|
||||
days_to_renewal = (
|
||||
(user.subscription_renewal_date - datetime.now(tz=timezone.utc)).days if user.subscription_renewal_date else 0
|
||||
)
|
||||
enabled_content_source = set(EntryAdapters.get_unique_file_source(user).all())
|
||||
|
||||
successfully_configured = {
|
||||
|
|
Loading…
Reference in a new issue