Ejemplo n.º 1
0
 def setup_bookmark(
     self,
     is_archived: bool = False,
     tags=None,
     user: User = None,
     url: str = '',
     title: str = '',
     description: str = '',
     website_title: str = '',
     website_description: str = '',
 ):
     if tags is None:
         tags = []
     if user is None:
         user = self.get_or_create_test_user()
     if not url:
         unique_id = get_random_string(length=32)
         url = 'https://example.com/' + unique_id
     bookmark = Bookmark(url=url,
                         title=title,
                         description=description,
                         website_title=website_title,
                         website_description=website_description,
                         date_added=timezone.now(),
                         date_modified=timezone.now(),
                         owner=user,
                         is_archived=is_archived)
     bookmark.save()
     for tag in tags:
         bookmark.tags.add(tag)
     bookmark.save()
     return bookmark
Ejemplo n.º 2
0
    def test_can_update_bookmark(self):
        bookmark = Bookmark(
            user=self.user,
            name='Bookmark 1',
            url='https://www.google.com',
            notes='This is the first note.',
        )

        bookmark.save()

        form_data = {
            'name': 'Bookmark 8',
            'url': 'https://www.google123.com',
            'notes': 'This is the first note.',
        }

        self.client.force_login(self.user)

        update_url = "/bookmarks/edit/%s" % bookmark.id
        response = self.client.post(update_url, data=form_data)

        self.assertEquals(response.status_code, 302)
        self.assertRedirects(response, '/bookmarks/')

        bookmark.refresh_from_db()

        self.assertEquals(form_data['name'], bookmark.name)
        self.assertEquals(form_data['url'], bookmark.url)
        self.assertEquals(form_data['notes'], bookmark.notes)
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)
Ejemplo n.º 4
0
    def save(self):

        try:
            bookmark = Bookmark(user=self.instance.user,
                                sound=self.instance.sound)

            if not self.cleaned_data['category']:
                if self.cleaned_data['new_category_name'] != "":
                    category = BookmarkCategory(
                        user=self.instance.user,
                        name=self.cleaned_data['new_category_name'])
                    category.save()
                    bookmark.category = category
                    self.cleaned_data['category'] = category
            else:
                bookmark.category = self.cleaned_data['category']

            if self.cleaned_data['name'] != "":
                bookmark.name = self.cleaned_data['name']

            bookmark.save()
            return True

        except:
            return False
Ejemplo n.º 5
0
def add_bookmark(request):
    incorrect_url = False
    added = False
    in_db = False
    if request.POST:
        url = request.POST["url"]
        if not url.startswith("http://www."):
            if not url.startswith("www."):
                url = "http://www." + url
            else:
                url = "http://" + url
        if correct_url(url):
            username = request.user.username
            if Bookmark.objects.filter(username=username, url=url).count():
                in_db = True
            else:
                metadata = parse_meta(url)
                bookmark = Bookmark(username=username,
                                    url=url, title=metadata["title"],
                                    favicon=metadata["favicon"],
                                    description=metadata["description"])
                bookmark.save()
                added = True
        else:
            incorrect_url = not correct_url(url)
    context = {"incorrect_url": incorrect_url, "added": added, "in_db": in_db}
    return render(request, "bookmarks/add_bookmark.html", context)
Ejemplo n.º 6
0
def update_bookmark(bookmark: Bookmark, tag_string, current_user: User):
    # Update website info
    _update_website_metadata(bookmark)
    # Update tag list
    _update_bookmark_tags(bookmark, tag_string, current_user)
    # Update dates
    bookmark.date_modified = timezone.now()
    bookmark.save()
    return bookmark
 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
Ejemplo n.º 8
0
def save(request):
    form = BookmarksSaveForm(request.POST)
    if not form.is_valid():
        return HttpResponseBadRequest(json.dumps({'error': form.errors}))
    content = form.cleaned_data['content']
    id = md5(content.encode('utf-8')).hexdigest()
    book = Bookmark(id=id, content=content)
    book.save()
    response = HttpResponse(json.dumps({'id': id}))
    response['Content-Type'] = "application/json"
    return response
 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
