alright, thank you for your help. I've broken it down to its most basic
level and its damn working. I'll post the essentials here. remember this
is for a class to teach them the basics of programming. yeah, fun
students...
under the DAL I setup a simple table to store the code:
NE = IS_NOT_EMPTY()
db.define_table('code',
Field('input_datetime', 'datetime', writable=False, readable=False,
requires=NE, default=datetime.datetime.now()),
Field('modified_datetime', 'datetime', writable=False, readable=False,
requires=NE, default=datetime.datetime.now()),
Field('user_id', db.auth_user, requires=IS_IN_DB(db, db.auth_user.id,
'%(last_name)s,
%(first_name)s (%(id)s)'), default=(auth.user.id if auth.is_logged_in() else
None), label='User', writable=False, readable=False),
Field('title', length="36", requires=NE, comment="title of the project
code"),
Field('description', length="256", requires=NE, comment="description of
the project code"),
Field('source_code', 'text'),
Field('share_code', 'boolean', default=('T' if auth.has_membership(
'Administrator') else 'F')),
format = '%(input_datetime)s')
under the controller, I have two functions:
@auth.requires_login()
def project():
response.title = T("Python Project")
pid = mcp.request_to_int(request.args[0], None)
if (pid is None):
pid = db.code.insert(share_code=(True if auth.has_membership(
'Administrator') else False))
redirect(URL(c="default", f="project", args=[pid]))
else:
sCode = db(db.code.id==pid).select().first()
if (sCode.user_id <> auth.user.id):
return dict(form="Error: User can only edit their own code.")
frm = SQLFORM(db.code, record=pid, showid=True)
"""
if auth.has_membership('Administrator'):
e = frm.element('input#code_share_code')
e.update(_checked="checked")
"""
return dict(form=frm)
@auth.requires_login()
def ajaxSaveProject():
#return "%s" % request.vars
id = mcp.request_to_int(request.vars.id, None)
title = request.vars.title
description = request.vars.description
source_code = request.vars.source_code.strip()
share_code = mcp.request_to_boolean(request.vars.share_code)
#return "%s" % locals()
try:
modified_datetime = datetime.datetime.now()
db(db.code.id==id).update(**locals())
except:
return False
return response.json(locals())
AND, finally, under the view, which is the most important:
{{extend 'layout.html'}}
{{block head}}
<link rel="stylesheet" href="/admin/static/codemirror/lib/codemirror.css" />
<script src="/admin/static/codemirror/lib/codemirror.js"></script>
<script src="/admin/static/codemirror/mode/python/python.js"></script>
<style>
div#form { border: 0px solid red; background-color: #fff; }
table { width: 100%; }
td { margin: 0px; padding: 0px; line-height: 24pt; border: 0px solid
gray; }
input[type=text] { margin: 1px 0px; padding: 1px 4px; }
input[name=title] { max-width: 160px; }
input[type=checkbox] { position: relative; margin: 2px; padding: 0px; }
button#save { height: 20px; font-size: 10pt; font-weight: bold;
line-height: 6px; }
</style>
<script>
var editor;
jQuery(function() {
jQuery('button#save').click(function(obj) {
obj.preventDefault();
console.log('button clicked');
doClickSave();
});
});
function doClickSave() {
var data = editor.getValue();
//console.log(data);
jQuery.post("{{=URL(c="default", f="ajaxSaveProject")}}", { 'id':jQuery(
'p#pid').html(), 'title':jQuery('input#code_title').val(), 'description':
jQuery('input#code_description').val(), 'source_code':data, 'share_code':
jQuery('input#code_share_code').prop('checked')}).done(function(t) {
console.log(t);
jQuery('td#msg').html('saved: '+t.modified_datetime);
});
}
jQuery(document).ready(function() {
console.log('('+jQuery(window).width()+','+jQuery(window).height()+')
jQuery.version: '+jQuery.fn.jquery);
//console.log(jQuery('nav').height()+' ___ '+jQuery('footer').height());
editor = CodeMirror.fromTextArea(document.getElementById("code_i"), {
lineNumbers: true,
mode: { name: 'python', version: 2, singleLineStringErrors: false,
},
indentUnit: 4,
indentWithTabs: false,
tabSize: 4,
lineWrapping: true
});
CodeMirror.commands.save = function() { doClickSave(); };
//console.log(jQuery(window).height()+" ___
"+jQuery('div#form').offset().top+" ___ "+jQuery('div#form').height()+" ___
"+jQuery('footer').height());
editor.setSize(jQuery(window).width() - 36, jQuery(window).height() - (
jQuery('div#form').offset().top + jQuery('div#form').height()) - jQuery(
'footer').height() - 12);
});
</script>
{{end}}
{{=form.custom.begin}}
<div id="form">
<table>
<tr><td colspan="5" id="msg" style="text-align: center; color:maroon;
font-weight:bold;"></td></tr>
<tr><td class="right">Project Title:</td><td>{{=form.custom.widget.title
}}</td><td class="right">Share Code?</td><td>{{=form.custom.widget.
share_code}}</td><td><button id="save">Save All</button></td></tr>
<tr><td class="right">Description:</td><td colspan="4">{{=form.custom.
widget.description}}</td></tr>
</table>
</div>
<p id="pid" style="display: none;">{{=form.custom.dspval.id}}</p>
<textarea id="code_i" style="display:none;">{{=form.custom.dspval.
source_code.strip() if (form.custom.dspval.source_code is not None) else "#
-*- coding: utf-8 -*-\nimport mcp\n\ndef index():\n\treturn BODY(\"testing
1 2 3\")"}}</textarea>
{{=form.custom.end}}
and it is working really nicely. thank you for the help and suggestions
and guidance. I hope the code above will help some other professors show
the importance and realities of programming in our modern world.
Lucas
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.