Exemple #1
0
def edit_error(request):
    rdict = request.matchdict
    params = request.params
    post = request.POST

    with ReqAuthorize(request, username=rdict['username']):
        if 'new' in request.url:
            BmarkMgr.store(post['url'], request.user.username,
                           post['description'], post['extended'], post['tags'])

        else:
            if 'hash_id' in rdict:
                hash_id = rdict['hash_id']
            elif 'hash_id' in params:
                hash_id = params['hash_id']

            bmark = BmarkMgr.get_by_hash(hash_id, request.user.username)
            if bmark is None:
                return HTTPNotFound()

            bmark.fromdict(post)
            bmark.update_tags(post['tags'])

        # if this is a new bookmark from a url, offer to go back to that url
        # for the user.
        if 'go_back' in params and params['comes_from'] != "":
            return HTTPFound(location=params['comes_from'])
        else:
            return HTTPFound(location=request.route_url(
                'user_bmark_recent', username=request.user.username))
Exemple #2
0
def edit_error(request):
    rdict = request.matchdict
    params = request.params
    post = request.POST

    with ReqAuthorize(request, username=rdict['username']):
        if 'new' in request.url:
            try:
                bmark = BmarkMgr.store(post['url'], request.user.username,
                                       post['description'], post['extended'],
                                       post['tags'])

                # Assign a task to fetch this pages content and parse it out
                # for storage and indexing.
                DBSession.flush()
                tasks.fetch_bmark_content.delay(bmark.bid)

            except InvalidBookmark, exc:
                # There was an issue using the supplied data to create a new
                # bookmark. Send the data back to the user with the error
                # message.
                bmark = Bmark(post['url'],
                              request.user.username,
                              desc=post['description'],
                              ext=post['extended'],
                              tags=post['tags'])

                return {
                    'new': True,
                    'bmark': bmark,
                    'message': exc.message,
                    'user': request.user,
                }

        else:
Exemple #3
0
def edit(request):
    """Manual add a bookmark to the user account

    Can pass in params (say from a magic bookmarklet later)
    url
    description
    extended
    tags

    """
    rdict = request.matchdict
    params = request.params
    new = False

    with ReqAuthorize(request, username=rdict['username']):

        if 'hash_id' in rdict:
            hash_id = rdict['hash_id']
        elif 'hash_id' in params:
            hash_id = params['hash_id']
        else:
            hash_id = None

        if hash_id:
            bmark = BmarkMgr.get_by_hash(hash_id, request.user.username)

            if bmark is None:
                return HTTPNotFound()
        else:
            # hash the url and make sure that it doesn't exist
            url = params.get('url', u"")
            if url != u"":
                new_url_hash = generate_hash(url)

                test_exists = BmarkMgr.get_by_hash(new_url_hash,
                                                   request.user.username)

                if test_exists:
                    location = request.route_url(
                        'user_bmark_edit',
                        hash_id=new_url_hash,
                        username=request.user.username)
                    return HTTPFound(location)

            new = True
            desc = params.get('description', None)
            bmark = Bmark(url, request.user.username, desc=desc)

        tag_suggest = TagMgr.suggestions(
            url=bmark.hashed.url,
            username=request.user.username
        )

        return {
            'new': new,
            'bmark': bmark,
            'user': request.user,
            'tag_suggest': tag_suggest,
        }
Exemple #4
0
    def import_bmarks(self):
        """Allow users to upload a bookmark export file for processing"""
        username = self.matchdict.get('username')

        # if auth fails, it'll raise an HTTPForbidden exception
        with ReqAuthorize(self.request):
            data = {}
            post = self.POST

            # We can't let them submit multiple times, check if this user has
            # an import in process.
            if ImportQueueMgr.get(username=username, status=NEW):
                # They have an import, get the information about it and shoot
                # to the template.
                return {
                    'existing': True,
                    'import_stats': ImportQueueMgr.get_details(
                        username=username)
                }

            if post:
                # we have some posted values
                files = post.get('import_file', None)

                if files is not None:
                    storage_dir_tpl = self.settings.get('import_files',
                                                        '/tmp/bookie')
                    storage_dir = storage_dir_tpl.format(
                        here=self.settings.get('app_root'))

                    out_fname = store_import_file(storage_dir, username, files)

                    # Mark the system that there's a pending import that needs
                    # to be completed
                    q = ImportQueue(username, unicode(out_fname))
                    DBSession.add(q)
                    DBSession.flush()
                    # Schedule a task to start this import job.
                    tasks.importer_process.delay(q.id)

                    return HTTPFound(
                        location=self.request.route_url('user_import',
                                                        username=username))
                else:
                    msg = self.request.session.pop_flash()
                    if msg:
                        data['error'] = msg
                    else:
                        data['error'] = None

                return data
            else:
                # we need to see if they've got
                # just display the form
                return {
                    'existing': False
                }
Exemple #5
0
def account(request):
    """Index of account page

    You can only load your own account page. If you try to view someone else's
    you'll just end up with your own.

    """
    # if auth fails, it'll raise an HTTPForbidden exception
    with ReqAuthorize(request):
        user = UserMgr.get(username=request.user.username)

        return {
            'user': user,
            'username': user.username,
        }
Exemple #6
0
def import_bmarks(request):
    """Allow users to upload a delicious bookmark export"""
    rdict = request.matchdict
    username = rdict.get('username')

    # if auth fails, it'll raise an HTTPForbidden exception
    with ReqAuthorize(request):
        data = {}
        post = request.POST

        # we can't let them submit multiple times, check if this user has an
        # import in process
        if ImportQueueMgr.get(username=username, status=NEW):
            # they have an import, get the information about it and shoot to
            # the template
            return {
                'existing': True,
                'import_stats': ImportQueueMgr.get_details(username=username)
            }

        if post:
            # we have some posted values
            files = post.get('import_file', None)

            if files is not None:
                # save the file off to the temp storage
                out_dir = "{storage_dir}/{randdir}".format(
                    storage_dir=request.registry.settings.get(
                        'import_files',
                        '/tmp/bookie').format(
                            here=request.registry.settings.get('app_root')),
                    randdir=random.choice(string.letters),
                )

                # make sure the directory exists
                # we create it with parents as well just in case
                if not os.path.isdir(out_dir):
                    os.makedirs(out_dir)

                out_fname = "{0}/{1}.{2}".format(
                    out_dir, username, files.filename)
                out = open(out_fname, 'w')
                out.write(files.file.read())
                out.close()

                # mark the system that there's a pending import that needs to
                # be completed
                q = ImportQueue(username, out_fname)
                DBSession.add(q)
                DBSession.flush()
                # Schedule a task to start this import job.
                tasks.importer_process.delay(q.id)

                return HTTPFound(location=request.route_url('user_import',
                                                            username=username))
            else:
                msg = request.session.pop_flash()
                if msg:
                    data['error'] = msg
                else:
                    data['error'] = None

            return data
        else:
            # we need to see if they've got
            # just display the form
            return {
                'existing': False
            }