2019-07-02 07:34:12 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2020-09-13 06:46:07 +00:00
|
|
|
from django.forms import model_to_dict
|
2019-07-01 23:28:02 +00:00
|
|
|
from django.http import JsonResponse
|
2020-09-13 06:46:07 +00:00
|
|
|
from django.urls import reverse
|
2019-07-01 23:28:02 +00:00
|
|
|
|
2019-07-03 15:18:29 +00:00
|
|
|
from bookmarks.services.website_loader import load_website_metadata
|
2020-09-13 06:46:07 +00:00
|
|
|
from bookmarks.models import Bookmark
|
2019-07-01 23:28:02 +00:00
|
|
|
|
|
|
|
|
2019-07-02 07:34:12 +00:00
|
|
|
@login_required
|
2020-09-13 06:46:07 +00:00
|
|
|
def check_url(request):
|
2019-07-01 23:28:02 +00:00
|
|
|
url = request.GET.get('url')
|
2020-09-13 06:46:07 +00:00
|
|
|
bookmark = Bookmark.objects.filter(owner=request.user, url=url).first()
|
|
|
|
existing_bookmark_data = None
|
|
|
|
|
|
|
|
if bookmark is not None:
|
|
|
|
existing_bookmark_data = {
|
|
|
|
'id': bookmark.id,
|
|
|
|
'edit_url': reverse('bookmarks:edit', args=[bookmark.id])
|
|
|
|
}
|
|
|
|
|
2019-07-01 23:28:02 +00:00
|
|
|
metadata = load_website_metadata(url)
|
2020-09-13 06:46:07 +00:00
|
|
|
|
|
|
|
return JsonResponse({
|
|
|
|
'bookmark': existing_bookmark_data,
|
|
|
|
'metadata': metadata.to_dict()
|
|
|
|
})
|