Ejemplo n.º 10
0
def _handle_import(contents, use_tags, owner):
    """ Handles the import of a bookmarks file
    
    Loops through all links in the file and adds them as bookmarks. It then tags them if the file contains tags and
    "use_tags" is true.
    """
    
    lines = contents.decode("utf-8").split("\n")
    
    title = re.compile(r"<a.*?>(.+?)</a>", re.I)
    url = re.compile(r"""<a.*href=['"](.+?)['"]""", re.I)
    tags = re.compile(r"""<a.*?tags=["'](.+?)["']""", re.I)
    addTime = re.compile(r"""<a.*?add_date=["'](\d+?)["']""", re.I)
    
    for l in lines:
        if "<a" in l.lower() and "</a>" in l.lower():
            bookmark = {}
            
            bookmark["title"] = title.search(l)
            if not bookmark["title"]:
                continue
            bookmark["title"] = _unescape(bookmark["title"].group(1))
            
            bookmark["url"] = url.search(l)
            if not bookmark["url"]:
                continue
            bookmark["url"] = _unescape(bookmark["url"].group(1))
            
            bookmark["tags"] = [];
            if use_tags:
                result = tags.search(l)
                if result:
                    bookmark["tags"] = map(_unescape, result.group(1).split(","))
            
            bookmark["added"] = addTime.search(l)
            if bookmark["added"]:
                bookmark["added"] = bookmark["added"].group(1)
            
            if not Bookmark.objects.filter(owner=owner, url=bookmark["url"]).exists():
                bm = Bookmark(owner=owner, url=bookmark["url"], title=bookmark["title"])
                
                bm.save()
                if bookmark["added"]:
                    bm.added = datetime.datetime.fromtimestamp(int(bookmark["added"]))
                
                for t in bookmark["tags"]:
                    bm.tag(t)
                
                bm.save()
                bm.autotag_rules()
Ejemplo n.º 11
0
    def test_archive_bookmark(self):
        bookmark = Bookmark(url='https://example.com',
                            date_added=timezone.now(),
                            date_modified=timezone.now(),
                            owner=self.user)
        bookmark.save()

        self.assertFalse(bookmark.is_archived)

        archive_bookmark(bookmark)

        updated_bookmark = Bookmark.objects.get(id=bookmark.id)

        self.assertTrue(updated_bookmark.is_archived)
    def handle(self, *args, **options):
        '''
        The layout of the export puts links into <dt> tags and any related
        comments into a <dd> tag immediately following.
        If a link has no comments then the <dd> tag is not present.
        Ignore all other tags
        '''

        user = get_user_model().objects.get(pk=1)
        # Load the export html into a beautiful soup object
        soup = BeautifulSoup(open('delicious.html'))

        print 'Parsing export with %s rows' % len(soup.dl.contents)
        for i in range(len(soup.dl.contents)):
            if soup.dl.contents[i].name == 'dt':
                print('{row}: Creating new bookmark for {url}'.format(
                    row=i, url=soup.dl.contents[i].a['href']))
                bookmark = Bookmark(
                    user=user,
                    url=soup.dl.contents[i].a['href'],
                    title=soup.dl.contents[i].a.string,
                    saved=timezone.make_aware(
                        timezone.datetime.fromtimestamp(
                            float(soup.dl.contents[i].a['add_date'])),
                        timezone.get_current_timezone()))
                print('    {row}: Bookmark title: {title}'.format(
                    row=i, title=soup.dl.contents[i].a.text.encode('utf-8')))

                try:
                    # Check for any comments for this bookmark
                    if soup.dl.contents[(i + 1)].name == 'dd':
                        print('    {row}: Got comments: {comments}'.format(
                            row=i, comments=soup.dl.contents[(i + 1)].string))
                        bookmark.comment = soup.dl.contents[(i + 1)].string
                except IndexError:
                    # We've reached the end of the export file
                    pass
                bookmark.save()
                # Add the tags
                for tag in soup.dl.contents[i].a['tags'].split(','):
                    print('        {row}: Found tag {tag}'.format(row=i,
                                                                  tag=tag))
                    if tag != '':
                        try:
                            the_tag = Tag.objects.get(name=tag.strip(' '))
                        except Tag.DoesNotExist as e:
                            the_tag = Tag(name=tag.strip(' '))
                            the_tag.save()
                        bookmark.tags.add(the_tag)
