From c9671e7f3c3c7adcad78ee69f02b1a4c6bb3d438 Mon Sep 17 00:00:00 2001 From: Chris Done Date: Thu, 13 Nov 2014 22:02:10 +0100 Subject: [PATCH] Implement basic tagging (closes #16) --- Data/Slug.hs | 9 ++++++ Handler/Package.hs | 23 ++++++++++++-- config/routes | 1 + templates/package.hamlet | 11 +++++-- templates/package.julius | 66 ++++++++++++++++++++++++++++++++++++++++ templates/package.lucius | 4 +++ 6 files changed, 109 insertions(+), 5 deletions(-) diff --git a/Data/Slug.hs b/Data/Slug.hs index 0d1b517..50d8f3a 100644 --- a/Data/Slug.hs +++ b/Data/Slug.hs @@ -1,6 +1,7 @@ module Data.Slug ( Slug , mkSlug + , mkSlugLen , safeMakeSlug , unSlug , InvalidSlugException (..) @@ -30,6 +31,14 @@ mkSlug t | otherwise = return $ Slug t where +mkSlugLen :: MonadThrow m => Int -> Int -> Text -> m Slug +mkSlugLen minLen maxLen t + | length t < minLen = throwM $ InvalidSlugException t "Too short" + | length t > maxLen = throwM $ InvalidSlugException t "Too long" + | any (not . validChar) t = throwM $ InvalidSlugException t "Contains invalid characters" + | "-" `isPrefixOf` t = throwM $ InvalidSlugException t "Must not start with a hyphen" + | otherwise = return $ Slug t + minLen, maxLen :: Int minLen = 3 maxLen = 30 diff --git a/Handler/Package.hs b/Handler/Package.hs index 40cd941..997e550 100644 --- a/Handler/Package.hs +++ b/Handler/Package.hs @@ -5,6 +5,7 @@ module Handler.Package where import Data.Char +import Data.Slug import qualified Data.Text as T import qualified Data.Text.Encoding as T import Data.Time (addUTCTime) @@ -44,6 +45,12 @@ getPackageR pn = do return (packages, downloads, recentDownloads, nLikes, liked, metadata) + tags <- fmap (map (\(E.Value v) -> v)) + (runDB (E.selectDistinct + (E.from (\t -> do E.where_ (t ^. TagPackage E.==. E.val pn) + E.orderBy [E.asc (t ^. TagTag)] + return (t ^. TagTag))))) + let likeTitle = if liked then "You liked this!" else "I like this!" :: Text @@ -63,8 +70,6 @@ getPackageR pn = do ]) $(widgetFile "package") where enumerate = zip [0::Int ..] - tags = ["web","framework","library","stable","maintained","potato"] :: [Text] - reformat (Value version, Value title, Value ident, Value hasHaddocks) = (version,fromMaybe title (stripPrefix "Stackage build for " title),ident,hasHaddocks) @@ -178,3 +183,17 @@ postPackageUnlikeR name = maybeAuthId >>= \muid -> case muid of E.where_ $ like ^. LikePackage E.==. E.val name &&. like ^. LikeVoter E.==. E.val uid return () + +postPackageTagR :: PackageName -> Handler () +postPackageTagR packageName = + maybeAuthId >>= + \muid -> + case muid of + Nothing -> return () + Just uid -> + do mtag <- lookupPostParam "slug" + case mtag of + Just tag -> + do slug <- mkSlugLen 1 20 tag + void (runDB (P.insert (Tag packageName slug uid))) + Nothing -> error "Need a slug" diff --git a/config/routes b/config/routes index 199cc1d..e44a155 100644 --- a/config/routes +++ b/config/routes @@ -29,3 +29,4 @@ /compressor-status CompressorStatusR GET /package/#PackageName/like PackageLikeR POST /package/#PackageName/unlike PackageUnlikeR POST +/package/#PackageName/tag PackageTagR POST diff --git a/templates/package.hamlet b/templates/package.hamlet index 1dc26e9..63e1af9 100644 --- a/templates/package.hamlet +++ b/templates/package.hamlet @@ -18,18 +18,23 @@ $newline never
+ $if null tags + + No tags yet. # $forall tag <- tags - + #{tag} , # - +

Add tag

-