Showing posts with label yesod. Show all posts
Showing posts with label yesod. Show all posts

Tuesday, December 31, 2019

Validated Hamlet Textarea Form Field

Here is my custom textarea field that validates correct hamlet content

in the Form:
...
  (hamletHtmlResult, hamletHtmlView) <- mreq validatedHamletTextareaField
...

the field:

validatedHamletTextareaField :: Field (HandlerFor App) Textarea
validatedHamletTextareaField = checkM isValidHamlet textareaField
  where
    isValidHamlet :: Textarea -> Handler (Either AppMessage Textarea)
    isValidHamlet textarea@(Textarea text) = do
      eitherParsedTemplate <- tryParse text
      return $
        case eitherParsedTemplate of
          Left _ -> Left MsgGlobalInvalidHamlet
          Right _ -> Right $ Textarea text
      where
        tryParse :: Text -> Handler (Either SomeException HamletTemplate)
        tryParse text = try $ parseHamletTemplate defaultHamletSettings $ unpack text

Tuesday, July 24, 2018

Ways to stream yesod data to the client


model:

Rawdata
  bytes ByteString


way 1:

  addHeader "Content-Disposition" $
    T.concat ["attachment; filename=\"", filename, "\""]
  rawdata <- runDB $ get404 rawdataId                                                                                                                                         
  let bytes = rawdataBytes rawdata                                                                                                                                            
  sendResponse (TE.encodeUtf8 mimetype, toContent bytes)                                                                                                                      


way 2:

  let bytesSource = selectSource [RawdataId ==. rawdataId] []                                                                                                                 
  respondSourceDB (TE.encodeUtf8 mimetype) $ bytesSource $= awaitForever toBuilder'                                                                                           
  where                                                                                                                                                                       
    toBuilder' (Entity _ rawdata) = do                                                                                                                                        
      sendChunkBS $ rawdataBytes rawdata                                                                                                                                      
      sendFlush                                                                                                                                                               


way 3:

  let bytesSource = E.selectSource $ E.from $ \rd -> do
        E.where_ (rd E.^. RawdataId E.==. E.val rawdataId)
        return $ rd E.^. RawdataBytes
  respondSourceDB (TE.encodeUtf8 mimetype) $ bytesSource $= awaitForever toBuilder'
  where
    toBuilder' (E.Value bytes) = do
      sendChunkBS bytes
      sendFlush

Friday, May 18, 2018

nixos haskell yesod package with overrides

create a new file my-stack-project.nix:

let
  pkgs = import  {};
  pkg = pkgs.haskellPackages.callCabal2nix "my-stack-project" ./. {};
in
  pkgs.haskell.lib.overrideCabal pkg (_: {
    doHaddock = false;
    postInstall = ''
      cp -r ./static $out/bin
      cp -r ./config $out/bin
    '';
  })

Tuesday, September 26, 2017

Yesod embed json in html


getPersonListR :: Handler Html
getPersonListR = do
  urlRender <- getUrlRender
  personEnts <- runDB $ selectList [] [Asc PersonId]
  let jData = map (\(Entity personId person) ->
                      JPerson { jPersonPerson = person
                              , jPersonEditFormUrl = urlRender $ PersonEditFormR personId }
                  ) personEnts
  jsonData <- returnJson jData >>= return . toJsonText
  defaultLayout $ do
    toWidgetBody [julius|var data = #{rawJS jsonData}|]
    $(widgetFile "person_list")

data JPerson = JPerson
  { jPersonPerson :: Person
  , jPersonEditFormUrl :: Text
  }

Wednesday, December 30, 2015

haskell yesod curl POST request ... handling CSRF


get the cookie with token in it:

$ curl -c cookie.txt http://localhost:3000/

$ cat cookie.txt 
# Netscape HTTP Cookie File
# http://curl.haxx.se/docs/http-cookies.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_localhost FALSE / FALSE 1451524550 _SESSION gxV1yVNefLhiS7K3/ukfWWi5GXfD7wXfwJFYDXk3fz/HvyPqcSIcU7BBIKdOrj0jrpcU9DroL0+ioD3rr8cbvCSy+A+jPDpt/8kkiSPjYE86cGyTiueVo2cOGWcc8=
localhost FALSE / FALSE 0 XSRF-TOKEN seQLdve8GY


use token:

$ curl -v \
    --cookie cookie.txt \
    -c cookie.txt \
    -H "x-xsrf-token: seQLdve8GY" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -X POST \
    -d '{"foo":"bar", ...}' \
    http://localhost:3000/updateDruckprodukt

Saturday, August 08, 2015

yesod persistent enum type

State.hs
{-# LANGUAGE TemplateHaskell #-}
module State where

import Database.Persist.TH
import Prelude

data State = New | Released
    deriving (Show, Read, Eq)
derivePersistField "State"
models
MyModel json
    state S.State
    createdAt UTCTime default=CURRENT_TIME
Model.hs
import qualified State as S

instance ToJSON S.State where
    toJSON S.New = String "New"
    toJSON S.Released = String "Released"

instance FromJSON S.State where
    parseJSON (String "New") = pure S.New
    parseJSON (String "Released") = pure S.Released

Tuesday, May 06, 2014

install yesod on debian wheezy

# apt-get install haskell-platform
# apt-get install yesod

$ yesod init
$ cd 
$ cabal install
$ yesod devel

Friday, April 25, 2014

install yesod on FreeBSD

This is for FreeBSD 10.0

http://www.yesodweb.com/page/quickstart
$ sudo pkg install hs-haskell-platform
$ cabal update

... now this says that we should upgrade with: cabal install cabal-install
... but do NOT do it, otherwise some packages will not find some header files

$ cabal install yesod-platform yesod-bin --max-backjumps=-1 --reorder-goals

... this takes long

$ ghc-pkg list | grep yesod
    yesod-1.2.5.2
    yesod-auth-1.3.0.4
    yesod-auth-hashdb-1.3.0.1
    yesod-core-1.2.14
    yesod-form-1.3.8.2
    yesod-persistent-1.2.2.3
    yesod-platform-1.2.10
    yesod-routes-1.2.0.6
    yesod-static-1.2.2.5
    yesod-test-1.2.1.2
If you happen to mess up the ghc/cabal packages, just
rm -rf ~/.cabal ~/.ghc/