changing logic. not working.

This commit is contained in:
nek0 2014-08-13 22:30:48 +02:00
parent 3dc4bf631a
commit 6fd8865944
7 changed files with 47 additions and 25 deletions

View file

@ -66,7 +66,7 @@ instance Yesod App where
defaultLayout widget = do defaultLayout widget = do
master <- getYesod master <- getYesod
mmsg <- getMessage mmsg <- getMessage
msu <- lookupSession "username" msu <- lookupSession "userId"
-- We break up the default layout into two components: -- We break up the default layout into two components:
-- default-layout is the contents of the body tag, and -- default-layout is the contents of the body tag, and

View file

@ -1,6 +1,7 @@
module Handler.Login where module Handler.Login where
import Import import Import
import Data.Text
data Credentials = Credentials data Credentials = Credentials
{ credentialsName :: Text { credentialsName :: Text
@ -24,7 +25,7 @@ postLoginR = do
Just user -> do Just user -> do
case credentialsPasswd cred == userPassword (entityVal user) of case credentialsPasswd cred == userPassword (entityVal user) of
True -> do True -> do
setSession "username" $ userName (entityVal user) setSession "userId" $ pack $ show $ entityKey user
setMessage $ [shamlet|<pre>Successfully logged in|] setMessage $ [shamlet|<pre>Successfully logged in|]
redirect $ HomeR redirect $ HomeR
False -> do False -> do
@ -41,6 +42,6 @@ loginForm = renderDivs $ Credentials
getLogoutR :: Handler Html getLogoutR :: Handler Html
getLogoutR = do getLogoutR = do
deleteSession "username" deleteSession "userId"
setMessage $ [shamlet|<pre>Succesfully logged out|] setMessage $ [shamlet|<pre>Succesfully logged out|]
redirect $ HomeR redirect $ HomeR

View file

@ -3,8 +3,11 @@ module Handler.Profile where
import Import import Import
getProfileR :: Text -> Handler Html getProfileR :: Text -> Handler Html
getProfileR username = do getProfileR username = error "reworking logic" -- do
msu <- lookupSession "username" -- msu <- lookupSession "id"
userMedia <- runDB $ selectList [MediumOwner ==. username] [Desc MediumTime] -- case msu of
defaultLayout $ do -- Just tempUserId -> do
$(widgetFile "profile") -- userId <- Key $ PersistInt64 $ fromIntegral tempUserId
-- userMedia <- runDB $ selectList [MediumOwner ==. userId] [Desc MediumTime]
-- defaultLayout $ do
-- $(widgetFile "profile")

View file

@ -44,6 +44,7 @@ signupForm = renderDivs $ User
<$> areq textField "Username" Nothing <$> areq textField "Username" Nothing
<*> areq emailField "Email" Nothing <*> areq emailField "Email" Nothing
<*> areq passwordField "Password" Nothing <*> areq passwordField "Password" Nothing
<*> pure []
validateLen :: Text -> Bool validateLen :: Text -> Bool
validateLen a = validateLen a =

View file

@ -4,37 +4,44 @@ import Import as I
import Data.Time import Data.Time
import Data.Text import Data.Text
import System.FilePath import System.FilePath
import Database.Persist.Types
data TempMedium = TempMedium data TempMedium = TempMedium
{ tempMediumTitle :: Text { tempMediumTitle :: Text
, tempMediumFile :: FileInfo , tempMediumFile :: FileInfo
, tempMediumTime :: UTCTime , tempMediumTime :: UTCTime
, tempMediumOwner :: Text , tempMediumOwner :: UserId
, tempMediumDesc :: Textarea , tempMediumDesc :: Textarea
, tempMediumTags :: [Text] , tempMediumTags :: [Text]
} }
getUploadR :: Handler Html getUploadR :: Handler Html
getUploadR = do getUploadR = do
msu <- lookupSession "username" msu <- lookupSession "userId"
case msu of case msu of
Just username -> do Just tempUserId -> do
(uploadWidget, enctype) <- generateFormPost (uploadForm username) userId <- lift $ getUserIdFromText tempUserId
(uploadWidget, enctype) <- generateFormPost (uploadForm userId)
defaultLayout $ do defaultLayout $ do
$(widgetFile "upload") $(widgetFile "upload")
Nothing -> do Nothing -> do
setMessage $ [shamlet|<pre>You need to be logged in|] setMessage $ [shamlet|<pre>You need to be logged in|]
redirect $ LoginR redirect $ LoginR
--getUserIdFromText :: Text -> UserId
getUserIdFromText tempUserId =
Key $ PersistInt64 $ fromIntegral $ read $ unpack tempUserId
postUploadR :: Handler Html postUploadR :: Handler Html
postUploadR = do postUploadR = do
msu <- lookupSession "username" msu <- lookupSession "userId"
case msu of case msu of
Just username -> do Just tempUserId -> do
((result, uploadWidget), enctype) <- runFormPost (uploadForm username) userId <- lift $ getUserIdFromText tempUserId
((result, uploadWidget), enctype) <- runFormPost (uploadForm userId)
case result of case result of
FormSuccess temp -> do FormSuccess temp -> do
path <- writeOnDrive username $ tempMediumFile temp path <- writeOnDrive $ tempMediumFile temp
medium <- return $ Medium medium <- return $ Medium
(tempMediumTitle temp) (tempMediumTitle temp)
path path
@ -52,21 +59,26 @@ postUploadR = do
setMessage $ [shamlet|<pre>You need to be logged in|] setMessage $ [shamlet|<pre>You need to be logged in|]
redirect $ LoginR redirect $ LoginR
writeOnDrive :: Text -> FileInfo -> Handler FilePath writeOnDrive :: FileInfo -> Handler FilePath
writeOnDrive username file = do writeOnDrive file = do
filename <- return $ fileName file filename <- return $ fileName file
path <- return $ "static" </> (unpack filename) path <- return $ "static" </> (unpack filename)
liftIO $ fileMove file path liftIO $ fileMove file path
return path return path
uploadForm :: Text -> Form TempMedium uploadForm :: UserId -> Form TempMedium
uploadForm username = renderDivs $ TempMedium uploadForm userId = renderDivs $ TempMedium
<$> areq textField "Title" Nothing <$> areq textField "Title" Nothing
<*> areq fileField "Select file" Nothing <*> areq fileField "Select file" Nothing
<*> lift (liftIO getCurrentTime) <*> lift (liftIO getCurrentTime)
<*> pure username <*> pure userId
<*> areq textareaField "Description" Nothing <*> areq textareaField "Description" Nothing
<*> areq tagField "Enter tags" Nothing <*> areq tagField "Enter tags" Nothing
-- <*> areq (selectField albums) "Album" Nothing
-- where
-- albums :: Handler App App (OptionList AlbumId)
-- albums = do
-- runDB $ selectList [AlbumOwner ==. userId] [Desc AlbumTitle]
tagField :: Field Handler [Text] tagField :: Field Handler [Text]
tagField = Field tagField = Field

View file

@ -2,20 +2,25 @@ User
name Text name Text
email Text email Text
password Text password Text
albums [AlbumId]
deriving Typeable deriving Typeable
Token Token
token Text token Text
user User user User
deriving
Album Album
title Text title Text
owner Text owner [UserId]
content [MediumId] content [MediumId]
deriving
Medium Medium
title Text title Text
path FilePath path FilePath
time UTCTime time UTCTime
owner Text owner UserId
description Textarea description Textarea
tags [Text] tags [Text]
album AlbumId
deriving
-- By default this file is used in Model.hs (which is imported by Foundation.hs) -- By default this file is used in Model.hs (which is imported by Foundation.hs)

View file

@ -1,8 +1,8 @@
<nav> <nav>
<div id="top-nav"> <div id="top-nav">
<ul id="user-nav"> <ul id="user-nav">
$maybe user <- msu $maybe userId <- msu
Logged in as #{fromJust msu} Logged in as #{userId}
<li> <li>
<a href=@{LogoutR}>Logout <a href=@{LogoutR}>Logout
<li> <li>