share collection
This commit is contained in:
parent
6e5d23ec3c
commit
468e4b8e62
6 changed files with 121 additions and 5 deletions
|
@ -27,6 +27,7 @@
|
|||
<link rel="stylesheet" href="{% static 'css/boofilsic.min.css' %}">
|
||||
<link rel="stylesheet" href="{% static 'lib/css/neo.css' %}">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/htmx/1.7.0/htmx.min.js"></script>
|
||||
<script src="{% static 'lib/js/hyperscript-0.9.5.min.js' %}"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
@ -95,7 +96,10 @@
|
|||
</h5>
|
||||
</div>
|
||||
</div>
|
||||
{% if request.user != collection.owner %}
|
||||
</div>
|
||||
|
||||
{% if request.user != collection.owner %}
|
||||
<div class="aside-section-wrapper">
|
||||
<div class="action-panel">
|
||||
<div class="action-panel__button-group action-panel__button-group--center">
|
||||
{% if following %}
|
||||
|
@ -111,7 +115,17 @@
|
|||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="aside-section-wrapper">
|
||||
<div class="action-panel">
|
||||
<div class="action-panel__button-group action-panel__button-group--center">
|
||||
<form>
|
||||
<button class="action-panel__button add-to-list" hx-get="{% url 'collection:share' collection.id %}" hx-target="body" hx-swap="beforeend">{% trans '分享到联邦网络' %}</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
56
collection/templates/share_collection.html
Normal file
56
collection/templates/share_collection.html
Normal file
|
@ -0,0 +1,56 @@
|
|||
{% load static %}
|
||||
{% load i18n %}
|
||||
{% load l10n %}
|
||||
{% load humanize %}
|
||||
{% load admin_url %}
|
||||
{% load mastodon %}
|
||||
{% load oauth_token %}
|
||||
{% load truncate %}
|
||||
{% load highlight %}
|
||||
{% load thumb %}
|
||||
|
||||
<div id="modal" _="on closeModal add .closing then wait for animationend then remove me">
|
||||
<div class="modal-underlay" _="on click trigger closeModal"></div>
|
||||
<div class="modal-content">
|
||||
<div class="add-to-list-modal__head">
|
||||
<span class="add-to-list-modal__title">{% trans '分享收藏单' %}</span>
|
||||
<span class="add-to-list-modal__close-button modal-close" _="on click trigger closeModal">
|
||||
<span class="icon-cross">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
|
||||
<polygon
|
||||
points="20 2.61 17.39 0 10 7.39 2.61 0 0 2.61 7.39 10 0 17.39 2.61 20 10 12.61 17.39 20 20 17.39 12.61 10 20 2.61">
|
||||
</polygon>
|
||||
</svg>
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
<div class="add-to-list-modal__body">
|
||||
<form action="/collections/share/{{ id }}/" method="post">
|
||||
{% csrf_token %}
|
||||
<div>
|
||||
<label for="id_visibility_0">分享可见性(不同于收藏单本身的权限):</label>
|
||||
<ul id="id_visibility">
|
||||
<li><label for="id_visibility_0"><input type="radio" name="visibility" value="0" required="" id="id_visibility_0" {% if visibility == 0 %}checked{% endif %}>
|
||||
公开</label>
|
||||
|
||||
</li>
|
||||
<li><label for="id_visibility_1"><input type="radio" name="visibility" value="1" required="" id="id_visibility_1" {% if visibility == 1 %}checked{% endif %}>
|
||||
仅关注者</label>
|
||||
|
||||
</li>
|
||||
<li><label for="id_visibility_2"><input type="radio" name="visibility" value="2" required="" id="id_visibility_2" {% if visibility == 2 %}checked{% endif %}>
|
||||
仅自己</label>
|
||||
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div>
|
||||
<textarea type="text" name="comment" placeholder="分享附言"></textarea>
|
||||
</div>
|
||||
<div class="add-to-list-modal__confirm-button">
|
||||
<input type="submit" class="button float-right" value="{% trans '提交' %}">
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -20,6 +20,7 @@ urlpatterns = [
|
|||
path('<int:id>/show_item_comment/<int:item_id>', show_item_comment, name='show_item_comment'),
|
||||
path('with/<str:type>/<int:id>/', list_with, name='list_with'),
|
||||
path('add_to_list/<str:type>/<int:id>/', add_to_list, name='add_to_list'),
|
||||
path('share/<int:id>/', share, name='share'),
|
||||
|
||||
# TODO: tag
|
||||
]
|
||||
|
|
|
@ -10,9 +10,9 @@ from django.utils import timezone
|
|||
from django.core.paginator import Paginator
|
||||
from mastodon import mastodon_request_included
|
||||
from mastodon.models import MastodonApplication
|
||||
from mastodon.api import post_toot, TootVisibilityEnum
|
||||
from mastodon.api import post_toot, TootVisibilityEnum, share_collection
|
||||
from common.utils import PageLinksGenerator
|
||||
from common.views import PAGE_LINK_NUMBER, jump_or_scrape
|
||||
from common.views import PAGE_LINK_NUMBER, jump_or_scrape, go_relogin
|
||||
from common.models import SourceSiteEnum
|
||||
from .models import *
|
||||
from .forms import *
|
||||
|
@ -413,3 +413,19 @@ def add_to_list(request, type, id):
|
|||
collection = Collection.objects.filter(owner=request.user, id=cid).first()
|
||||
collection.append_item(item, request.POST.get('comment'))
|
||||
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
|
||||
|
||||
|
||||
@login_required
|
||||
def share(request, id):
|
||||
collection = Collection.objects.filter(id=id).first()
|
||||
if not collection:
|
||||
return HttpResponseBadRequest()
|
||||
if request.method == 'GET':
|
||||
return render(request, 'share_collection.html', {'id': id, 'visibility': request.user.get_preference().default_visibility})
|
||||
else:
|
||||
visibility = int(request.POST.get('visibility', default=0))
|
||||
comment = request.POST.get('comment')
|
||||
if share_collection(collection, comment, request.user, visibility):
|
||||
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
|
||||
else:
|
||||
return go_relogin(request)
|
||||
|
|
|
@ -159,4 +159,8 @@
|
|||
|
||||
#modal .add-to-list-modal__confirm-button {
|
||||
float: right;
|
||||
}
|
||||
}
|
||||
|
||||
#modal li, #modal ul, #modal label {
|
||||
display: inline;
|
||||
}
|
||||
|
|
|
@ -433,3 +433,28 @@ def share_review(review):
|
|||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def share_collection(collection, comment, user, visibility_no):
|
||||
if visibility_no == 2:
|
||||
visibility = TootVisibilityEnum.DIRECT
|
||||
elif visibility_no == 1:
|
||||
visibility = TootVisibilityEnum.PRIVATE
|
||||
elif user.get_preference().mastodon_publish_public:
|
||||
visibility = TootVisibilityEnum.PUBLIC
|
||||
else:
|
||||
visibility = TootVisibilityEnum.UNLISTED
|
||||
tags = '\n' + user.get_preference().mastodon_append_tag.replace('[category]', '收藏单') if user.get_preference().mastodon_append_tag else ''
|
||||
content = f"分享收藏单《{collection.title}》\n{collection.url}\n{comment}{tags}"
|
||||
response = post_toot(user.mastodon_site, content, visibility, user.mastodon_token)
|
||||
if response and response.status_code in [200, 201]:
|
||||
j = response.json()
|
||||
if 'url' in j:
|
||||
shared_link = j['url']
|
||||
elif 'data' in j:
|
||||
shared_link = f"https://twitter.com/{user.username}/status/{j['data']['id']}"
|
||||
if shared_link:
|
||||
pass
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
|
Loading…
Add table
Reference in a new issue