refactor func maps
This commit is contained in:
@@ -5,9 +5,12 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
||||
validatedprofile "git.coopgo.io/coopgo-apps/parcoursmob/utils/validated-profile"
|
||||
groupsstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
@@ -148,6 +151,37 @@ func strval(v interface{}) string {
|
||||
}
|
||||
}
|
||||
|
||||
// GetTemplateFuncMap returns the common template functions for rendering
|
||||
func GetTemplateFuncMap(group groupsstorage.Group, globalConfig *viper.Viper, fileStorage filestorage.FileStorage) template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"moduleAvailable": ModuleAvailable(group, globalConfig),
|
||||
"timeFrom": TimeFrom,
|
||||
"timeFormat": TimeFormat,
|
||||
"genderISO5218": GenderISO5218,
|
||||
"dict": Dict,
|
||||
"json": JSON,
|
||||
"rawjson": RawJSON,
|
||||
"unescapeHTML": UnescapeHTML,
|
||||
"walkingLength": WalkingLength,
|
||||
"divideFloat64": Divide[float64],
|
||||
"divideInt": Divide[int],
|
||||
"typeOf": reflect.TypeOf,
|
||||
"shortDuration": ShortDuration,
|
||||
"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")),
|
||||
"beneficiaryDocuments": func(id string) []filestorage.FileInfo {
|
||||
return fileStorage.List(filestorage.PREFIX_BENEFICIARIES + "/" + id)
|
||||
},
|
||||
"solidarityDocuments": func(id string) []filestorage.FileInfo {
|
||||
return fileStorage.List(filestorage.PREFIX_SOLIDARITY_TRANSPORT_DRIVERS + "/" + id)
|
||||
},
|
||||
"carpoolDocuments": func(id string) []filestorage.FileInfo {
|
||||
return fileStorage.List(filestorage.PREFIX_ORGANIZED_CARPOOL_DRIVERS + "/" + id)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func Divide[V int | float64](a, b V) V {
|
||||
return a / b
|
||||
}
|
||||
|
||||
@@ -4,13 +4,10 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/icons"
|
||||
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
|
||||
cache "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
||||
filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
||||
validatedprofile "git.coopgo.io/coopgo-apps/parcoursmob/utils/validated-profile"
|
||||
"git.coopgo.io/coopgo-platform/emailing"
|
||||
"git.coopgo.io/coopgo-platform/groups-management/storage"
|
||||
"github.com/coreos/go-oidc/v3/oidc"
|
||||
@@ -54,38 +51,7 @@ func (renderer *Renderer) Render(name string, w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
t := template.New(name).Funcs(
|
||||
template.FuncMap{
|
||||
"moduleAvailable": ModuleAvailable(state.Group, renderer.GlobalConfig),
|
||||
"timeFrom": TimeFrom,
|
||||
"timeFormat": TimeFormat,
|
||||
"genderISO5218": GenderISO5218,
|
||||
"dict": Dict,
|
||||
"json": JSON,
|
||||
"rawjson": RawJSON,
|
||||
"unescapeHTML": UnescapeHTML,
|
||||
"walkingLength": WalkingLength,
|
||||
"divideFloat64": Divide[float64],
|
||||
"divideInt": Divide[int],
|
||||
"typeOf": reflect.TypeOf,
|
||||
"shortDuration": ShortDuration,
|
||||
"beneficiaryValidatedProfile": validatedprofile.ValidateProfile(renderer.GlobalConfig.Sub("modules.beneficiaries.validated_profile")),
|
||||
"solidarityDriverValidatedProfile": validatedprofile.ValidateProfile(renderer.GlobalConfig.Sub("modules.solidarity_transport.drivers.validated_profile")),
|
||||
"carpoolDriverValidatedProfile": validatedprofile.ValidateProfile(renderer.GlobalConfig.Sub("modules.organized_carpool.drivers.validated_profile")),
|
||||
"beneficiaryDocuments": func(id string) []filestorage.FileInfo {
|
||||
documents := renderer.FileStorage.List(filestorage.PREFIX_BENEFICIARIES + "/" + id)
|
||||
return documents
|
||||
},
|
||||
"solidarityDocuments": func(id string) []filestorage.FileInfo {
|
||||
documents := renderer.FileStorage.List(filestorage.PREFIX_SOLIDARITY_TRANSPORT_DRIVERS + "/" + id)
|
||||
return documents
|
||||
},
|
||||
"carpoolDocuments": func(id string) []filestorage.FileInfo {
|
||||
documents := renderer.FileStorage.List(filestorage.PREFIX_ORGANIZED_CARPOOL_DRIVERS + "/" + id)
|
||||
return documents
|
||||
},
|
||||
},
|
||||
)
|
||||
t := template.New(name).Funcs(GetTemplateFuncMap(state.Group, renderer.GlobalConfig, renderer.FileStorage))
|
||||
t = template.Must(t.ParseFiles(prefixed_files...))
|
||||
|
||||
err := t.ExecuteTemplate(w, "main", state)
|
||||
@@ -101,36 +67,7 @@ func (renderer *Renderer) RenderNoLayout(name string, w http.ResponseWriter, r *
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
t := template.New(name).Funcs(
|
||||
template.FuncMap{
|
||||
"timeFrom": TimeFrom,
|
||||
"timeFormat": TimeFormat,
|
||||
"genderISO5218": GenderISO5218,
|
||||
"dict": Dict,
|
||||
"json": JSON,
|
||||
"rawjson": RawJSON,
|
||||
"unsescapeHTML": UnescapeHTML,
|
||||
"divideFloat64": Divide[float64],
|
||||
"divideInt": Divide[int],
|
||||
"typeOf": reflect.TypeOf,
|
||||
"shortDuration": ShortDuration,
|
||||
"beneficiaryValidatedProfile": validatedprofile.ValidateProfile(renderer.GlobalConfig.Sub("modules.beneficiaries.validated_profile")),
|
||||
"solidarityDriverValidatedProfile": validatedprofile.ValidateProfile(renderer.GlobalConfig.Sub("modules.solidarity_transport.drivers.validated_profile")),
|
||||
"carpoolDriverValidatedProfile": validatedprofile.ValidateProfile(renderer.GlobalConfig.Sub("modules.organized_carpool.drivers.validated_profile")),
|
||||
"beneficiaryDocuments": func(id string) []filestorage.FileInfo {
|
||||
documents := renderer.FileStorage.List(filestorage.PREFIX_BENEFICIARIES + "/" + id)
|
||||
return documents
|
||||
},
|
||||
"solidarityDocuments": func(id string) []filestorage.FileInfo {
|
||||
documents := renderer.FileStorage.List(filestorage.PREFIX_SOLIDARITY_TRANSPORT_DRIVERS + "/" + id)
|
||||
return documents
|
||||
},
|
||||
"carpoolDocuments": func(id string) []filestorage.FileInfo {
|
||||
documents := renderer.FileStorage.List(filestorage.PREFIX_ORGANIZED_CARPOOL_DRIVERS + "/" + id)
|
||||
return documents
|
||||
},
|
||||
},
|
||||
)
|
||||
t := template.New(name).Funcs(GetTemplateFuncMap(state.Group, renderer.GlobalConfig, renderer.FileStorage))
|
||||
|
||||
t = template.Must(t.ParseFiles(prefixed_files...))
|
||||
err := t.ExecuteTemplate(w, "main", state)
|
||||
|
||||
Reference in New Issue
Block a user