By | Chris Done;Chris Done |
At | 2011-08-13; 2011-08-13 |
Title | Reports page. |
Description |
Edit file SQLCHANGES 33188 → 33188
- 1 CREATE TABLE hint (id serial primary key, paste integer references
- 2 paste(id) on delete cascade on update cascade, type text not null,
- 3 content text not null);
- 4 - 5 create table report (id serial primary key, paste integer not null
- 6 references paste(id) on delete cascade on update cascade, comments
- 7 text not null);
+ 1 alter table report add created timestamp with time zone not null default now();
… … … … Edit file src/Main.hs 33188 → 33188
14 14 import Amelie.Controller.Home as Home
15 15 import Amelie.Controller.New as New
16 16 import Amelie.Controller.Paste as Paste
17 17 import Amelie.Controller.Raw as Raw
18 18 import Amelie.Controller.Report as Report
+ 19 import Amelie.Controller.Reported as Reported
19 20 import Amelie.Controller.Script as Script
20 21 import Amelie.Controller.Stepeval as Stepeval
21 22 import Amelie.Controller.Steps as Steps
22 23 import Amelie.Controller.Style as Style
23 24 import Amelie.Model.Announcer (newAnnouncer)
… … … … 84 85 ,("/steps/:id",run Steps.handle)
85 86 -- @ next stepsPage
86 87 ,("/raw/:id",run Raw.handle)
87 88 -- @ next rawPastePage
88 89 ,("/report/:id",run Report.handle)
+ 90 ,("/reported",run Reported.handle)
89 91 -- @ next reportPastePage
90 92 ,("/new",run New.handle)
91 93 -- @ next newPastePage
92 94 ,("/edit/:id",run New.handle)
93 95 -- @ next annotatePage
… … … … Edit file src/Amelie/Types.hs 33188 → 33188
10 10 ,module Amelie.Types.Page
11 11 ,module Amelie.Types.Newtypes
12 12 ,module Amelie.Types.View
13 13 ,module Amelie.Types.Config
14 14 ,module Amelie.Types.Activity
- 15 ,module Amelie.Types.Stepeval)
+ 15 ,module Amelie.Types.Stepeval
+ 16 ,module Amelie.Types.Report)
16 17 where
17 18 18 19 import Amelie.Types.MVC
19 20 import Amelie.Types.Paste
20 21 import Amelie.Types.Channel
… … … … 23 24 import Amelie.Types.Newtypes
24 25 import Amelie.Types.View
25 26 import Amelie.Types.Config
26 27 import Amelie.Types.Activity
27 28 import Amelie.Types.Stepeval
- 28 + 29 import Amelie.Types.Report
… … … … Edit file src/Amelie/Model/Report.hs 33188 → 33188
5 5 {-# LANGUAGE ViewPatterns #-}
6 6 7 7 -- | Report model.
8 8 9 9 module Amelie.Model.Report
- 10 (getReports,createReport)
+ 10 (getSomeReports,createReport,countReports)
11 11 where
12 12 13 13 import Amelie.Types
14 14 import Amelie.Model
15 15 16 16 import Control.Monad
- 17 import Prelude hiding ((++))
+ 17 import Data.Maybe
+ 18 import Data.Monoid.Operator ((++))
+ 19 import Prelude hiding ((++))
18 20 19 21 -- @ label getReports
- 20 -- @ task Get reports.
- 21 -- | Get the reports.
- 22 getReports :: Model [Paste]
- 23 getReports =
- 24 queryNoParams ["SELECT *"
+ 22 -- @ task Get some reports.
+ 23 -- | Get some paginated reports.
+ 24 getSomeReports :: Pagination -> Model [Report]
+ 25 getSomeReports Pagination{..} =
+ 26 queryNoParams ["SELECT created,paste,comments"
25 27 ,"FROM report"
26 28 ,"ORDER BY id DESC"
- 27 ,"LIMIT 20"]
+ 29 ,"OFFSET " ++ show (max 0 (pnPage - 1) * pnLimit)
+ 30 ,"LIMIT " ++ show pnLimit]
+ 31 + 32 -- | Count reports.
+ 33 countReports :: Model Integer
+ 34 countReports = do
+ 35 rows <- singleNoParams ["SELECT COUNT(*)"
+ 36 ,"FROM report"]
+ 37 return $ fromMaybe 0 rows
28 38 29 39 -- @ label createReport
30 40 -- @ task Create report.
31 41 -- | Create a new report.
32 42 createReport :: ReportSubmit -> Model (Maybe ReportId)
… … … … Add file src/Amelie/View/Reported.hs 33188
+ 1 {-# OPTIONS -Wall -fno-warn-name-shadowing #-} + 2 {-# LANGUAGE OverloadedStrings #-} + 3 {-# LANGUAGE RecordWildCards #-} + 4 + 5 -- | Reported page view. + 6 + 7 module Amelie.View.Reported + 8 (page) + 9 where + 10 + 11 import Amelie.Types + 12 import Amelie.View.Html + 13 import Amelie.View.Layout + 14 + 15 import Data.Monoid.Operator ((++)) + 16 import Data.Time.Show (showDateTime) + 17 import Prelude hiding ((++)) + 18 import Text.Blaze.Html5 as H hiding (map) + 19 + 20 -- | Render the reported page. + 21 page :: Pagination -> [Report] -> Html + 22 page pn rs = + 23 layoutPage $ Page { + 24 pageTitle = "Reported pastes" + 25 , pageBody = reported pn rs + 26 , pageName = "reported" + 27 } + 28 + 29 -- | View the paginated reports. + 30 reported :: Pagination -> [Report] -> Html + 31 reported pn rs = do + 32 darkSection "Reported pastes" $ do + 33 paginate pn $ do + 34 table ! aClass "latest-pastes" $ do + 35 tr $ mapM_ (th . toHtml) $ words "Date Paste Comments" + 36 reports rs + 37 + 38 where reports = mapM_ $ \Report{..} -> tr $ do + 39 td $ toHtml $ showDateTime reportDate + 40 td $ toHtml $ href ("/" ++ show reportPasteId) $ show reportPasteId + 41 td $ toHtml reportComments Add file src/Amelie/Types/Report.hs 33188
+ 1 module Amelie.Types.Report where + 2 + 3 import Amelie.Types.Newtypes (PasteId) + 4 + 5 import Data.Text (Text) + 6 import Data.Time (UTCTime,zonedTimeToUTC) + 7 import Database.PostgreSQL.Simple.QueryResults (QueryResults(..)) + 8 + 9 data Report = Report { + 10 reportDate :: UTCTime + 11 ,reportPasteId :: PasteId + 12 ,reportComments :: Text + 13 } deriving Show + 14 + 15 instance QueryResults Report where + 16 convertResults field values = Report { + 17 reportDate = zonedTimeToUTC date + 18 , reportPasteId = paste + 19 , reportComments = comments + 20 } + 21 where (date,paste,comments) = convertResults field values Add file src/Amelie/Controller/Reported.hs 33188
+ 1 {-# OPTIONS -Wall #-} + 2 {-# LANGUAGE OverloadedStrings #-} + 3 + 4 -- | Reported page controller. + 5 + 6 module Amelie.Controller.Reported + 7 (handle) + 8 where + 9 + 10 import Amelie.Controller (output,getPagination) + 11 import Amelie.Model + 12 import Amelie.Model.Report (getSomeReports,countReports) + 13 import Amelie.View.Reported (page) + 14 + 15 -- @ label reportedPage + 16 -- @ do Reported pastes. + 17 -- @ trigger getPastes + 18 -- @ next pastePage + 19 -- @ next reportedPage + 20 handle :: Controller () + 21 handle = do + 22 pn <- getPagination + 23 total <- model countReports + 24 reports <- model $ getSomeReports pn + 25 let pn' = pn { pnRoot = "/reported" + 26 , pnResults = fromIntegral (length reports) + 27 , pnTotal = total } + 28 output $ page pn' reports