{-# LANGUAGE TemplateHaskell, QuasiQuotes, OverloadedStrings, RecordWildCards #-} {- hledger-web's request handlers, and helpers. -} module Hledger.Web.Handlers ( -- * GET handlers getRootR, getJournalR, getJournalEntriesR, getJournalEditR, getRegisterR, -- ** helpers -- sidebar, -- accountsReportAsHtml, -- accountQuery, -- accountOnlyQuery, -- accountUrl, -- entriesReportAsHtml, -- journalTransactionsReportAsHtml, -- registerReportHtml, -- registerItemsHtml, -- registerChartHtml, -- stringIfLongerThan, -- numberTransactionsReportItems, -- mixedAmountAsHtml, -- * POST handlers postJournalR, postJournalEntriesR, postJournalEditR, postRegisterR, -- * Common page components -- * Utilities ViewData(..), nullviewdata, ) where import Prelude import Control.Applicative ((<$>)) import Data.Either (lefts,rights) import Data.List import Data.Maybe import Data.Text(Text,pack,unpack) import qualified Data.Text (null) import Data.Time.Calendar import Data.Time.Clock import Data.Time.Format import System.FilePath (takeFileName) import System.IO.Storage (putValue, getValue) import System.Locale (defaultTimeLocale) #if BLAZE_HTML_0_5 import Text.Blaze.Internal (preEscapedString) import Text.Blaze.Html (toHtml) #else import Text.Blaze (preEscapedString, toHtml) #endif import Text.Hamlet hiding (hamlet) import Text.Printf import Yesod.Core -- import Yesod.Json import Hledger hiding (is) import Hledger.Cli hiding (version) import Hledger.Web.Foundation import Hledger.Web.Options import Hledger.Web.Settings -- routes: -- /static StaticR Static getStatic -- -- /favicon.ico FaviconR GET -- /robots.txt RobotsR GET -- / RootR GET -- /journal JournalR GET POST -- /journal/entries JournalEntriesR GET POST -- /journal/edit JournalEditR GET POST -- /register RegisterR GET POST -- -- /accounts AccountsR GET -- -- /api/accounts AccountsJsonR GET ---------------------------------------------------------------------- -- GET handlers getRootR :: Handler RepHtml getRootR = redirect defaultroute where defaultroute = RegisterR -- | The formatted journal view, with sidebar. getJournalR :: Handler RepHtml getJournalR = do vd@VD{..} <- getViewData let sidecontent = sidebar vd -- XXX like registerReportAsHtml inacct = inAccount qopts -- injournal = isNothing inacct filtering = m /= Any -- showlastcolumn = if injournal && not filtering then False else True title = case inacct of Nothing -> "Journal"++s2 Just (a,inclsubs) -> "Transactions in "++a++s1++s2 where s1 = if inclsubs then " (and subaccounts)" else "" where s2 = if filtering then ", filtered" else "" maincontent = journalTransactionsReportAsHtml opts vd $ journalTransactionsReport (reportopts_ $ cliopts_ opts) j m defaultLayout $ do setTitle "hledger-web journal" addWidget $ toWidget [hamlet| ^{topbar vd}
#{txn} |] where evenodd = if even n then "even" else "odd" :: String txn = trimnl $ showTransaction t where trimnl = reverse . dropWhile (=='\n') . reverse -- | Render a "TransactionsReport" as html for the formatted journal view. journalTransactionsReportAsHtml :: WebOpts -> ViewData -> TransactionsReport -> HtmlUrl AppRoute journalTransactionsReportAsHtml _ vd (_,items) = [hamlet|