You guys all rock hah.
I got the form working as I want it to. I'm gonna try putting in the
compute argument in my model tomorrow but for now I have the form working
with the following code:
new controller:
moment.py:
import re
def form():
## TODO: Get db.moment.type set to best or worst depending
## on frequency of word used. (this is best in controller or view?)
form = SQLFORM(db.moment)
try:
db.moment.user_id.default = auth.user.id
except:
pass
if 'type' in request.vars:
bestCount = re.findall('best', request.vars.content.lower())
bestCount = bestCount.count('best')
worstCount = re.findall('worst', request.vars.content.lower())
worstCount = worstCount.count('worst')
if bestCount > worstCount:
request.vars.type = 'best'
elif worstCount > bestCount:
request.vars.type = 'worst'
if form.accepts(request.vars, formname='moment_submit_form'):
response.flash = 'form accepted'
elif form.errors:
response.flash = 'form has errors'
return dict(form=form)
new view,
views/moment/form.html
{{if auth.is_logged_in():}}
<form method="post" id="submitMoment">
<textarea class="text" cols="40" id="moment_content" name="content"
rows="10"></textarea>
<input type="submit" value="Submit">
<input type="hidden" id="submitMomentType" name="type" value="" />
<input type="hidden" name="_formname" value="moment_submit_form" />
</form>
{{else:}}
Sign in to play!
{{pass}}
and then put in my
views/layout.html to show the form:
{{=LOAD('moment', 'form.html')}}
One thing that's no longer working is flashing the error or accept messages,
but at least functionally it's working. I don't think it's the prettiest
code but let me know if you have any suggestions please!
On Wed, Dec 1, 2010 at 10:27 PM, DenesL <[email protected]> wrote:
>
> True, but you gotta learn to walk before you run ;)
>
>
> On Dec 1, 9:48 pm, "mr.freeze" <[email protected]> wrote:
> > I would personally use a computed field for this:
> >
> > Model:
> > def count_words(txt):
> > ...
> > return count
> >
> > def get_type(txt):
> > count = count_words(txt)
> > if count < 10: return 'small'
> > if count < 20: return 'medium'
> > if count < 30: return 'large'
> > return 'huge'
> >
> > db.define_table('moment',
> > Field('textarea','text'),
> > Field('txttype', readable=False, writable=False,
> > compute=lambda r: get_type(r['textarea'])),
> > ...
> > )
> >
> > Controller:
> > def submit_moment():
> > form = crud.create(db.moment)
> > return dict(form=form)
> >
> > On Dec 1, 8:09 pm, DenesL <[email protected]> wrote:
> >
> > > your_model.py:
> >
> > > db.define_table('moment',
> > > Field('textarea','text'),
> > > Field('txttype', readable=False, writable=False),
> > > ...
> > > )
> >
> > > default.py:
> >
> > > def moment_type(form):
> > > form.vars.txttype=...
> >
> > > def submit_moment():
> > > form=SQLFORM(db.moment)
> > > if form accepts(request.vars, session, onvalidation=moment_type):
> > > response.flash = 'form accepted'
> > > elif form.errors:
> > > ...
> > > return dict(form=form, ...)
> >
> > > views/default/submit_moment.html:
> >
> > > {{extend 'layout.html'}}
> > > {{=form}}
> >
> > > On Dec 1, 11:47 am, hswolff <[email protected]> wrote:
> >
> > > > I'll try the model implementation. I need to be able to parse the
> > > > submit content and count the frequency a word appears and set the
> type
> > > > accordingly.
> >
> > > > I'm using response.moment_form because when I had it in the index()
> > > > function and I would go to the user page it wouldn't render because
> > > > the dict wouldn't return the form that was displayed on views/
> > > > layout.html.
> >
> > > > Correct me if I'm wrong: if I were to make a submit_moment():
> > > > function and a views/default/submit_moment.html view I would be able
> > > > to include the view on every page and have it be functionally
> > > > accurate, right?
> >
> > > > I'm still getting the hang of web2py's MVC.
> >
> >
>