示例#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 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='')))
示例#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", **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)
示例#4
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="")))