lot of new functionalities
This commit is contained in:
@@ -5,12 +5,13 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"math"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
||||
validatedprofile "git.coopgo.io/coopgo-apps/parcoursmob/utils/validated-profile"
|
||||
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/core/utils/storage"
|
||||
validatedprofile "git.coopgo.io/coopgo-apps/parcoursmob/core/utils/validated-profile"
|
||||
groupsstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
@@ -151,6 +152,24 @@ func strval(v interface{}) string {
|
||||
}
|
||||
}
|
||||
|
||||
// JSEscape escapes a string for safe use in JavaScript
|
||||
func JSEscape(s string) template.JS {
|
||||
return template.JS(template.JSEscapeString(s))
|
||||
}
|
||||
|
||||
// IsGuaranteedTripMotivation checks if a motivation is a guaranteed trip
|
||||
func IsGuaranteedTripMotivation(globalConfig *viper.Viper) func(string) bool {
|
||||
return func(motivation string) bool {
|
||||
guaranteedMotivations := globalConfig.GetStringSlice("modules.solidarity_transport.guaranteed_trip_motivations")
|
||||
for _, m := range guaranteedMotivations {
|
||||
if m == motivation {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// GetTemplateFuncMap returns the common template functions for rendering
|
||||
func GetTemplateFuncMap(group groupsstorage.Group, globalConfig *viper.Viper, fileStorage filestorage.FileStorage) template.FuncMap {
|
||||
return template.FuncMap{
|
||||
@@ -162,14 +181,17 @@ func GetTemplateFuncMap(group groupsstorage.Group, globalConfig *viper.Viper, fi
|
||||
"json": JSON,
|
||||
"rawjson": RawJSON,
|
||||
"unescapeHTML": UnescapeHTML,
|
||||
"jsEscape": JSEscape,
|
||||
"walkingLength": WalkingLength,
|
||||
"divideFloat64": Divide[float64],
|
||||
"divideInt": Divide[int],
|
||||
"typeOf": reflect.TypeOf,
|
||||
"shortDuration": ShortDuration,
|
||||
"round2": Round2,
|
||||
"beneficiaryValidatedProfile": validatedprofile.ValidateProfile(globalConfig.Sub("modules.beneficiaries.validated_profile")),
|
||||
"solidarityDriverValidatedProfile": validatedprofile.ValidateProfile(globalConfig.Sub("modules.solidarity_transport.drivers.validated_profile")),
|
||||
"carpoolDriverValidatedProfile": validatedprofile.ValidateProfile(globalConfig.Sub("modules.organized_carpool.drivers.validated_profile")),
|
||||
"isGuaranteedTripMotivation": IsGuaranteedTripMotivation(globalConfig),
|
||||
"beneficiaryDocuments": func(id string) []filestorage.FileInfo {
|
||||
return fileStorage.List(filestorage.PREFIX_BENEFICIARIES + "/" + id)
|
||||
},
|
||||
@@ -211,3 +233,25 @@ func ShortDuration(d interface{}) string {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Round2 rounds a float64 to 2 decimal places to avoid floating point issues
|
||||
func Round2(value interface{}) float64 {
|
||||
var f float64
|
||||
switch v := value.(type) {
|
||||
case float64:
|
||||
f = v
|
||||
case *float64:
|
||||
if v != nil {
|
||||
f = *v
|
||||
}
|
||||
case float32:
|
||||
f = float64(v)
|
||||
case int:
|
||||
f = float64(v)
|
||||
case int64:
|
||||
f = float64(v)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
return math.Round(f*100) / 100
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user