Ejemplo n.º 13
0
def bookmarklet_save(request):
    # where the bookmarklet first lands them
    if request.method == 'GET':
        if request.GET['link']:
            link = request.GET['link']

            # check user and save url
            user_profile = get_object_or_404(UserProfile, user = request.user)
            check = Bookmark.objects.filter(url = link, owner = user_profile)

            if not check:
                # if they have the old bookmarklet, load the title the old way
                if 'title' in request.GET:
                    title = request.GET['title']
                else:
                    messages.add_message(request, messages.INFO, "There's been an error. You're using an old version of our Bookmarklet. "
                            + "Just drag the new version on this site to your bookmarks bar and you will be good to go!")

                    return redirect(home)

            else:
                messages.add_message(request, messages.INFO, 'You have already added this mark')
                return redirect(home)
    
    # post vars, AKA time to save the bookmark
    elif request.method == 'POST':
        link = request.POST['link']
        title = request.POST['title']
        privacy = request.POST['privacy']

        if privacy == 'private':
            public = False
        elif privacy == 'public':
            public = True

        user_profile = get_object_or_404(UserProfile, user = request.user)

        m = Bookmark(url = link, owner = user_profile, title = title, public = public)
        m.save()
        messages.add_message(request, messages.INFO, 'Consider that marked')

        return redirect(home)

    # they got here somehow without post or get vars
    else:
        return redirect(home)

    return render_to_response('bookmarks/save.html', { 'title': title, 'link': link }, 
            context_instance=RequestContext(request) )
Ejemplo n.º 14
0
def add(ctx, url, tags, verbose, validate):
    click.echo()

    # TODO better user-agent header
    # TODO app name
    # TODO version
    headers = {'user-agent': '{}/{}'.format("my-bookmark-cli-app", "0.0.0")}
    r = get(url, headers=headers)

    # TODO make something better for other status codes
    if (not validate) or (validate and r.status_code == 200):
        if verbose:
            display_url(url)
            tags = list(set(tags))
            display_tags(tags)
            click.echo()

        backend.begin()
        ts = []
        for tag in tags:
            ts += [Tag.find_or_create_new(backend, tag)]

        page_title = BeautifulSoup(r.text, "html.parser").title.text
        b = Bookmark({
            'url': url,
            'tags': ts,
            'title': page_title
        })
        b.save(backend)

        if verbose >= 2:
            click.echo()
            click.echo(
                click.style("URL: ") +
                click.style(b.url, fg='green')
            )
            click.echo("Tags: {}".format(b.tags))
            click.echo("Title: {}".format(b.title))
            for tag in b.tags:
                click.echo("name: {} value: {}".format(
                    tag.name,
                    tag.value
                ))

        if (True):
            backend.commit()
    else:
        click.echo(r.status_code)
Ejemplo n.º 15
0
def new_bookmark(request):
    if request.method == 'POST':
        bookmark = Bookmark()
        form = NewBookmarkForm(request.POST, instance=bookmark)
        if form.is_valid():
            bookmark.owner = request.user
            bookmark.save()
            for tag in form.cleaned_data['tags']:
                bookmark.tags.add(tag)
            bookmark.save()
            url = reverse("user-list", args=[request.user.username])
            return HttpResponseRedirect(url)
    else:
        form = NewBookmarkForm()
    template_name = "bookmarks/new_bookmark.html"
    context = {'form': form}
    return(template_name, context)
