Saturday, June 19, 2010

Integrating hurry.resource into BFG

Back in PyCon APAC last week I was introduced to hurry.resource by its author, which is also one of the founder behind Grok, Martijn Faassen.

So, what does it do?.

Read Here

Lets go to the fun part

Setting Up

Assuming you are using one of the default BFG templates

First, enable ZCA. (or just do this in run.py):
...
config = Configurator(root_factory=get_root, settings=settings)
config.hook_zca()
config.begin()
...


Do this little modification on the get_root function in model.py:
from repoze.bfg.interfaces import IRequest

def get_root(request):
request.registry.registerUtility(request,provided=IRequest)
return root


Add some component codes, name the file resource.py
from hurry.resource.interfaces import ICurrentNeededInclusions
from hurry.resource.interfaces import ILibraryUrl
from zope.interface import implements,implementer
from zope.component import getUtility,adapter
from hurry.resource import NeededInclusions
from repoze.bfg.interfaces import IRequest
from hurry.resource.core import Library

class NeededInclusion(object):
implements(ICurrentNeededInclusions)

def __call__(self):
request = getUtility(IRequest)
if not hasattr(request,'needed'):
request.needed = NeededInclusions()
return request.needed

@implementer(ILibraryUrl)
@adapter(Library)
def LibraryUrl(library):
request = getUtility(IRequest)
return "%s/hurry/%s" % (request.application_url,library.name)


and hook them into ZCA by adding these into configure.zcml
<utility factory=".resource.NeededInclusion"/>
<adapter factory=".resource.LibraryUrl"/>


Adding into template
In your main template head section, add:
<tal:cond condition="getattr(request,'needed',None)">
<tal:res replace="structure request.needed.render()"/>
</tal:cond>


Using it

Lets take hurry.jquery for example:

First, register the static files, add these into configure.zcml
<static
name="hurry/jquery"
path="hurry.jquery:jquery-build"
/>


If you want to include Jquery js into your current view, just do this in the view code:
from hurry.jquery import jquery
def my_view(request):
jquery.need()
return {'project':'helloworld'}


If you did the stuff above correctly, with luck , your view should have this in place of the tal block.

<script type="text/javascript" 
src="http://localhost:6543/hurry/jquery/jquery-1.4.2.js"></script>


Custom Resource Libraries

You can create your own custom resource libraries and share/reuse it. To create your own resource, read the documentation of hurry.resource

Considering hurry.resource is also used in Grok and probably several other ZCA based frameworks, your resource library can also be reused on these frameworks with little to no modifications.

Credits to Martijn for this awesome component.

Happy hacking :D

Thursday, June 03, 2010

"They'll realize that they're different."

Quote from JDrama CHANGE. Episode 5


Keita Asakura

I'm sorry for being arrogant.
I used to be a grade school teacher.
Last year,
I was in charge of the fifth graders.
Anyway, they fight quite often.
But some of them become spiteful
and then sometimes it leads to bullying.
When those problems arise,
I would tell them,
"Let's think."
"You're in the same class,
so when there's something you don't like or you're not satisfied of, then tell them properly,
and listen to them properly."
"Then let's think thoroughly. Then..."

Gunji

They'll understand each other.

Keita Asakura

No.
They'll realize that they're different.

Since we think we're all alike,
we get mad when we get contradicted.
When someone acts different, we think "What the heck?"
That's when fights and bullying begin.
But no two people are alike.
Everyone thinks differently and has different situations.
That's why I wanted them to understand
that they're different from each other.
After that, think of what words to use so that their feelings will reach each other
and so that they can convince each other.



^.^


O mankind, We have created you male and female, and appointed you races and tribes, that you may know one another. Surely the noblest among you in the sight of God is the most righteous of you. God is All-knowing, All-aware. (49:13)