Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (56 sloc) 2.55 KB
<moya xmlns="http://moyaproject.com"
xmlns:moya="http://moyaproject.com"
xmlns:db="http://moyaproject.com/db"
xmlns:let="http://moyaproject.com/let"
xmlns:w="http://moyaproject.com/widgets"
xmlns:forms="http://moyaproject.com/forms">
<model libname="ShortURL" name="shorturl" xmlns="http://moyaproject.com/db"
repr="shorturl ${id}">
<string name="url" length="300" />
<integer name="visitcount" default="0"/>
</model>
<mountpoint name="main">
<url route="/" view="#view.front" />
<url route="/{urlkey}" view="#view.redirect" name="redirect" />
</mountpoint>
<form libname="form.create_shorturl" xmlns="http://moyaproject.com/forms" style="horizontal" legend="URL Shortener">
<input name="url" maxlength="300" required="yes" label="URL" class="input-xlarge"/>
<submit-button class="btn-primary" name="shorten" text="Shorten URL"/>
<validate-field field="url">
<fail if="len:value gt 300">URL must be no more than 300 characters</fail>
<fail if="not (value ^= 'http://' or value ^='https://')">
URL must start with http:// or https://
</fail>
</validate-field>
</form>
<view libname="view.front" content="#content.front" requires="permission:.app.settings.add_url_permission">
<forms:get form="#form.create_shorturl" dst="form" />
<forms:validate src="form">
<db:get-or-create model="#ShortURL" let:url="form.data.url" dst="shorturl"/>
<forms:reset src="form"/>
</forms:validate>
</view>
<view libname="view.redirect">
<db:get-required model="#ShortURL" let:id="url.urlkey | 'decode_id'" dst="shorturl"/>
<inc dst="shorturl.visitcount" />
<redirect-to url="${shorturl.url}" />
</view>
<content libname="content.front">
<section name="body">
<if test="shorturl">
<let urlkey="shorturl.id | 'encode_id'" />
<str dst="fullurl">${.request.host_url}${.urls.shorturl.redirect}</str>
<w:success>
Your URL has been shortened from ${len:shorturl.url}
to ${len:fullurl} characters,
making it
${100 - int:((len:fullurl / len:shorturl.url) * 100.0)}% shorter!
</w:success>
<w:well>
<w:url url="${fullurl}"/> &#8594; ${shorturl.url}
</w:well>
</if>
<render src="form"/>
</section>
</content>
</moya>