Ejemplo n.º 16
0
 def setup_bookmark(self,
                    is_archived: bool = False,
                    tags: [Tag] = [],
                    user: User = None):
     if user is None:
         user = self.get_or_create_test_user()
     unique_id = get_random_string(length=32)
     bookmark = Bookmark(url='https://example.com/' + unique_id,
                         date_added=timezone.now(),
                         date_modified=timezone.now(),
                         owner=user,
                         is_archived=is_archived)
     bookmark.save()
     for tag in tags:
         bookmark.tags.add(tag)
     bookmark.save()
     return bookmark
Ejemplo n.º 17
0
def bookmarks_ajax(request):
    response_data = {}
    response_data['success'] = False
    response_data['errors'] = ''

    if not request.method == 'POST'or not request.is_ajax():
        return http.HttpResponseForbidden('Forbidden.')

    data = request.POST
    files = request.FILES
    form = BookmarkForm(data, files, user=request.user)

    if form.is_valid():
        try:
            b = Bookmark.objects.get(article_id=form.cleaned_data.get('article_id'),
                                     user=request.user)
            b.delete()
        except Bookmark.DoesNotExist:
            b = Bookmark(article_id=form.cleaned_data.get('article_id'),
                         user=request.user)
            b.save()

        # Delete bookmarks session key so they get rebuilt on next request
        # via middleware
        if request.session.get('bookmarks'):
            del request.session['bookmarks']

        # Delete article instance cache
        a = Article.objects.get(pk=form.cleaned_data.get('article_id'))
        a.clear_cache()

        # Delete user dashboard cache if possible
        try:
            urlpath = URLPath.objects.get(slug=request.user.username)
            urlpath.article.clear_cache()
        except:
            pass

        response_data['success'] = True
    else:
        response_data['errors'] = dict((k, map(unicode, v))
                                       for (k, v) in form.errors.iteritems())

    return http.HttpResponse(simplejson.dumps(response_data),
                             mimetype='application/json; charset=UTF-8')
Ejemplo n.º 18
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")
Ejemplo n.º 19
0
    def handle(self, **options):
        with open(options['path'], 'r') as file:
            data = json.load(file)

        self.log_message(
            u"Found {} bookmarks".format(len(data))
        )

        for bookmark in data:
            self.log_message(u"{}".format(bookmark['title']))
            b = Bookmark(
                title=bookmark['title'],
                description=bookmark['description'],
                date_added=bookmark['date_added'],
                private=bookmark['private'],
                url=bookmark['url']
            )
            b.save()
            for tag in bookmark['tags']:
                b.tags.add(tag)
Ejemplo n.º 20
0
def create_bookmark(bookmark: Bookmark, tag_string: str, current_user: User):
    # If URL is already bookmarked, then update it
    existing_bookmark: Bookmark = Bookmark.objects.filter(
        owner=current_user, url=bookmark.url).first()

    if existing_bookmark is not None:
        _merge_bookmark_data(bookmark, existing_bookmark)
        return update_bookmark(existing_bookmark, tag_string, current_user)

    # Update website info
    _update_website_metadata(bookmark)
    # Set currently logged in user as owner
    bookmark.owner = current_user
    # Set dates
    bookmark.date_added = timezone.now()
    bookmark.date_modified = timezone.now()
    bookmark.save()
    # Update tag list
    _update_bookmark_tags(bookmark, tag_string, current_user)
    bookmark.save()
    return bookmark
Ejemplo n.º 21
0
 def save(self):
     
     try:
         bookmark = Bookmark(user=self.instance.user,sound=self.instance.sound)
         
         if not self.cleaned_data['category']:
             if self.cleaned_data['new_category_name'] != "":
                 category = BookmarkCategory(user=self.instance.user, name=self.cleaned_data['new_category_name'])
                 category.save()
                 bookmark.category = category
                 self.cleaned_data['category'] = category
         else:
             bookmark.category = self.cleaned_data['category']
         
         if self.cleaned_data['name'] != "":
             bookmark.name = self.cleaned_data['name']
         
         bookmark.save()
         return True
     
     except:
         return False
