diff --git a/Application.hs b/Application.hs index 05370c9..4f5abda 100644 --- a/Application.hs +++ b/Application.hs @@ -49,6 +49,7 @@ import Handler.Shared import Handler.YourAccount import Handler.AccountReset import Handler.Presentation +import Handler.Tags -- This line actually creates our YesodDispatch instance. It is the second half -- of the call to mkYesodData which occurs in Foundation.hs. Please see the diff --git a/Foundation.hs b/Foundation.hs index bce5a3f..22792dd 100644 --- a/Foundation.hs +++ b/Foundation.hs @@ -120,6 +120,8 @@ instance Yesod App where isAuthorized (QueryResultsR _) _ = return Authorized isAuthorized ListChallengesR _ = return Authorized + isAuthorized TagsR _ = return Authorized + isAuthorized (ShowChallengeR _) _ = return Authorized isAuthorized (ChallengeReadmeR _) _ = return Authorized isAuthorized (ChallengeAllSubmissionsR _) _ = return Authorized diff --git a/Handler/Tags.hs b/Handler/Tags.hs new file mode 100644 index 0000000..ed9f792 --- /dev/null +++ b/Handler/Tags.hs @@ -0,0 +1,46 @@ +module Handler.Tags where + +import Import +import Yesod.Form.Bootstrap3 (BootstrapFormLayout (..), renderBootstrap3, bfs) + +import qualified Yesod.Table as Table + +getTagsR :: Handler Html +getTagsR = do + (formWidget, formEnctype) <- generateFormPost tagForm + mUser <- maybeAuth + doTags mUser formWidget formEnctype + +postTagsR :: Handler Html +postTagsR = do + ((result, formWidget), formEnctype) <- runFormPost tagForm + mUser <- maybeAuth + when (checkIfAdmin mUser) $ do + case result of + FormSuccess (t, d) -> do + _ <- runDB $ insert $ Tag t d + return () + _ -> do + return () + doTags mUser formWidget formEnctype + +doTags mUser formWidget formEnctype = do + tags <- runDB $ selectList [] [Asc TagName] + defaultLayout $ do + setTitle "Tags" + $(widgetFile "tags") + +tagsTable :: Table.Table App (Entity Tag) +tagsTable = mempty + ++ Table.text "tag" (\(Entity _ tag) -> tagName tag) + ++ Table.text "description" (\(Entity _ tag) -> (fromMaybe (""::Text) (tagDescription tag))) + + +tagForm :: Form (Text, Maybe Text) +tagForm = renderBootstrap3 BootstrapBasicForm $ (,) + <$> areq textField (bfs MsgTagName) Nothing + <*> aopt textField (bfs MsgTagDescription) Nothing + +checkIfAdmin :: Maybe (Entity User) -> Bool +checkIfAdmin (Just (Entity _ user)) = userIsAdmin user +checkIfAdmin Nothing = False diff --git a/config/models b/config/models index 3a34f2a..fe4716d 100644 --- a/config/models +++ b/config/models @@ -74,4 +74,8 @@ Out test TestId checksum SHA1 UniqueOutSubmissionTestChecksum submission test checksum +Tag + name Text + description Text Maybe + UniqueTagName name -- By default this file is used in Model.hs (which is imported by Foundation.hs) diff --git a/config/routes b/config/routes index 5bce25d..fd40b22 100644 --- a/config/routes +++ b/config/routes @@ -31,4 +31,6 @@ /create-reset-link CreateResetLinkR GET POST /reset-password/#Text ResetPasswordR GET POST +/tags TagsR GET POST + /presentation/4real Presentation4RealR GET diff --git a/gonito.cabal b/gonito.cabal index 0996d0d..8aadcc2 100644 --- a/gonito.cabal +++ b/gonito.cabal @@ -45,6 +45,7 @@ library Handler.YourAccount Handler.AccountReset Handler.Presentation + Handler.Tags if flag(dev) || flag(library-only) cpp-options: -DDEVELOPMENT diff --git a/messages/en.msg b/messages/en.msg index f3fe06b..d6dfb8c 100644 --- a/messages/en.msg +++ b/messages/en.msg @@ -29,3 +29,6 @@ Password: new password EMail: e-mail CreateResetLink: create reset link LinkWrongOrExpired: Link wrong or expired, please ask the site admin again +TagName: tag +TagDescription: description +ListTags: list tags diff --git a/templates/default-layout.hamlet b/templates/default-layout.hamlet index c74548b..ca96c8f 100644 --- a/templates/default-layout.hamlet +++ b/templates/default-layout.hamlet @@ -8,6 +8,7 @@
  • _{MsgHome}
  • _{MsgAbout}
  • _{MsgListChallenges} +
  • _{MsgListTags} $if userIsAdmin $ entityVal user
  • _{MsgCreateChallenge}
  • _{MsgCreateResetLink} @@ -23,6 +24,7 @@
  • _{MsgHome}
  • _{MsgAbout}
  • _{MsgListChallenges} +
  • _{MsgListTags}