Exemple #1
0
 def create(self, validated_data):
     bookmark = Bookmark()
     bookmark.url = validated_data['url']
     bookmark.title = validated_data['title']
     bookmark.description = validated_data['description']
     tag_string = build_tag_string(validated_data['tag_names'], ' ')
     return create_bookmark(bookmark, tag_string, self.context['user'])
def _post_bookmarks(request):
    try:
        url = request.POST['url']
        title = request.POST['title']
        form = BookmarkForm({'url': url, 'title': title})
        if form.is_valid():
            bookmark = Bookmark()
            bookmark.url = url
            bookmark.title = title
            bookmark.save()
            return JsonResponse({}, status=201)
        else:
            data = {
                'errors': [{
                    'title': '入力エラー',
                    'data': json.loads(form.errors.as_json())
                }]
            }
            return JsonResponse(data, status=400)

    except:
        data = {
            'errors': [{
                'title': 'ブックマークを登録できませんでした'
            }]
        }
        return JsonResponse(data, status=500)
 def handle_data(self, data):
     if self.in_a:
         bookmark = Bookmark()
         bookmark.url = self.attrs.get('href')
         bookmark.title = data
         bookmark.bookmarked_at = unixtime2datetime(self.attrs.get('add_date'))
         try:
             bookmark.save()
         except IntegrityError:
             pass
 def handle_data(self, data):
     if self.in_a:
         bookmark = Bookmark()
         bookmark.url = self.attrs.get('href')
         bookmark.title = data
         bookmark.bookmarked_at = unixtime2datetime(
             self.attrs.get('add_date'))
         try:
             bookmark.save()
         except IntegrityError:
             pass
Exemple #5
0
def add(request):
    """ Adds a new bookmark
    
    Expects the url to be provided by the url POST value.
    
    The URL is automatically validated, if it isn't a valid URL, then http:// is prepended to it. If that fails, then
    the bookmark isn't added and an error is sent.
    
    The title for the bookmark is automatically downloaded based on the URL.
    
    If it succeeds, it returns the JSON representation of thebookmark it has added.
    
    If it fails it returns a JSON object with only an "error" property.
    """
    if "url" not in request.POST:
        raise SuspiciousOperation
    
    url = request.POST["url"]
    val = URLValidator()
    
    if request.user.settings.url_settings == Settings.URL_SETTINGS_VALIDATE:
        try:
            val(url)
        except ValidationError:
            try:
                val("http://"+url)
                url = "http://"+url
            except ValidationError:
                return HttpResponse('{"error":"Invalid URL"}', content_type="application/json", status=422)
    
    bm = Bookmark(owner=request.user, url=url)
    
    bm.title = url
    bm.save()
    if bm.valid_url:
        bm.download_title()
        bm.save()
    
    if "tag" in request.POST:
        bm.tag(request.POST["tag"])
    
    bm.autotag_rules()
    
    return HttpResponse(bm.to_json(), content_type="application/json")
Exemple #6
0
def delicious_import(request):
    if request.method == 'POST':
        form = DeliciousImportForm(request.POST, request.FILES)
        if form.is_valid():
            try:
                soup = BeautifulStoneSoup(request.FILES['delicious_html'],
                    convertEntities="html", smartQuotesTo="html")
            except TypeError:
                raise forms.ValidationError("Not a delicious HTML file")
            rows = soup.findAll(['dt','dd'])
            for row in rows:
                if row.name == 'dd':
                    continue
                else:
                    bookmark = Bookmark()
                    link = row.first()
                    bookmark.title = link.text
                    bookmark.url = link['href']
                    bookmark.owner = request.user
                    pub_date = datetime.utcfromtimestamp(int(link['add_date']))
                    bookmark.pub_date = pub_date
                    bookmark.private = link['private'] == u"1"
                    if row.find('dd'):
                        bookmark.description = row.find('dd').text
                    bookmark.save()
                    if link.has_key('tags'):
                        tags = link['tags'].split(',')
                        for tag in tags:
                            if len(tag) > 0:
                                bookmark.tags.add(tag)
                        bookmark.save()
            url = reverse("user-list", args=[request.user.username])
            return HttpResponseRedirect(url)
    else:
        form = DeliciousImportForm()
    template_name = "bookmarks/delicious_import.html"
    context = {'form': form}
    return(template_name, context)
def _post_bookmarks(request):
    try:
        url = request.POST['url']
        title = request.POST['title']
        form = BookmarkForm({'url': url, 'title': title})
        if form.is_valid():
            bookmark = Bookmark()
            bookmark.url = url
            bookmark.title = title
            bookmark.save()
            return JsonResponse({}, status=201)
        else:
            data = {
                'errors': [{
                    'title': '入力エラー',
                    'data': json.loads(form.errors.as_json())
                }]
            }
            return JsonResponse(data, status=400)

    except:
        data = {'errors': [{'title': 'ブックマークを登録できませんでした'}]}
        return JsonResponse(data, status=500)
Exemple #8
0
def _merge_bookmark_data(from_bookmark: Bookmark, to_bookmark: Bookmark):
    to_bookmark.title = from_bookmark.title
    to_bookmark.description = from_bookmark.description
Exemple #9
0
 def update(self, instance: Bookmark, validated_data):
     instance.url = validated_data['url']
     instance.title = validated_data['title']
     instance.description = validated_data['description']
     tag_string = build_tag_string(validated_data['tag_names'], ' ')
     return update_bookmark(instance, tag_string, self.context['user'])