示例#1
0
    def add(self,
            short_url='',
            full_url='',
            description='',
            private='off',
            update=False,
            **kw):
        if update:
            require_access(self.app, 'update')
        else:
            require_access(self.app, 'create')
        if request.method == 'POST':
            if pylons.c.form_errors:
                error_msg = 'Error: '
                for msg in list(pylons.c.form_errors):
                    names = {'short_url': 'Short url', 'full_url': 'Full URL'}
                    error_msg += '%s: %s ' % (names[msg], c.form_errors[msg])
                    flash(error_msg, 'error')
                redirect(request.referer)

            shorturl = ShortUrl.query.find({
                'app_config_id': self.app.config._id,
                'short_name': short_url
            }).first()

            if shorturl is not None:
                if not update:
                    flash('Short url %s already exists' % short_url, 'error')
                    redirect(request.referer)
                else:
                    msg = ('update short url %s from %s to %s' %
                           (short_url, shorturl.full_url, full_url))
                    flash("Short url updated")

            else:
                shorturl = ShortUrl()
                shorturl.created = datetime.utcnow()
                shorturl.app_config_id = self.app.config._id
                msg = 'create short url %s for %s' % (short_url, full_url)
                flash("Short url created")

            shorturl.short_name = short_url
            shorturl.full_url = full_url
            shorturl.description = description
            shorturl.create_user = c.user._id
            shorturl.private = private == 'on'
            shorturl.last_updated = datetime.utcnow()

            M.AuditLog.log(msg)
            redirect(request.referer)
        return dict(app=self.app)
示例#2
0
文件: main.py 项目: heiths/allura
    def add(self, short_url='', full_url='', description='', private='off',
            update=False, **kw):
        if update:
            require_access(self.app, 'update')
        else:
            require_access(self.app, 'create')
        if request.method == 'POST':
            if c.form_errors:
                error_msg = 'Error: '
                for msg in list(c.form_errors):
                    names = {'short_url': 'Short url', 'full_url': 'Full URL'}
                    error_msg += '%s: %s ' % (names[msg], c.form_errors[msg])
                    flash(error_msg, 'error')
                redirect(request.referer)

            shorturl = ShortUrl.query.find({
                'app_config_id': self.app.config._id,
                'short_name': short_url}).first()

            if shorturl is not None:
                if not update:
                    flash('Short url %s already exists' % short_url, 'error')
                    redirect(request.referer)
                else:
                    msg = ('update short url %s from %s to %s'
                           % (short_url, shorturl.full_url, full_url))
                    flash("Short url updated")

            else:
                shorturl = ShortUrl()
                shorturl.created = datetime.utcnow()
                shorturl.app_config_id = self.app.config._id
                msg = 'create short url %s for %s' % (short_url, full_url)
                flash("Short url created")

            shorturl.short_name = short_url
            shorturl.full_url = full_url
            shorturl.description = description
            shorturl.create_user = c.user._id
            shorturl.private = private == 'on'
            shorturl.last_updated = datetime.utcnow()

            M.AuditLog.log(msg)
            redirect(request.referer)
        return dict(
            app=self.app,
            url_len=len(ShortUrl.build_short_url(c.app, short_name='')))
示例#3
0
    def add(self, short_url="", full_url="", description="", private="off", update=False, **kw):
        if update:
            require_access(self.app, "update")
        else:
            require_access(self.app, "create")
        if request.method == "POST":
            if c.form_errors:
                error_msg = "Error: "
                for msg in list(c.form_errors):
                    names = {"short_url": "Short url", "full_url": "Full URL"}
                    error_msg += "%s: %s " % (names[msg], c.form_errors[msg])
                    flash(error_msg, "error")
                redirect(request.referer)

            shorturl = ShortUrl.query.find({"app_config_id": self.app.config._id, "short_name": short_url}).first()

            if shorturl is not None:
                if not update:
                    flash("Short url %s already exists" % short_url, "error")
                    redirect(request.referer)
                else:
                    msg = "update short url %s from %s to %s" % (short_url, shorturl.full_url, full_url)
                    flash("Short url updated")

            else:
                shorturl = ShortUrl()
                shorturl.created = datetime.utcnow()
                shorturl.app_config_id = self.app.config._id
                msg = "create short url %s for %s" % (short_url, full_url)
                flash("Short url created")

            shorturl.short_name = short_url
            shorturl.full_url = full_url
            shorturl.description = description
            shorturl.create_user = c.user._id
            shorturl.private = private == "on"
            shorturl.last_updated = datetime.utcnow()

            M.AuditLog.log(msg)
            redirect(request.referer)
        return dict(app=self.app, url_len=len(ShortUrl.build_short_url(c.app, short_name="")))