Ejemplo n.º 22
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)
Ejemplo n.º 24
0
class BookmarksDeleteTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user(username='******',
                                             email='*****@*****.**',
                                             password='******')

        self.bookmark = Bookmark(
            user=self.user,
            name='Bookmark 1',
            url='https://www.google.com',
            notes='This is the first note.',
        )

        self.bookmark.save()

    def test_can_see_delete_bookmark_form(self):
        self.client.force_login(self.user)

        delete_url = "/bookmarks/delete/%s" % self.bookmark.id
        response = self.client.get(delete_url)

        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'confirm_delete.html')
    def handle(self, *args, **options):
        '''
        The layout of the export puts links into <dt> tags and any related
        comments into a <dd> tag immediately following.
        If a link has no comments then the <dd> tag is not present.
        Ignore all other tags
        '''

        user = get_user_model().objects.get(pk=1)
        # Load the export html into a beautiful soup object
        soup = BeautifulSoup(open('delicious.html'))

        print 'Parsing export with %s rows' % len(soup.dl.contents)
        for i in range(len(soup.dl.contents)):
            if soup.dl.contents[i].name == 'dt':
                print(
                    '{row}: Creating new bookmark for {url}'.format(
                        row=i, url=soup.dl.contents[i].a['href']
                    )
                )
                bookmark = Bookmark(
                    user=user,
                    url=soup.dl.contents[i].a['href'],
                    title=soup.dl.contents[i].a.string,
                    saved=timezone.make_aware(
                        timezone.datetime.fromtimestamp(
                            float(soup.dl.contents[i].a['add_date'])
                        ),
                        timezone.get_current_timezone()
                    )
                )
                print(
                    '    {row}: Bookmark title: {title}'.format(
                        row=i,
                        title=soup.dl.contents[i].a.text.encode('utf-8')
                    )
                )

                try:
                    # Check for any comments for this bookmark
                    if soup.dl.contents[(i + 1)].name == 'dd':
                        print(
                            '    {row}: Got comments: {comments}'.format(
                                row=i,
                                comments=soup.dl.contents[(i + 1)].string
                            )
                        )
                        bookmark.comment = soup.dl.contents[(i + 1)].string
                except IndexError:
                    # We've reached the end of the export file
                    pass
                bookmark.save()
                # Add the tags
                for tag in soup.dl.contents[i].a['tags'].split(','):
                    print(
                        '        {row}: Found tag {tag}'.format(
                            row=i, tag=tag
                        )
                    )
                    if tag != '':
                        try:
                            the_tag = Tag.objects.get(name=tag.strip(' '))
                        except Tag.DoesNotExist as e:
                            the_tag = Tag(name=tag.strip(' '))
                            the_tag.save()
                        bookmark.tags.add(the_tag)
Ejemplo n.º 26
0
class BookmarksUpdateTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.user = User.objects.create_user(username='******',
                                             email='*****@*****.**',
                                             password='******')

        self.bookmark = Bookmark(
            user=self.user,
            name='Bookmark 1',
            url='https://www.google.com',
            notes='This is the first note.',
        )

        self.bookmark.save()

    def test_edit_bookmark_uses_form_template(self):
        self.client.force_login(self.user)

        update_url = "/bookmarks/edit/%s" % self.bookmark.id
        response = self.client.get(update_url)

        self.assertEquals(response.status_code, 200)
        self.assertTemplateUsed(response, 'create_update.html')

    def test_cannot_update_bookmark_with_no_name(self):
        self.client.force_login(self.user)

        form_data = {
            'name': '',
            'url': 'https://www.google.com',
            'notes': '',
        }

        update_url = "/bookmarks/edit/%s" % self.bookmark.id
        response = self.client.post(update_url, data=form_data)
        self.assertContains(response, 'Please enter a name')

    def test_cannot_update_bookmark_with_no_url(self):
        self.client.force_login(self.user)

        form_data = {
            'name': 'Bookmark 1',
            'url': '',
            'notes': '',
        }

        update_url = "/bookmarks/edit/%s" % self.bookmark.id
        response = self.client.post(update_url, data=form_data)
        self.assertContains(response, 'Please enter a valid URL')

    def test_can_update_bookmark(self):
        bookmark = Bookmark(
            user=self.user,
            name='Bookmark 1',
            url='https://www.google.com',
            notes='This is the first note.',
        )

        bookmark.save()

        form_data = {
            'name': 'Bookmark 8',
            'url': 'https://www.google123.com',
            'notes': 'This is the first note.',
        }

        self.client.force_login(self.user)

        update_url = "/bookmarks/edit/%s" % bookmark.id
        response = self.client.post(update_url, data=form_data)

        self.assertEquals(response.status_code, 302)
        self.assertRedirects(response, '/bookmarks/')

        bookmark.refresh_from_db()

        self.assertEquals(form_data['name'], bookmark.name)
        self.assertEquals(form_data['url'], bookmark.url)
        self.assertEquals(form_data['notes'], bookmark.notes)
