proper handling of pure whitespace barcodes.

This commit is contained in:
nek0 2015-07-21 18:56:24 +02:00
parent 323cfddb90
commit 4e3c496845
4 changed files with 42 additions and 42 deletions

View file

@ -7,6 +7,7 @@ import qualified Data.List as L
import qualified Data.Text.Lazy.Encoding as E import qualified Data.Text.Lazy.Encoding as E
import qualified Data.Text.Read as R import qualified Data.Text.Read as R
import Data.Maybe import Data.Maybe
import qualified Data.Char as C
import Yesod.Form.Functions import Yesod.Form.Functions
import Text.Shakespeare.Text import Text.Shakespeare.Text
import Network.Mail.Mime import Network.Mail.Mime
@ -93,53 +94,52 @@ handleBarcodes :: Either UserId BeverageId -> [Text] -> Handler ()
handleBarcodes (Left uId) nbs = do handleBarcodes (Left uId) nbs = do
raws <- runDB $ selectList [BarcodeUser ==. Just uId] [] raws <- runDB $ selectList [BarcodeUser ==. Just uId] []
obs <- return $ map (barcodeCode . entityVal) raws obs <- return $ map (barcodeCode . entityVal) raws
case length nbs > length obs of toDel <- return $ obs L.\\ nbs
True -> do toAdd <- return $ nbs L.\\ obs
sect <- return $ nbs L.\\ obs _ <- mapM (\b -> runDB $ insert_ $ Barcode
_ <- mapM (\b -> runDB $ insert $ Barcode
b b
True True
(Just uId) (Just uId)
Nothing Nothing
) sect ) toAdd
return () ents <- mapM (runDB . getBy . UniqueBarcode) toDel
False -> do
sect <- return $ obs L.\\ nbs
ents <- mapM (runDB . getBy . UniqueBarcode) sect
mapM_ (runDB . delete . entityKey . fromJust) ents mapM_ (runDB . delete . entityKey . fromJust) ents
handleBarcodes (Right bId) nbs = do handleBarcodes (Right bId) nbs = do
raws <- runDB $ selectList [BarcodeBev ==. Just bId] [] raws <- runDB $ selectList [BarcodeBev ==. Just bId] []
obs <- return $ map (barcodeCode . entityVal) raws obs <- return $ map (barcodeCode . entityVal) raws
case length nbs > length obs of toDel <- return $ obs L.\\ nbs
True -> do toAdd <- return $ nbs L.\\ obs
sect <- return $ nbs L.\\ obs
_ <- mapM (\b -> runDB $ insert $ Barcode _ <- mapM (\b -> runDB $ insert $ Barcode
b b
False False
Nothing Nothing
(Just bId) (Just bId)
) sect ) toAdd
return () ents <- mapM (runDB . getBy . UniqueBarcode) toDel
False -> do
sect <- return $ obs L.\\ nbs
ents <- mapM (runDB . getBy . UniqueBarcode) sect
mapM_ (runDB . delete . entityKey . fromJust) ents mapM_ (runDB . delete . entityKey . fromJust) ents
handleGetParam :: Maybe Text -> Either UserId BeverageId -> Handler () handleGetParam :: Maybe Text -> Either UserId BeverageId -> Handler ()
handleGetParam Nothing _ = handleGetParam Nothing _ =
return () return ()
handleGetParam (Just b) eub = do handleGetParam (Just b) eub = do
e <- runDB $ getBy $ UniqueBarcode b f <- return $ T.filter C.isAlphaNum b
case (T.length f) > 0 of
True -> do
e <- runDB $ getBy $ UniqueBarcode f
case e of case e of
Nothing -> do Nothing -> do
_ <- case eub of _ <- case eub of
Left uId -> do Left uId -> do
-- should usernames containing, among other, spaces cause problems, replace b for f here
runDB $ insert_ $ Barcode b True (Just uId) Nothing runDB $ insert_ $ Barcode b True (Just uId) Nothing
Right bId -> do Right bId -> do
-- and here
runDB $ insert_ $ Barcode b False Nothing (Just bId) runDB $ insert_ $ Barcode b False Nothing (Just bId)
setMessageI MsgBarcodeAdded setMessageI MsgBarcodeAdded
Just _ -> Just _ ->
setMessageI MsgBarcodeDuplicate setMessageI MsgBarcodeDuplicate
False -> do
setMessageI MsgProvideBarcode
amountField :: (RenderMessage (HandlerSite m) FormMessage, Show a, Monad m, Integral a) => Field m a amountField :: (RenderMessage (HandlerSite m) FormMessage, Show a, Monad m, Integral a) => Field m a
amountField = Field amountField = Field

View file

@ -102,7 +102,7 @@ AvatarInUseError: Avatar nelze smazat dokud je v užitku
Volume: Objem v l Volume: Objem v l
CorrectedAmount: Stráta CorrectedAmount: Stráta
BarcodeNotUser: Tento kód není pro uživatele, ale pro produkt BarcodeNotUser: Tento kód není pro uživatele, ale pro produkt
ProvideBarcode: Prosím zadej kód ProvideBarcode: Prosím zadej správný kód
BarcodeNotBev: Tento kód není pro produkt, ale pro uživatele BarcodeNotBev: Tento kód není pro produkt, ale pro uživatele
BarcodeUnknown: Tento kód je neznámý BarcodeUnknown: Tento kód je neznámý
BarcodeField: Čárový kódy BarcodeField: Čárový kódy

View file

@ -102,7 +102,7 @@ AvatarInUseError: Avatare können nicht gelöscht werden, solange sie in Benutzu
Volume: Volumen in l Volume: Volumen in l
CorrectedAmount: Schwund CorrectedAmount: Schwund
BarcodeNotUser: Dieser Barcode ist keinem Benutzer zugeordnet, sondern einem Produkt BarcodeNotUser: Dieser Barcode ist keinem Benutzer zugeordnet, sondern einem Produkt
ProvideBarcode: Bitte Barcode angeben ProvideBarcode: Bitte korrekten Barcode angeben
BarcodeNotBev: Dieser Barcode ist keinem Produkt zugeordnet, sondern einem Benutzer BarcodeNotBev: Dieser Barcode ist keinem Produkt zugeordnet, sondern einem Benutzer
BarcodeUnknown: Dieser Barcode ist unbekannt BarcodeUnknown: Dieser Barcode ist unbekannt
BarcodeField: Barcodes BarcodeField: Barcodes

View file

@ -102,7 +102,7 @@ AvatarInUseError: You can not delete avatars that are currently in use
Volume: Volume in l Volume: Volume in l
CorrectedAmount: Loss CorrectedAmount: Loss
BarcodeNotUser: This barcode is no user, but a product BarcodeNotUser: This barcode is no user, but a product
ProvideBarcode: Please provide a barcode ProvideBarcode: Please provide a correct barcode
BarcodeNotBev: This barcode is no product, but a user BarcodeNotBev: This barcode is no product, but a user
BarcodeUnknown: This barcode is unknown BarcodeUnknown: This barcode is unknown
BarcodeField: Barcodes BarcodeField: Barcodes