Prechádzať zdrojové kódy

Working on templates func

Andrea Fazzi 5 rokov pred
rodič
commit
f739a43e6d

+ 1 - 1
Dockerfile

@@ -1,4 +1,4 @@
-FROM golang
+FROM golang:latest
 
 RUN apt-get update -qq && apt-get install -y netcat
 RUN apt-get install -y -q libsm6 libcups2 libcairo2 libdbus-1-3 libxinerama1

+ 2 - 0
Makefile

@@ -15,6 +15,8 @@ run_with_docker:
 	curl -u admin:password http://localhost:8080/remote.php/webdav/Documents/Department/reference.odt -X PUT --data-binary @"./generator/generators/department/example_1/resources/reference.odt"
 
 run_outside_docker:
+	killall main || echo "Process was not running."
+	docker-compose -f compose/karmen/docker-compose_outside_docker.yml down
 	docker-compose -f compose/karmen/docker-compose_outside_docker.yml up -d db
 	go run main.go --config=config/config_outside_docker.yaml &
 

+ 1 - 1
main.go

@@ -37,7 +37,7 @@ var (
 )
 
 func main() {
-	configFile := flag.String("config", "Load the given config file", "config/config.yaml")
+	configFile := flag.String("config", "config/config.yaml", "Load the given config file")
 	flag.Parse()
 
 	log.Println("Loading config file...")

+ 11 - 11
orm/teacher.go

@@ -227,6 +227,17 @@ func (t *Teacher) Update(args map[string]string, r *http.Request) (interface{},
 	}
 }
 