Ejemplo n.º 27
0
def save_data(request):
    
    data = request.GET.getlist('data[]')
    if data is None:
        return HttpResponseBadRequest()

    taskss = data[0]  
    del data[0]
    
    if not is_valid_url(taskss):
        return HttpResponse('<h1>Invalid URL!</h1>')
    
    
    if "http" not in taskss and "www" not in taskss:
        if "//" in taskss:
            return HttpResponse('<h2>// is present without http(or https) and www!</h2>')
        tasks = "http://www." + taskss
        
        
    elif "http" in taskss and "//" not in taskss and "www" in taskss:
        return HttpResponse('<h2>http(or https) and www are there but no "//"!</h2>')
    
    
    elif "http" not in taskss and "www" in taskss:
        if taskss[:3] == "www":
            tasks = "http://" + taskss
        else:
            return HttpResponse('<h2>Second elif ("www" not the First Characters)!</h2>')
        
        
    elif "http" in taskss and "www" not in taskss:
        try:
            taskss.index("/")
        except:
             return HttpResponse('<h2>http(or https) is there but www and // are not there!</h2>')
        k = taskss.index("/")
        tasks = taskss[:(k+2)] + "www." + taskss[(k+2):]
        
    else:
        tasks = taskss

    
    #loop for removing spaces from list data
    data = [x.strip(' ') for x in data]
    u_data = unique(data)

    conn = sqlite3.connect('db.sqlite3')
    c = conn.cursor()
    c.execute("SELECT * FROM bookmarks_bookmark WHERE task LIKE '%" + tasks + "%'")
    result = c.fetchall()


    if not result:
        searchInstance = Bookmark(task = tasks, tag_name = ",".join(u_data))
        searchInstance.save()
        return HttpResponse('<h1>Data Saved!</h1>')
    
    '''      
    c.execute("SELECT * FROM bookmarks_bookmark")
    url_result = c.fetchall()
       
    urls = [idx for idx,r in enumerate(url_result)]
    url = [r[2] for r in url_result]      
        
    for i in urls:
        if not is_valid_url(url[i]):
            Bookmark.objects.filter(task = url[i]).delete()
    '''
    
    tup = [r[1] for r in result]
    tags1 = ",".join(tup)                               #Converting list into string i.e. from ['abc, xyz'] to 'abc, xyz'
    tags2 = tags1.split(',')                            # 'abc, xyz' to ['abc', 'xyz']
        
    #u_d = tup + u_data
    u_d = union(tags2, u_data)

    Bookmark.objects.filter(task__contains = tasks).update(tag_name = ",".join(u_d))

    return HttpResponse('<h1>Data Saved!</h1>')
Ejemplo n.º 28
0
def unarchive_bookmark(bookmark: Bookmark):
    bookmark.is_archived = False
    bookmark.date_modified = timezone.now()
    bookmark.save()
    return bookmark