Allow wl disconnect.
authorRadek Czajka <rczajka@rczajka.pl>
Thu, 29 Jun 2023 12:55:26 +0000 (14:55 +0200)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 29 Jun 2023 12:55:26 +0000 (14:55 +0200)
src/apiclient/urls.py
src/apiclient/views.py
src/documents/templates/documents/my_page.html
src/documents/views.py

index f061380..5317863 100644 (file)
@@ -10,4 +10,5 @@ urlpatterns = [
     path('oauth_callback/', views.oauth_callback, name='apiclient_oauth_callback'),
     path('oauth-beta/', views.oauth, kwargs={'beta': True}, name='apiclient_beta_oauth'),
     path('oauth_callback-beta/', views.oauth_callback, kwargs={'beta': True}, name='apiclient_beta_callback'),
+    path('disconnect', views.disconnect, name='apiclient_disconnect'),
 ]
index 666ef48..b2f3aac 100644 (file)
@@ -6,6 +6,7 @@ from urllib.parse import parse_qsl
 from django.contrib.auth.decorators import login_required
 from django.urls import reverse
 from django.http import HttpResponseRedirect, HttpResponse
+from django.views.decorators.http import require_POST
 import oauth2
 
 from apiclient.models import OAuthConnection
@@ -61,3 +62,10 @@ def oauth_callback(request, beta=False):
     conn.save()
 
     return HttpResponseRedirect('/')
+
+
+@login_required
+@require_POST
+def disconnect(request):
+    request.user.oauthconnection_set.filter(beta=False).delete()
+    return HttpResponseRedirect(reverse('documents_user'))
index 6828706..68c458e 100644 (file)
 {% endblock leftcolumn %}
 
 {% block rightcolumn %}
-<div class="card">
+  {% if wllogin %}
+    <div class="card">
+      <div class="card-header">
+        <h2>Konto WL</h2>
+      </div>
+      <div class="card-body">
+        <p>
+          Podłączone konto na Wolnych Lekturach:
+          <strong>{{ wllogin }}</strong>
+        </p>
+        <form method="post" action="{% url 'apiclient_disconnect' %}">
+          {% csrf_token %}
+          <button class="btn btn-danger">
+            Odłącz
+          </button>
+        </form>
+      </div>
+
+    </div>
+  {% endif %}
+
+  <div class="card">
        <div class="card-header">
         <h2>{% trans "Your last edited documents" %}</h2>
        </div>
index 39320d5..a930b8d 100644 (file)
@@ -23,7 +23,7 @@ from django.utils.translation import gettext_lazy as _
 from django.views.decorators.http import require_POST
 from django_cas_ng.decorators import user_passes_test
 
-from apiclient import NotAuthorizedError
+from apiclient import api_call, NotAuthorizedError
 from . import forms
 from . import helpers
 from .helpers import active_tab
@@ -65,9 +65,17 @@ def my(request):
         key=lambda x: x[1]['time'], reverse=True)
     for k, v in last_books:
         v['time'] = datetime.fromtimestamp(v['time'])
+    try:
+        resp = api_call(request.user, 'username/')
+    except NotAuthorizedError:
+        wllogin = None
+    else:
+        wllogin = resp['username']
+
     return render(request, 'documents/my_page.html', {
         'last_books': last_books,
         "logout_to": '/',
+        "wllogin": wllogin,
         })