Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
[closes #1] merged upstream/master
Browse files Browse the repository at this point in the history
  • Loading branch information
alaz committed Jan 17, 2011
2 parents 5b1162c + 0302e90 commit 10aaf01
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 103 deletions.
2 changes: 1 addition & 1 deletion PocketChange/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
document-oriented database for storage.</description>
<properties>
<scala.version>2.8.1</scala.version>
<lift.version>2.2-RC6</lift.version>
<lift.version>2.2</lift.version>
</properties>

<pluginRepositories>
Expand Down
11 changes: 11 additions & 0 deletions PocketChange/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%t] %-5p %logger{25} - %m%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
13 changes: 0 additions & 13 deletions PocketChange/src/main/resources/props/default.log4j.xml

This file was deleted.

139 changes: 80 additions & 59 deletions PocketChange/src/main/scala/com/pocketchangeapp/snippet/AddEntry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.xml.{NodeSeq,Text}
import net.liftweb.common.{Box,Empty,Full,Logger}
import net.liftweb.http.{FileParamHolder,S,SHtml,StatefulSnippet}
import org.slf4j.LoggerFactory
import java.text.ParseException

// Import "bind", as well as the implicits that make bind easy
import net.liftweb.util.Helpers._
Expand Down Expand Up @@ -37,73 +38,93 @@ class AddEntry extends StatefulSnippet with Logger {
case Full(user) if user.editable.size > 0 => {
def doTagsAndSubmit(t: String) {
tags = t
if (tags.trim.length == 0) {
error("Cannot add an entry without tags")
S.error("We're going to need at least one tag.")

// Pre-validate the unparsed values
val requiredFields = List((date, "a date"), (desc, "a description"), (value, "a value"), (tags, "tags"))

val missing = requiredFields.flatMap { case (field,label) =>
if (field.trim.length == 0) {
Some(label)
} else {
None
}
}

if (! missing.isEmpty) {
missing.foreach { label =>
error("Entry add attempted without " + label)
S.error("You must provide " + label)
}
} else {
/* Get the date correctly, add the datepicker: comes in as yyyy/mm/dd */
val entryDate = Util.slashDate.parse(date)

val amount = Expense.amount.fromString(value)

// Rework to not throw exceptions
val currentAccount = Account.byId(account).get

// We need to determine the last serial number and balance for the date in question
val (entrySerial,entryBalance) = Expense.getLastExpenseData(currentAccount, entryDate)

val e = new Expense(currentAccount)
e.dateOf = entryDate
e.serialNumber = entrySerial + 1
e.description = desc
e.amount = amount
e.tags(tags)
e.currentBalance = entryBalance + amount

// Add the optional receipt if it's the correct type
val receiptOk = fileHolder match {
case Full(FileParamHolder(_, null, _, _)) => true
case Full(FileParamHolder(_, mime, fileName, data))
if mime.startsWith("image/") => {
e.uploadReceipt(mime, fileName, data)
true
}
// If someone sends nothing...
case Full(FileParamHolder(_, _, "", _)) => true
case Full(something) => {
error("Received invalid file attachment: " + something)
S.error("Invalid receipt attachment")
false
try {
/* Get the date correctly, add the datepicker: comes in as yyyy/mm/dd */
val entryDate = Util.slashDate.parse(date)

val amount = Expense.amount.fromString(value)

// Rework to not throw exceptions
val currentAccount = Account.byId(account).get

// We need to determine the last serial number and balance for the date in question
val (entrySerial,entryBalance) = Expense.getLastExpenseData(currentAccount, entryDate)

val e = new Expense(currentAccount)
e.dateOf = entryDate
e.serialNumber = entrySerial + 1
e.description = desc
e.amount = amount
e.tags(tags)
e.currentBalance = entryBalance + amount

// Add the optional receipt if it's the correct type
val receiptOk = fileHolder match {
case Full(FileParamHolder(_, null, _, _)) => true
case Full(FileParamHolder(_, mime, fileName, data)) if mime.startsWith("image/") => {
e.uploadReceipt(mime, fileName, data)
true
}
// If someone sends nothing...
case Full(FileParamHolder(_, _, "", _)) => true
case Full(something) => {
error("Received invalid file attachment: " + something)
S.error("Invalid receipt attachment")
false
}
case _ => true
}
case _ => true
}

(Expense.validate(e),receiptOk) match {
case (Nil,true) => {
Expense.updateEntries(entrySerial + 1, amount)
Expense save e

val newBalance = currentAccount.balance + e.amount
currentAccount.balance = newBalance
Account save currentAccount
S.notice("Entry added!")
this.unregisterThisSnippet() // dpp: remove the statefullness of this snippet
}
case (x,_) => {
error(x)
S.error(x)
(Expense.validate(e),receiptOk) match {
case (Nil,true) => {
Expense.updateEntries(entrySerial + 1, amount)
Expense save e

val newBalance = currentAccount.balance + e.amount
currentAccount.balance = newBalance
Account save currentAccount
S.notice("Entry added!")
this.unregisterThisSnippet() // dpp: remove the statefullness of this snippet
S.redirectTo("/")
}
case (x,_) => {
error(x)
S.error(x)
}
}
}
} catch {
case pe : ParseException => error(pe); S.error("Invalid date: " + date)
case nfe : NumberFormatException => error(nfe); S.error("Invalid value: " + value)
}
}
}

bind("e", in,
"account" -> SHtml.select(user.editable.map(acct => (acct.id, acct.name)).toSeq, Empty, oid => account = oid),
"dateOf" -> SHtml.text("", date = _) % ("size" -> "10"),
"desc" -> SHtml.text("", desc = _),
"value" -> SHtml.text("", value = _),
"receipt" -> SHtml.fileUpload(fph => fileHolder = Full(fph)),
"tags" -> SHtml.text(tags, doTagsAndSubmit))
"account" -%> SHtml.select(user.editable.map(acct => (acct.id, acct.name)).toSeq, Empty, oid => account = oid),
// Note that we use "-%>" so that the id and maxlength attrs on the template are preserved
"dateOf" -%> SHtml.text("", date = _),
"desc" -%> SHtml.text("", desc = _),
"value" -%> SHtml.text("", value = _),
"receipt" -%> SHtml.fileUpload(fph => fileHolder = Full(fph)),
"tags" -%> SHtml.text(tags, doTagsAndSubmit))
}
case _ => Text("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import scala.xml.{NodeSeq,Text}

import model.User
import net.liftweb.common.{Logger, Full}
import net.liftweb.http.DispatchSnippet

// Import Helper's implicits for binding
import net.liftweb.util.Helpers._
import net.liftweb.util.BindHelpers._

/**
* This is the home page snippet. Unlike the Accounts and AddEntry snippet classes,
Expand All @@ -18,15 +17,21 @@ import net.liftweb.util.Helpers._
class HomePage extends Logger {
def render (xhtml : NodeSeq) : NodeSeq = User.currentUser match {
case Full(user) => {
val entries : NodeSeq = user.allAccounts match {
case Nil => Text("You have no accounts set up") // TODO: Add link to create one...
case accounts => accounts.flatMap({account =>
bind("acct", chooseTemplate("account", "entry", xhtml),
"name" -> <a href={"/account/" + account.name}>{account.name}</a>,
"balance" -> Text(account.balance.toString))
}).toSeq
}
bind("account", xhtml, "entry" -> entries)
user.allAccounts match {
case Nil => Text("You have no accounts set up, ") ++
<lift:Menu.item name="Add Account">please add one</lift:Menu.item>
case accounts =>
("#summary" #> {
"li" #> accounts.map { account =>
"a [href]" #> ("/account/" + account.name) &
"a *" #> account.name &
"a [class+]" #> "foo" &
"#balance" #> account.balance.toString
}
} &
"#total" #> accounts.map(_.balance).sum.toString
).apply(xhtml) // apply the CSS binding to the input XHTML
}
}
case _ => <lift:embed what="welcome_msg" />
}
Expand Down
35 changes: 19 additions & 16 deletions PocketChange/src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,43 @@
}
</style>
</head>
<lift:HomePage>
<div class="column span-24 bordered">
<h2>Summary of accounts:</h2>
<account:entry>
<acct:name /> : <acct:balance /> <br/>
</account:entry>
<div class="lift:HomePage">
<div class="column span-24 bordered">
<h2>Summary of accounts:</h2>
<ul id="summary">
<li><a href="#">Account</a> : <span id="balance">$0</span></li>
</ul>
<p>Total: <span id="total">$0</span></p>
</div>
</div>

<hr />
</lift:HomePage>

<hr />

<div class="column span-24">
<lift:AddEntry.addentry form="POST" multipart="true" >
<div class="column span-24"><h3>Entry Form</h3>
<div id="entryform">
<div id="entryform" style="height: 120px">
<table>
<tr>
<td>Account</td>
<td>Date</td>
<td>Description</td>
<td>Value</td>
<td>Receipt Image</td>
<td>Tags</td>
<td>Tags (comma separated)</td>
<td></td>
</tr>
<tr>
<td><e:account /></td>
<td><e:dateOf id="entrydate" maxlength="10"/></td>
<td><e:dateOf id="entrydate" maxlength="10" size="8"/></td>
<td><e:desc /></td>
<td><e:value /></td>
<td><e:receipt /></td>
<td><e:tags /></td>
<td><e:value size="6"/></td>
<td><e:tags size="15" /></td>
<td><button>Add Expense</button></td>
</tr>
<tr>
<td>Receipt Image</td>
<td colspan="2"><e:receipt /></td>
</tr>
</table>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion PocketChange/src/main/webapp/manage.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h3>Your accounts</h3>
</div>
<br />

<h3><a href="/editAcct">Do you need to add an Account?</a></h3>
<h3><lift:Menu.item name="Add Account"/></h3>
</div>

</lift:surround>
4 changes: 4 additions & 0 deletions PocketChange/src/main/webapp/style/pca.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ color: white;
margin-bottom: 15px;
height: 80px;
text-size: 160%;
}

#entryform th {
color: white;
background: #60a8c0;
}

#formBox {
Expand Down
2 changes: 1 addition & 1 deletion PocketChange/src/main/webapp/templates-hidden/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<hr />

<div class="column span-24 last" style="text-align: center">
Copyright © 2009, 2010 - Marius Danciu, Derek Chen-Becker and Tyler Weir
Copyright © 2009-2011 - Marius Danciu, Derek Chen-Becker and Tyler Weir<br/>
MongoDB backend by Alexander Azarov
</div>

Expand Down
2 changes: 1 addition & 1 deletion PocketChange/src/main/webapp/viewAcct.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ <h2>Summary</h2>

<div>
<h3>Filters:</h3>
<table>
<table id="entryform">
<tr>
<th>Start Date</th>
<td><acct:startDate /></td>
Expand Down

0 comments on commit 10aaf01

Please sign in to comment.