Digestive functors: multiple file upload field? - haskell

I'm trying to recreate fairly standard image / file upload functionality whereby a given field allows the uploading of one or more files / images with something like an "add another file" button and/or the ability to replace existing files.
I have file uploads working and I have multiple subforms working, but I can't get multiple subforms working with file input.
I have created an example (shown below) heavily based on examples/dynamic-list.hs which highlights the problem, which appears to be that postForm returns an appropriate FilePath in the view but does not return it in the "result".
Another problem with dynamic-list.hs is that it only shows a trivial use case using static data. Having an actual dynamic form where the data changes in response to user input is significantly more complicated, so I'm hoping that I can hammer out a more comprehensive version of dynamic-list.hs which would be (much) more helpful to beginners.
My code so far:
{-# LANGUAGE OverloadedStrings, PackageImports, TupleSections, ScopedTypeVariables, DeriveDataTypeable, TypeSynonymInstances, FlexibleInstances #-}
module Handler.Test where
import Prelude hiding (div, span)
import Control.Applicative
import Control.Monad
import Control.Monad.Trans
import Data.Maybe
import Data.Text hiding (unlines, intercalate, concat)
import Data.Text.Encoding
import Snap.Core hiding (method)
import Snap.Snaplet
------------------------------------------------------------------------------
import Heist.Splices.Html
import Text.Digestive
import Text.Digestive.Snap
import Text.Digestive.Heist
import Text.Blaze.Html5 as H
import Text.Digestive.Blaze.Html5 as DH
import qualified Text.Blaze.Html5.Attributes as A
import Text.Digestive.Form
import Text.Digestive.Util
import Text.Blaze.Renderer.XmlHtml
import Data.List as L
------------------------------------------------------------------------------
import Application
import Helpers.Forms
import Helpers.Theme
import Debug.Trace
------------------------------------------------------------------------------
handleEntityTest :: Handler App App ()
handleEntityTest = undefined
type Product = Text
type Quantity = Int
--------------------------------------------------------------------------------
data Order = Order {
orderName :: Text
, orderItems :: [OrderItem]
} deriving (Show)
data OrderItem = OrderItem
{ orderProduct :: Text
, orderQuantity :: Quantity
, orderFile :: Maybe FilePath
} deriving (Show)
--------------------------------------------------------------------------------
orderForm :: Monad m => Order -> Form Html m Order
orderForm order = Order
<$> "orderName" .: text (Just $ orderName order)
<*> "orderItems" .: listOf orderItemForm (Just $ orderItems order)
orderItemForm :: Monad m => Formlet Html m OrderItem
orderItemForm def = OrderItem
<$> "product" .: text (orderProduct <$> def)
<*> "quantity" .: stringRead "Can't parse quantity" (orderQuantity <$> def)
<*> "file" .: file
--------------------------------------------------------------------------------
orderView :: View H.Html -> H.Html
orderView view = do
DH.form view "" $ do
DH.label "name" view "Order name: "
DH.inputText "orderName" view
H.br
DH.label "orderItems.indices" view "(Usually hidden) Indices: "
DH.inputText "orderItems.indices" view
H.br
mapM_ orderItemView $ listSubViews "orderItems" view
H.br
DH.inputSubmit "Submit"
orderItemView :: View H.Html -> H.Html
orderItemView view = do
childErrorList "" view
DH.label "product" view "Product: "
DH.inputText "product" view
H.br
DH.label "quantity" view "Quantity: "
DH.inputText "quantity" view
H.br
DH.label "file" view "file"
DH.inputFile "file" view
H.br
-------------------------------------------------------
handleTest :: Handler App App ()
handleTest = do
r <- runFormWith defaultFormConfig "test" $ orderForm $ Order "test form" [(OrderItem "" 0 Nothing)]
case r of
(view, Nothing) -> do
-- GET
renderPageHtml "Initial form view" $ toHtml $ orderView $ debugForm view
-- POST
(view, Just order) -> do
s <- runFormWith (defaultFormConfig { method = Just Get }) "test" $ orderForm $ order {orderItems = ((orderItems order) ++ [(OrderItem "" 0 Nothing)]) }
case s of
(view', Nothing) -> do
renderPageHtml "Subsequent form view" $ html
where
html = do
p $ do
mapM_ div [ br, br, br
, orderView $ debugForm view'
, toHtml $ show order
]
(view', Just order) -> do
renderPageHtml "Subsequent form view" $ p "This shouldn't ever happen"
------------------------------------------------------------
debugForm :: View Html -> View Html
debugForm v = trace (t) v
where
showTuple (path,input) = ("path : " ++ (show path) ++ "=" ++ (show input))
t = unlines $ [
(""), ("")
, ("viewName : " ++ (unpack $ viewName v) )
, ("viewMethod : " ++ (show $ viewMethod v) )
, ("viewContext : " ++ (show $ viewContext v) )
--, ("viewInput : " ++ (unlines $ fmap (\(path, input) -> (show path) ++ "=" ++ (show input) ) $ viewInput v ))
, ("viewInput : " ++ (unlines $ fmap showTuple $ viewInput v ))
, ("debugViews : " ++ (unlines $ fmap show $ debugViewPaths v) )
]

Related

Template Haskell on Aeson

I have a data type like this:
module My.Module
data A = A { aFoo :: Integer } deriving (Generic, Show)
And I have generic option for Aeson
import Data.Char ( toUpper, toLower )
genericOptions :: String -> Options
genericOptions prefix = defaultOptions
{ fieldLabelModifier = dropPrefix $ length prefix
, constructorTagModifier = addPrefix prefix
, omitNothingFields = True
}
where
dropPrefix l s = let remainder = drop l s
in (toLower . head) remainder : tail remainder
addPrefix p s = p ++ toUpper (head s) : tail s
So I can use it like this
instance A.FromJSON A where
parseJSON = A.genericParseJSON $ genericOptions "A"
instance A.ToJSON A where
toJSON = A.genericToJSON $ genericOptions "A"
But I realize I could use some template haskell
import Data.Aeson.TH ( deriveJSON )
import Language.Haskell.TH.Syntax ( Dec, Name, Q )
genericDeriveJSON :: Name -> Q [Dec]
genericDeriveJSON name =
deriveJSON (genericOptions (show name)) name
$(genericDeriveJSON ''A)
It throws an error:
Exception when trying to run
compile-time code:
Prelude.tail: empty list
Code: A.genericDeriveJSON ''A
It seems drop l s on dropPrefix returned an empty string meaning the value of show name is not string "A". Since I don't think I could inspect the value, anybody knows what is the value?
Try to use nameBase instead of show (which is meant for debugging and not core logic).
To see what show is doing you can look at the implementation of show which is defined as showName which is itself defined as showName' Alone, to see roughly that it constructs the fully qualified name of your type.

How to get a DoubleClicked event from a reflex-dom listbox

The following code displays a reflex-dom dropdown element visually as a listbox and displays at the bottom always the last selected (clicked) line.
{-# LANGUAGE OverloadedStrings #-}
import Reflex.Dom
import qualified Data.Text as T
import qualified Data.Map as Map
import Data.Monoid((<>))
import Data.Maybe (fromJust)
main :: IO ()
main = mainWidget $ el "div" $ do
dd <- dropdown "2" (constDyn countries) $ def & attributes .~ constDyn ("size" =: "10")
el "p" $ return ()
let selItem = result <$> value dd
dynText selItem
return ()
countries :: Map.Map T.Text T.Text
countries = Map.fromList [("1", "France"), ("2", "Switzerland"), ("3", "Germany"), ("4", "Italy"), ("5", "USA")]
result :: T.Text -> T.Text
result key = "You selected: " <> fromJust (Map.lookup key countries)
I want to change this code, so it displays at the bottom always the last double clicked line!
I tried several things
use the domEvent function: This does not work, because Dropdown is not an instance of the HasDomEvent class.
filter the event in the value _dropdown_change of the Dropdown record. But I didn't find any way to filter only DoubleClick events.
use the newtype EventSelector. Again I don't see hwo I can use it.
Question: How can I get at the double click event?
You can use domEvent to get at the double click.
The following code uses elAttr to create a listbox like the one you created with dropdown. The domEvent function is used to create double click Event's for each of the listbox options which are then combined to get a Dynamic that represents the most recently double clicked option.
I left the dropbox code in place for comparison purposes.
{-# LANGUAGE OverloadedStrings #-}
import Reflex.Dom
import qualified Data.Text as T
import qualified Data.Map as Map
import Data.Monoid((<>))
import Data.Maybe (fromJust)
import Data.Traversable (forM)
-- a listbox that responds to double clicks
listbox :: MonadWidget t m => T.Text -- default
-> Map.Map T.Text T.Text -- entries
-> Map.Map T.Text T.Text -- attributes
-> m (Dynamic t T.Text)
listbox def entries attr = do
optEv <- elAttr "select" attr $
forM (Map.toList entries) $ \(i,c) -> do
let sel = if i == def
then "selected" =: "selected"
else mempty
(e, _) <- elAttr' "option" sel $ text c
return (i <$ domEvent Dblclick e)
holdDyn def $ leftmost optEv
main :: IO ()
main = mainWidget $ el "div" $ do
-- original code (responds to single clicks)
dd <- dropdown "2" (constDyn countries) $ def & attributes .~ constDyn ("size" =: "10")
el "p" $ return ()
let selItem = result <$> value dd
dynText selItem
el "p" $ return ()
-- new code (responds to double clicks)
lb <- listbox "3" countries ("size" =: "10")
el "p" $ return ()
let dblItem = result <$> lb
dynText dblItem
return ()
countries :: Map.Map T.Text T.Text
countries = Map.fromList [("1", "France"), ("2", "Switzerland"), ("3", "Germany"), ("4", "Italy"), ("5", "USA")]
result :: T.Text -> T.Text
result key = "You selected: " <> fromJust (Map.lookup key countries)

how do I modify inputText to use inputCheckbox

I am trying to do something similar to this, where for an element in list of strings, I have a checkbox next to it and figure out which checkbox is checked or not. Using examples from the internet, I was able to get an example running
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid
import Data.String
import Data.List
import qualified Data.Text as T
import Web.Spock.Safe
import Web.Spock.Digestive
import Text.Blaze (ToMarkup(..))
import Text.Blaze.Html5 hiding (html, param, main)
import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
import Text.Digestive
import Text.Digestive.Blaze.Html5
import System.Directory
import Control.Monad.IO.Class
import Control.Monad (forM_)
gen :: Html -> [Html] -> Html
gen title elts = H.html $ do
H.head $
H.title title
H.body $
H.ul $ mapM_ H.li elts
data CheckBox = CheckBox { postTitle :: T.Text }
checkboxForm = CheckBox
<$> "title" .: Text.Digestive.text Nothing
renderForm :: View Html -> Html
renderForm v = do
Text.Digestive.Blaze.Html5.form v "POST" $ do
H.p $ do
Text.Digestive.Blaze.Html5.label "title" v "Post title: "
inputText "text" v
inputSubmit "Submit Post"
main :: IO ()
main =
runSpock 8080 $ spockT Prelude.id $ do
get root $ do
listing <- liftIO $ getDirectoryContents "/home/hasenov/mydir"
let filteredListing = filter (\l -> not $ isPrefixOf "." l) listing
(view, result) <- runForm "checkboxForm" checkboxForm
case result of
Nothing -> lazyBytes $ renderHtml (renderForm view)
Just newCheckbox -> lazyBytes $ renderHtml (renderForm view)
-- lazyBytes $ renderHtml (gen "My Blog" (Data.List.map fromString filteredListing))
-- get ("hello" <//> var) $ \name ->
-- text ("Hello " <> name <> "!")
However, in the function renderForm, when I change inputText to something like inputCheckbox "True", I get the error True does not exist. I am not able to find an example where inputCheckbox is used, I was hoping someone would help me adapt filteredString so it would display checkboxes next to it, and I can run the form properly. Also, in previous link I posted, I don't know what the function inputCheckBox, since I could only find inputCheckbox. Perhaps this is an outdated function?
I'm answering my own question since I figured out how to get inputCheckbox instead of inputText. Actually, this example helped alot. It was the only one I could find which uses inputCheckbox. What I needed to do was change
data CheckBox = CheckBox { postTitle :: T.Text }
to
data CheckBox = CheckBox Bool
Then I could just initialized
checkboxForm = CheckBox
<$> "title" .: bool (Just False)
Here is the full source:
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid
import Data.String
import Data.List
import qualified Data.Text as T
import Web.Spock.Safe
import Web.Spock.Digestive
import Text.Blaze (ToMarkup(..))
import Text.Blaze.Html5 hiding (html, param, main)
import qualified Text.Blaze.Html5 as H
import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
import Text.Digestive
import Text.Digestive.Blaze.Html5
import System.Directory
import Control.Monad.IO.Class
import Control.Monad (forM_)
gen :: Html -> [Html] -> Html
gen title elts = H.html $ do
H.head $
H.title title
H.body $
H.ul $ mapM_ H.li elts
data CheckBox = CheckBox Bool
checkboxForm = CheckBox
<$> "title" .: bool (Just False)
renderForm :: View Html -> [Html] -> Html
renderForm v strings = do
Text.Digestive.Blaze.Html5.form v "POST" $ do
H.p $ mapM_ (\string -> do
inputCheckbox "title" v
Text.Digestive.Blaze.Html5.label "title" v string
H.br) strings
inputSubmit "Submit Post"
main :: IO ()
main =
runSpock 8080 $ spockT Prelude.id $ do
get root $ do
listing <- liftIO $ getDirectoryContents "/home/ecks/btsync-gambino"
let filteredListing = filter (\l -> not $ isPrefixOf "." l) listing
(view, result) <- runForm "checkboxForm" checkboxForm
case result of
Nothing -> lazyBytes $ renderHtml (renderForm view (Data.List.map fromString filteredListing))
Just newCheckbox -> lazyBytes $ renderHtml (renderForm view (Data.List.map fromString filteredListing))
-- lazyBytes $ renderHtml (gen "My Blog" (Data.List.map fromString filteredListing))
-- get ("hello" <//> var) $ \name ->
-- text ("Hello " <> name <> "!")

Is there a way how to enumerate all functions in a module using Template Haskell?

While I can use reify to get information about most other syntactic constructs, I couldn't find anything that would give some information about a module.
Unfortunately Template Haskell currently has no such capabilities. All the solutions involve parsing of the module's source-code. However the location and loc_filename functions of TH make it easy to locate the module with the calling splice.
Here is a solution extracted from the source code of one of my projects:
{-# LANGUAGE LambdaCase, TupleSections #-}
import Language.Haskell.TH
import qualified Data.Attoparsec.Text as AP
import qualified Data.Text.IO as Text
import qualified Data.Text as Text
import qualified Data.Char as Char
import Data.Maybe
import Data.List
import Control.Applicative
import Data.Traversable
import Prelude hiding (mapM)
reifyLocalFunctions :: Q [(Name, Type)]
reifyLocalFunctions =
listTopLevelFunctionLikeNames >>=
mapM (\name -> reifyFunction name >>= mapM (return . (name, ))) >>=
return . catMaybes
where
listTopLevelFunctionLikeNames = do
loc <- location
text <- runIO $ Text.readFile $ loc_filename loc
return $ map (mkName . Text.unpack) $ nub $ parse text
where
parse text =
either (error . ("Local function name parsing failure: " ++)) id $
AP.parseOnly parser text
where
parser =
AP.sepBy (optional topLevelFunctionP <* AP.skipWhile (not . AP.isEndOfLine))
AP.endOfLine >>=
return . catMaybes
where
topLevelFunctionP = do
head <- AP.satisfy Char.isLower
tail <- many (AP.satisfy (\c -> Char.isAlphaNum c || c `elem` ['_', '\'']))
return $ Text.pack $ head : tail
reifyFunction :: Name -> Q (Maybe Type)
reifyFunction name = do
tryToReify name >>= \case
Just (VarI _ t _ _) -> return $ Just $ t
_ -> return Nothing
tryToReify :: Name -> Q (Maybe Info)
tryToReify n = recover (return Nothing) (fmap Just $ reify n)

Reify a module into a record

Suppose I have an arbitrary module
module Foo where
foo :: Moo -> Goo
bar :: Car -> Far
baz :: Can -> Haz
where foo, bar, and baz are correctly implemented, etc.
I'd like to reify this module into an automatically-generated data type and corresponding object:
import Foo (Moo, Goo, Car, Far, Can, Haz)
import qualified Foo
data FooModule = Foo
{ foo :: Moo -> Goo
, bar :: Car -> Far
, baz :: Can -> Haz
}
_Foo_ = Foo
{ foo = Foo.foo
, bar = Foo.bar
, baz = Foo.baz
}
Names must be precisely the same as the original module.
I could do this by hand, but that is very tedious, so I'd like to write some code to perform this task for me.
I'm not really sure how to approach such a task. Does Template Haskell provide a way to inspect modules? Should I hook into some GHC api? Or am I just as well off with a more ad-hoc approach such as scraping haddock pages?
(This is for GHC-7.4.2; it probably won't compile with HEAD or 7.6 because of some changes in Outputable). I didn't find anything to inspect modules in TH.
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# OPTIONS -Wall #-}
import GHC
import GHC.Paths -- ghc-paths package
import Outputable
import GhcMonad
main :: IO ()
main = runGhc (Just libdir) $ goModule "Data.Map"
goModule :: GhcMonad m => String -> m ()
goModule modStr = do
df <- getSessionDynFlags
_ <- setSessionDynFlags df
-- ^ Don't know if this is the correct way, but it works for this purpose
setContext [IIDecl (simpleImportDecl (mkModuleName modStr))]
infos <- mapM getInfo =<< getNamesInScope
let ids = onlyIDs infos
liftIO . putStrLn . showSDoc . render $ ids
onlyIDs :: [Maybe (TyThing, Fixity, [Instance])] -> [Id]
onlyIDs infos = [ i | Just (AnId i, _, _) <- infos ]
render :: [Id] -> SDoc
render ids = mkFields ids $$ text "------------" $$ mkInits ids
mkFields :: [Id] -> SDoc
mkFields = vcat . map (\i ->
text "," <+> pprUnqual i <+> text "::" <+> ppr (idType i))
mkInits :: [Id] -> SDoc
mkInits = vcat . map (\i ->
text "," <+> pprUnqual i <+> text "=" <+> ppr i)
-- * Helpers
withUnqual :: SDoc -> SDoc
withUnqual = withPprStyle (mkUserStyle neverQualify AllTheWay)
pprUnqual :: Outputable a => a -> SDoc
pprUnqual = withUnqual . ppr

Resources