Tasklist Application

Hi Guys!

So, far you have learnt how to use the Brainwave Platform to create simple applications. Now we are going to look how the "Poseidon" database works and how data is being stored and retrieved from there.

For this we are going to create an application called "Tasklist". In which a user will be able to assign a task to any user including him also, and will be able to view all the tasks assigned to him. He can also delete any task from his tasklist.

Guide

Step 1: Install the Brainwave Platform in your computer. Double click on the executable file and follow the instructions.

Step 2: Install Python 2.5, available from http://www.python.org/download/releases/2.5/Python-2.5.msi. Download it and Install it.

Step 3: Download and install your favorite programmer's text editor. We use the free Notepad++ which can be downloaded from http://prdownloads.sourceforge.net/notepad-plus/npp.3.9.Installer.exe?use_mirror=umn.

Step 4: The Brainwave Platform requires Firefox 1.5 or later. Firefox can be be downloaded from http://www.mozilla.com/en-US/products/download.html?product=firefox-2.0&os=win&lang=en-USDownload & Install Firefox.

Before creating the application you need to understand the "Poseidon Database Structure". For this please refer "Poseidon Documentation".

In our application the structure will look something like this.

In the structure you can see we are creating two sets of links
First is ('user','tasklist','user_tasklist')

Second is ('user_tasklist','task','tasks')

First link will be created during the initialization of the application with p.setContext as "sys_user".
Second will be created by the default p.setContext

To create the application click on the Brainwave icon on the desktop
Or
Go to Start -> Programs -> Brainwave -> Start Brainwave.
The brainwave home page will be displayed. Click on the Application Builder icon.

The logon screen will appear. As the Brainwave Platform comes with a built-in authentication feature, you have to be logged in to view any application or to use the platform.

Log in as 'root' & password is 'password', as root has the super user permissions.

It will open the application builder application.

Enter 'Tasklist' as the application name or any name you want to give. Now follow the tutorial on the right side of the screen.

Go to the created Tasklist folder 'C:/Program Files/Brainwave/Apps/Tasklist', there you will see a file Tasklist.py. Open it in Notepad++.

We need to create the first link for every user. So, we are going to write the following code in the def __init__(self) method

def __init__(self):
		
		iris.App.__init__(self)
		self.aphrodite = aphrodite.Aphrodite('tasklist')
		
		p=self.findObject('poseidon')
		
		p.setContext('sys_user','sys_user')
		
		Idea.setPoseidon(p)
		
		users = Idea.findAll('user')
		tasklist_verb = self.createNamespace(p, "tasklist.tasklist", save=False)['o']
		for x in users:
			
			user_id = x.get_id()
			user_list = x.findValues('tasklist')
			user_name = x.get_name()
			
			if len(user_list)<1:
				
				
				p.addCapability(user_id, 'read', user_list_id, save=False)
				p.addCapability(user_id, 'write', user_list_id, save=False)
				p.addCapability(user_id, 'link', user_list_id, save=False)
				
				p.setPolicy(user_list_id,'write','True',save=False)
				p.addLink(user_id, tasklist_verb, user_list_id)
				p.save()

So, far we have created the First Link for any user.

Now we are going to define the index method. Here we are trying to find out, if any tasks were there in the database for the current user. If yes, then it will print them on the screen.

@iris.expose
	def index(self, *args, **kwargs):
		p=self.findObject('poseidon')
		users = Idea.findAll('user')
		user_tasks = Idea.get(iris.session['uid']).findValues("tasklist")
		tasks = Idea.get(user_tasks[0]['__id']).findValues("task")
		return CheetahRender(data={'users':users,'tasks':tasks},template="index")

Now we are going to create the interface of the application i.e the Index template file. Write the following code into the "index.tmpl" file

#filter WebSafe
<window>
	<div style="width:100%;text-align:center;margin:0 auto;">
		<p>
		<div class="box" style="width:700px;height:350px;margin:0 auto;">		
			<div class="boxtitle" style="text-align:center;font:25 Times New Roman"><b>Tasklist</b></div>
				<p/>
				<table>
					<tr>
						<td width="200" align="center">
							<div style="border:1px solid #ccc;font:16 Times New Roman"><b>Tasks</b></div>
						</td>
						<td width="500" align = "center">
							<div style="border:1px solid #ccc;font:16 Times New Roman"><b>Create and Assign Task</b></div>
						</td>
					</tr>
				
					<tr>
						<td width="250">
							<div id="tasklist" style="border:1px solid #ccc;height:250px;padding-left:10px;overflow:auto">
								<ol>
								#for $task in $tasks
								<li id="${task.get_id()}">$task.get_name()
									<button image="del" update="tasklist" url="/tasklist/remove/]]><![CDATA[$]]><![CDATA[{task.get_id()}" />
								</li>
	
								</ol>
								</div>
						</td>
						<td width="350">
							<div style="border:1px solid #ccc;height:250px;padding-left:10px"><p/>
								<ajaxform id="createtask" url="/tasklist/create" method="get" update="tasklist">
									<table>
										<tr>
											<td><input type="text" name="task" id='task_box' style="width:225px;"/></td>
											<td><select name="user" style="width:150px; height:21px;">]]>
											<#><for> <$><users in> <$><users>
											<<option value='><$><!users.__id'>><$><users.__name</option>>
											<#><end for

											
											</select></td>
											<td><button image="go" submit="createtask" url="/tasklist/create" update="task_box=''"/></td>
										</tr>
										<tr align="center">
											<td><label>Task</label></td>
											<td><label>Assigned to</label></td>
										</tr>
									</table>
								</ajaxform>
							<br/><p/>
							</div>
						</td>
					</tr>
					
				</table>	
			</div>
		</p>
	</div>
</window>



The interface will look this.

Now we are going to create the 'create method' for creating the tasks.

@iris.expose
	def create(self, *args, **kwargs):
		p=self.findObject('poseidon')
		del kwargs['_']
		
		u_def_task= p.addMeme(kwargs['task'])[0].id
		
		if p.getNeuron('task') is None:
			ver = p.addMeme('task')[0].id
			p.setPolicy(ver,'write','True',save=False)
			p.save()
		else:
			ver = p.getNeuron('task')[0].id
			
		user_tasklist = Idea.get(kwargs['user']).findValues("tasklist")
		
		p.addLink(user_tasklist[0]['__id'],ver,u_def_task)
		p.addLink(u_def_task,p.addMeme('is a')[0].id,ver)
		p.save()
		
		user_tasks = Idea.get(iris.session['uid']).findValues("tasklist")
		tasks = Idea.get(user_tasks[0]['__id']).findValues("task")
		return CheetahRender(data={'tasks':tasks},template="tasklist")

To view the tasks create the tasklist.tmpl and put the following code there.

#filter WebSafe
<ol>
	#for $task in $tasks
		<li>$task
			<button image="del" />
		</li>
	#end for
</ol>

Here we display the tasks along with an image button to delete the task.

Now lets create a task for the current user himself.

Once we are able to create tasks, let's create another method to delete the tasks.

@iris.expose
	def remove(self, *args, **kwargs):
		p=self.findObject('poseidon')
		
		user_tasklist = Idea.get(iris.session['uid']).findValues('tasklist')
				
		pol=p.addLink(user_tasklist[0]['__id'],p.getNeuron('task')[0].id,args[0])
		
		p.removeLink(pol.id)
		
		user_tasks = Idea.get(iris.session['uid']).findValues("tasklist")
		
		tasks = Idea.get(user_tasks[0]['__id']).findValues("task")
		
		return CheetahRender(data={'tasks':tasks},template="tasklist")