+func (t *Teacher) Delete(args map[string]string, r *http.Request) (interface{}, error) {
+	teacher, err := t.Read(args, nil)
+	if err != nil {
+		return nil, err
+	}
+	if err := DB().Unscoped().Delete(teacher.(*Teacher)).Error; err != nil {
+		return nil, err
+	}
+	return teacher.(*Teacher), nil
+}
+
 func SaveTeacher(teacher interface{}) (interface{}, error) {
 	if err := DB().Omit("Classes", "Subjects", "Activities").Save(teacher).Error; err != nil {
 		return nil, err
@@ -248,17 +259,6 @@ func CreateTeacher(teacher *Teacher) (*Teacher, error) {
 	return teacher, nil
 }
 
-func (t *Teacher) Delete(args map[string]string, r *http.Request) (interface{}, error) {
-	teacher, err := t.Read(args, nil)
-	if err != nil {
-		return nil, err
-	}
-	if err := DB().Unscoped().Delete(teacher.(*Teacher)).Error; err != nil {
-		return nil, err
-	}
-	return teacher.(*Teacher), nil
-}
-
 func (t *Teacher) GetClasses() ([]*Class, error) {
 	if err := DB().Raw(selectUniqueTeacherClasses, t.ID, t.ID).Scan(&t.Classes).Error; err != nil {
 		return nil, err

+ 45 - 1
renderer/funcmap.go

@@ -1,6 +1,7 @@
 package renderer
 
 import (
+	"errors"
 	"fmt"
 	"html/template"
 	"net/url"
@@ -9,6 +10,7 @@ import (
 	"time"
 
 	"github.com/jinzhu/inflection"
+	yml "gopkg.in/yaml.v2"
 )
 
 var (
@@ -16,10 +18,36 @@ var (
 		"query":       query,
 		"convertDate": convertDate,
 		"modelPath":   modelPath,
-		"slice":       slice,
+		"dict":        dict,
+		"yaml":        yaml,
+		"create":      create,
 	}
 )
 
+func yaml(content string) (interface{}, error) {
+	var result interface{}
+	err := yml.Unmarshal([]byte(content), &result)
+	if err != nil {
+		return nil, err
+	}
+	return result, nil
+}
+
+func dict(values ...interface{}) (map[string]interface{}, error) {
+	if len(values)%2 != 0 {
+		return nil, errors.New("invalid dict call")
+	}
+	dict := make(map[string]interface{}, len(values)/2)
+	for i := 0; i < len(values); i += 2 {
+		key, ok := values[i].(string)
+		if !ok {
+			return nil, errors.New("dict keys must be strings")
+		}
+		dict[key] = values[i+1]
+	}
+	return dict, nil
+}
+
 func slice(values ...string) []string {
 	var result []string
 	result = append(result, values...)
@@ -76,3 +104,19 @@ func modelPath(model string, action string, id uint) string {
 	q = query("tpl_layout", "base", "tpl_content", plural)
 	return fmt.Sprintf("/%s?%s", plural, q)
 }
+
+func create(model string) string {
+	action := "create"
+	plural := inflection.Plural(strings.ToLower(model))
+	q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_add_update", plural))
+
+	return fmt.Sprintf("/%s/%s/?%s", plural, action, q)
+}
+
+func show(model string, id uint) string {
+	action := "show"
+	plural := inflection.Plural(strings.ToLower(model))
+	q := query("tpl_layout", "base", "tpl_content", fmt.Sprintf("%s_%s", plural, action))
+
+	return fmt.Sprintf("/%s/%d?%s", plural, id, q)
+}

+ 2 - 2
templates/layout/create_button.html.tpl

@@ -1,6 +1,6 @@
 {{define "create_button"}}
-<a href="{{index . 1}}" class="btn btn-primary">
+<a href="{{.modelPath}}" class="btn btn-primary">
   <span class="fa fa-plus-circle" aria-hidden="true"></span>
-  {{index . 0}}
+  {{.buttonTitle}}
 </a>
 {{end}}

+ 14 - 0
templates/layout/read_all_header.html.tpl

@@ -0,0 +1,14 @@
+{{define "read_all_header"}}
+<div class="karmen-info-header">
+  <div class="row">
+    <div class="col-md-8">
+      <h1>{{.options.title}} ({{.lengthData}})</h1>
+    </div>
+    <div class="col-md-4">
+      <div class="btn-group float-right">
+        {{template "create_button" dict "buttonTitle" .options.buttonTitle "modelPath" .modelPath}}
+      </div>
+    </div>
+  </div>
+</div>
+{{end}}

+ 9 - 0
templates/layout/small.html.tpl

@@ -0,0 +1,9 @@
+{{define "small"}}
+{{if .}}
+{{range $el := .}}
+<small>{{$el.Name}}</small>
+{{end}}
+{{else}}
+<small>.options.noElements</small>
+{{end}}
+{{end}}

+ 8 - 17
templates/teachers.html.tpl

@@ -1,22 +1,13 @@
 {{ define "content" }}
 
-<div class="container">
-  
-  <div class="karmen-info-header">
-    
-    <div class="row">
-      <div class="col-md-8">
-	<h1>Docenti ({{len .Data}})</h1>
-      </div>
-      <div class="col-md-4">
-	<div class="btn-group float-right">
-	  {{template "create_button" slice "Crea nuovo docente" (modelPath "teacher" "create" 0)}}
-        </div>
-      </div>
-    </div>
-    
-  </div>
+<div class="container">  
 
+  {{$options := `
+  title: "Docenti"
+  buttonTitle: "Crea nuovo docente"
+  `}}
+  
+  {{template "read_all_header" dict  "options" ($options | yaml) "lengthData" (len .Data) "modelPath" (create "teacher")}}
   {{template "search_input"}}
     
   {{if not .}}
@@ -24,7 +15,7 @@
   {{else}}
   <div class="list-group" id="myUL">
     {{range $teacher := .Data}}
-    <a class="list-group-item list-group-item-action" href={{$teacher.ID|modelPath "teacher" "show"}}>
+    <a class="list-group-item list-group-item-action" href={{$teacher.ID|show "teacher"}}>
       <span class="fa fa-user"></span>
       {{$teacher.Surname}} {{$teacher.Name}}
       <div class="text-right">