示例#4
0
 def search(self, q=None, project=None, limit=None, page=0, **kw):
     c.search_results = W.search_results
     c.help_modal = W.search_help
     search_params = kw
     search_params.update(
         {"q": q or "", "project": project, "limit": limit, "page": page, "allowed_types": ["ShortUrl"]}
     )
     if not has_access(c.app, "view_private"):
         search_params["fq"] = ["private_b:False"]
     d = search_app(**search_params)
     d["search_comments_disable"] = True
     d["search_history_disable"] = True
     d["url_len"] = len(ShortUrl.build_short_url(c.app, short_name=""))
     return d
示例#5
0
 def search(self, q=None, project=None, limit=None, page=0, **kw):
     c.search_results = W.search_results
     c.help_modal = W.search_help
     search_params = kw
     search_params.update({
         'q': q or '',
         'project': project,
         'limit': limit,
         'page': page,
         'allowed_types': ['ShortUrl'],
     })
     if not has_access(c.app, 'view_private'):
         search_params['fq'] = ['private_b:False']
     d = search_app(**search_params)
     d['search_comments_disable'] = True
     d['search_history_disable'] = True
     d['url_len'] = len(ShortUrl.build_short_url(c.app, short_name=''))
     return d
示例#6
0
    def index(self, page=0, limit=None, **kw):
        c.page_list = W.page_list
        c.page_size = W.page_size
        limit, pagenum, start = g.handle_paging(limit, page, default=100)
        p = {'app_config_id': c.app.config._id}
        if not has_access(c.app, 'view_private'):
            p['private'] = False
        short_urls = (ShortUrl.query.find(p))
        count = short_urls.count()

        short_urls = short_urls.skip(start).limit(limit)

        return {
            'short_urls': short_urls,
            'limit': limit,
            'pagenum': pagenum,
            'count': count,
            'url_len': len(ShortUrl.build_short_url(c.app, short_name='')),
        }
示例#7
0
    def index(self, page=0, limit=100, **kw):
        c.page_list = W.page_list
        c.page_size = W.page_size
        limit, pagenum, start = g.handle_paging(limit, page, default=100)
        p = {"app_config_id": c.app.config._id}
        if not has_access(c.app, "view_private"):
            p["private"] = False
        short_urls = ShortUrl.query.find(p)
        count = short_urls.count()

        short_urls = short_urls.skip(start).limit(limit)

        return {
            "short_urls": short_urls,
            "limit": limit,
            "pagenum": pagenum,
            "count": count,
            "url_len": len(ShortUrl.build_short_url(c.app, short_name="")),
        }
示例#8
0
    def add(self, short_url="",
            full_url="",
            description="",
            private="off", **kw):
        if (request.method == 'POST'):
            if pylons.c.form_errors:
                error_msg = "Error creating Short URL: "
                for msg in list(pylons.c.form_errors):
                    names = {"short_url": "Short name", "full_url": "Full URL"}
                    error_msg += "%s - %s " % (names[msg], c.form_errors[msg])
                    flash(error_msg, "error")
                redirect(request.referer)

            if (short_url != full_url):
                shorturl = ShortUrl.query.find({
                    'app_config_id': self.app.config._id,
                    'short_name': short_url}).first()
                if shorturl is None:
                    shorturl = ShortUrl()
                    shorturl.created = datetime.utcnow()
                    log_msg = 'create short url %s for %s' %\
                              (short_url,
                               full_url)
                else:
                    log_msg = 'update short url %s from %s to %s' %\
                              (short_url,
                               shorturl.full_url,
                               full_url)
                shorturl.full_url = full_url
                shorturl.short_name = short_url
                shorturl.description = description
                shorturl.create_user = c.user._id
                shorturl.app_config_id = self.app.config._id
                if private == "on":
                    shorturl.private = True
                else:
                    shorturl.private = False
                shorturl.last_updated = datetime.utcnow()
                M.AuditLog.log(log_msg)
                flash("Short url created")
            else:
                flash("Error creating Short URL: "
                      "Short Name and Full URL must be different", "error")
            redirect(request.referer)
        return dict(app=self.app)