Compare commits

...

2 commits

24 changed files with 221 additions and 208 deletions

View file

@ -2,7 +2,6 @@ package controllers
import (
"fmt"
"log"
"net/http"
"net/mail"
"officeHours/config"
@ -149,12 +148,12 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
id, err := b.officeHourRepo.Add(officeHour)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Templates.ExecuteTemplate(w, "addFailure.html", err)
b.serveTemplate(w, "addFailure", err)
return
}
officeHour, _ = b.officeHourRepo.FindById(id)
b.requestRepo.Add(officeHour, models.RequestActivate)
Templates.ExecuteTemplate(w, "addSuccess.html", struct{}{})
b.serveTemplate(w, "addSuccess", nil)
}
}
@ -165,10 +164,5 @@ func (b *BaseHandler) writeAddOfficeHourMask(w http.ResponseWriter, req *http.Re
if len(data.Errors) != 0 {
w.WriteHeader(http.StatusBadRequest)
}
err := Templates.ExecuteTemplate(w, "addMask.html", data)
if err != nil {
log.Printf("Template addMask.html could not be parsed: %s", err.Error())
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
return
}
b.serveTemplate(w, "addMask", data)
}

View file

@ -9,17 +9,15 @@ func (b *BaseHandler) ConfirmRequestHandler(w http.ResponseWriter, req *http.Req
request, err := b.requestRepo.FindBySecret(secret)
if err != nil {
w.WriteHeader(http.StatusNotFound)
Templates.ExecuteTemplate(w, "requestNotFound.html", struct{}{})
b.serveTemplate(w, "requestNotFound", nil)
return
}
err = b.requestRepo.Execute(request)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Templates.ExecuteTemplate(w, "executeFailure.html", err.Error())
b.serveTemplate(w, "executeFailure", err.Error())
return
}
Templates.ExecuteTemplate(w, "executeSuccess.html", struct{}{})
b.serveTemplate(w, "executeSuccess", nil)
}

View file

@ -18,7 +18,7 @@ func (b *BaseHandler) DeleteOfficeHourHandler(w http.ResponseWriter, req *http.R
w.WriteHeader(http.StatusBadRequest)
}
_, err = b.requestRepo.Add(officeHour, models.RequestDelete)
Templates.ExecuteTemplate(w, "deleteSuccess.html", struct{}{})
b.serveTemplate(w, "deleteSuccess", nil)
} else {
officeHours, _ := b.officeHourRepo.GetAll(true)
timetable, slots := b.GetTimetable(officeHours)

View file

@ -1,7 +1,6 @@
package controllers
import (
"fmt"
"html/template"
"net/http"
"officeHours/models"
@ -28,6 +27,7 @@ func (b *BaseHandler) GetByCourseHandler(w http.ResponseWriter, req *http.Reques
courseid, err := strconv.Atoi(req.FormValue("veranstaltung"))
if err != nil {
b.RootHandler(w, req)
return
}
course, err := b.courseRepo.FindById(courseid)
if err != nil {
@ -51,9 +51,5 @@ func (b *BaseHandler) writeTimetablePage(w http.ResponseWriter, req *http.Reques
SelectedRoom int
SelectedCourse int
}{courses, rooms, timetable, selectedRoom, selectedCourse}
err := Templates.ExecuteTemplate(w, "index.html", data)
if err != nil {
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
return
}
b.serveTemplate(w, "index", data)
}

View file

@ -1,21 +1,47 @@
package controllers
import (
"fmt"
"html/template"
"net/http"
"officeHours/models"
"os"
)
var Templates, _ = template.Must(template.ParseFiles(
"templates/addFailure.html",
"templates/addMask.html",
"templates/addSuccess.html",
"templates/deleteSuccess.html",
"templates/executeFailure.html",
"templates/executeSuccess.html",
"templates/footer.html",
"templates/head.html",
"templates/index.html",
"templates/officeHourTable.html",
"templates/requestNotFound.html")).
New("").Funcs(template.FuncMap{"DayName": models.DayName,
"divide": func(i int, j int) int { return i / j }}).ParseFiles("templates/confirmationMail", "templates/td.html")
var funcs = template.FuncMap{
"DayName": models.DayName,
"divide": func(i int, j int) int { return i / j },
}
var baseTemplate = template.Must(template.ParseFiles("templates/base.html")).New("base.html").Funcs(funcs)
func (b *BaseHandler) serveTemplate(w http.ResponseWriter, name string, data any) {
full_name := "templates/" + name + ".html"
// check that template exists
info, err := os.Stat(full_name)
if (err != nil && os.IsNotExist(err)) || info.IsDir() {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404: Template nicht gefunden."))
return
}
tmpl, err := baseTemplate.ParseFiles(full_name)
if err != nil {
// TODO: log.Printf
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500: Template "+full_name+" konnte nicht geparst werden : %s", err.Error())))
return
}
// TODO: cache templates in parsed state, but not executed
err = template.Must(tmpl.Clone()).Execute(w, data)
if err != nil {
// TODO: log.Printf
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("500: Template "+full_name+" konnte nicht ausgeführt werden : %s", err.Error())))
return
}
}
var RawTemplates = template.Must(template.New("").Funcs(funcs).ParseFiles(
"templates/confirmationMail",
"templates/td.html",
"templates/officeHourTable.html"))

View file

@ -84,7 +84,7 @@ func (b *BaseHandler) printTimetable(timetable map[models.Date]map[int]models.Of
MinuteGranularity int
DeleteIcons bool
}{OfficeHour: current, MinuteGranularity: b.config.Date.MinuteGranularity, DeleteIcons: deleteIcons}
Templates.ExecuteTemplate(&celldata, "td.html", data)
RawTemplates.ExecuteTemplate(&celldata, "td.html", data)
tableBody += celldata.String()
}
} else {
@ -116,6 +116,6 @@ func (b *BaseHandler) printTimetable(timetable map[models.Date]map[int]models.Of
slots[4],
template.HTML(tableBody),
}
Templates.ExecuteTemplate(&table, "officeHourTable.html", tableData)
RawTemplates.ExecuteTemplate(&table, "officeHourTable.html", tableData)
return template.HTML(table.String())
}

View file

@ -7,11 +7,11 @@ import (
"log"
"log/syslog"
"net/http"
"os"
"officeHours/config"
"officeHours/controllers"
"officeHours/repositories"
"officeHours/sqldb"
"os"
)
func main() {
@ -35,6 +35,9 @@ func main() {
log.Fatalf("%s: %s", "Reading JSON config file into config structure", err)
}
// serve static files
staticHandler := http.FileServer(http.Dir("./static"))
db := sqldb.Connect(conf)
// Create repos
roomRepo := repositories.NewRoomRepo(db)
@ -50,6 +53,7 @@ func main() {
http.HandleFunc("/confirmRequest", h.ConfirmRequestHandler)
http.HandleFunc("/deleteOfficeHour", h.DeleteOfficeHourHandler)
http.HandleFunc("/", h.RootHandler)
http.Handle("/static/", http.StripPrefix("/static/", staticHandler))
err = http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Server.ListenAddress, conf.Server.ListenPort), nil)
log.Println(err.Error())

View file

@ -128,7 +128,7 @@ func (r *RequestRepo) sendConfirmationMail(request models.Request) error {
Config config.Config
Request models.Request
}{r.config, request}
err := controllers.Templates.ExecuteTemplate(&message, "confirmationMail", data)
err := controllers.RawTemplates.ExecuteTemplate(&message, "confirmationMail", data)
if err != nil {
log.Printf("Error parsing confirmation Mail: %s", err.Error())
return err

1
static/bootstrap Symbolic link
View file

@ -0,0 +1 @@
bootstrap-5.2.1-dist/

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,13 +1,7 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title>Sprechstunde anlegen</title>
{{template "head.html" .}}
</head>
<body>
{{define "title"}}Fehler{{end}}
{{define "content"}}
Irgendetwas ist schief gegangen. Bitte sende folgende Daten an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a> mit einer Beschreibung, was du tun wolltest.
<br />
{{.}}
{{template "footer.html" .}}
</body>
</html>
{{end}}

View file

@ -1,17 +1,15 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title>Sprechstunde anlegen</title>
{{template "head.html" .}}
</head>
<body>
<{{define "title"}}Sprechstunde anlegen{{end}}
{{define "content"}}
<p>
{{range .Errors}}{{.}}<br />{{end}}
</p>
<form method="POST" action="addOfficeHour">
<label for="veranstaltung">Veranstaltung</label>:
<select name="veranstaltung" id="veranstaltung">{{range $course := .Courses}}
<option value="{{$course.Id}}"{{if eq $course.Id $.SelectedCourse}} selected{{end}}>{{$course.Name}}</option>{{end}}
<select name="veranstaltung" id="veranstaltung">
{{range $course := .Courses}}
<option value="{{$course.Id}}"{{if eq $course.Id $.SelectedCourse}} selected{{end}}>{{$course.Name}}</option>
{{end}}
</select><br />
<label for="woche">Woche</label>:
<select name="woche" id="woche">
@ -29,8 +27,10 @@
<label for="startzeit">Startzeit</label>: <input type="time" name="startzeit" id="startzeit" min="08:00" max="17:30" {{if gt $.Date.Hour 7}}value="{{printf "%02d" $.Date.Hour}}:{{printf "%02d" $.Date.Minute}}"{{end}} required/><br />
<label for="dauer">Dauer in Minuten</label>: <input name="dauer" id="dauer" type="number" min="{{.MinuteGranularity}}" max="120" step="{{.MinuteGranularity}}" value="{{.Duration}}" required/><br />
<label for="raum">Raum</label>:
<select name="raum" id="raum">{{range $room := .Rooms}}
<option value="{{$room.Id}}"{{if eq $room.Id $.SelectedRoom}} selected{{end}}>{{$room.Name}}</option>{{end}}
<select name="raum" id="raum">
{{range $room := .Rooms}}
<option value="{{$room.Id}}"{{if eq $room.Id $.SelectedRoom}} selected{{end}}>{{$room.Name}}</option>
{{end}}
</select><br />
<label for="raumname">Raumname (für Sonderräume)</label>: <input type="text" name="raumname" id="raumname" value="{{.Roomname}}"/><br />
<label for="name">Name</label>: <input name="name" id="name" type="text" size="50" value="{{.Name}}" required/><br />
@ -39,12 +39,14 @@
<label for="info">Info</label>: <input name="info" id="info" type="text" size="50" value="{{.Info}}"/><br />
<input type="submit">
</form>
{{if ne .Config.Tutor.MailSuffix ""}}Du musst hier eine Email-Adresse angeben, die auf „{{.Config.Tutor.MailSuffix}}“ endet.<br />{{end}}
{{if ne .Config.Tutor.MailSuffix ""}}
Du musst hier eine Email-Adresse angeben, die auf „{{.Config.Tutor.MailSuffix}}“ endet.<br />
{{end}}
Außerdem dürfen in Räumen nur begrenzt viele Sprechstunden gleichzeitig stattfinden, nämlich
<dl>
{{range $room := .Rooms}}
<dt>{{$room.Name}}</dt><dd>{{$room.MaxOccupy}} Sprechstunde{{if gt $room.MaxOccupy 1}}n{{end}}</dd>{{end}}
<dt>{{$room.Name}}</dt>
<dd>{{$room.MaxOccupy}} Sprechstunde{{if gt $room.MaxOccupy 1}}n{{end}}</dd>
{{end}}
</dl>
{{template "footer.html" .}}
</body>
</html>
{{end}}

View file

@ -1,12 +1,5 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title>Sprechstunde anlegen</title>
{{template "head.html" .}}
</head>
<body>
{{define "title"}}Sprechstunde anlegen{{end}}
{{define "content"}}
Die Sprechstunde wurde angelegt. Du solltest eine Mail mit einem Aktivierungslink erhalten haben.
<br />
{{template "footer.html" .}}
</body>
</html>
{{end}}

27
templates/base.html Normal file
View file

@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="Mathebau, Sprechstunde, Sprechstunden, Mathe, Mathematik, technische, Universität, Darmstadt, TU, Fachschaft">
<meta name="description" content="Eine Übersicht der Sprechstunden, die in den offenen Arbeitsräumen der Fachschaft Mathematik, TU Darmstadt, angeboten werden">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
<title>{{block "title" .}}Start{{end}} Sprechstunden</title>
</head>
<body>
<div class="container">
{{block "content" .}}Du solltest dies nicht sehen.{{end}}
</div>
<footer class="container">
<a href="/">Startseite</a><br />
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
<a href="/deleteOfficeHour">Sprechstunde löschen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
<script src="/static/bootstrap/js/bootstrap.bundle.min.js"></script>
</body>
</html>

View file

@ -1,13 +1,7 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title>Sprechstunde löschen</title>
{{template "head.html" .}}
</head>
<body>
{{define "title"}}Sprechstunde löschen{{end}}
{{define "content"}}
Du solltest eine Mail mit einem Bestätigungslink erhalten haben. <br />
Sie wurde an die Adresse geschickt, mit der die Sprechstunde angelegt wurde.
<br />
{{template "footer.html" .}}
</body>
</html>
{{end}}

View file

@ -1,13 +1,7 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title>Anfrage ausführen fehlgeschlagen</title>
{{template "head.html" .}}
</head>
<body>
{{define "title"}}Anfrage ausführen fehlgeschlagen{{end}}
{{define "content"}}
Irgendetwas ist schief gegangen. Bitte sende folgende Daten an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a> mit einer Beschreibung, was du tun wolltest.
<br />
{{.}}
{{template "footer.html" .}}
</body>
</html>
{{end}}

View file

@ -1,11 +1,5 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title>Anfrage ausgeführt</title>
{{template "head.html" .}}
</head>
<body>
Deine Anfrage wurde ausgeführt. <br />
{{template "footer.html" .}}
</body>
</html>
{{define "title"}}Anfrage ausgeführt{{end}}
{{define "content"}}
Deine Anfrage wurde ausgeführt.
{{end}}

View file

@ -1,6 +0,0 @@
<footer>
<a href="/">Startseite</a><br />
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
<a href="/deleteOfficeHour">Sprechstunde löschen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>

View file

@ -1,4 +0,0 @@
<meta charset="UTF-8">
<meta name="keywords" content="Mathebau, Sprechstunde, Sprechstunden, Mathe, Mathematik, technische, Universität, Darmstadt, TU, Fachschaft">
<meta name="description" content="Eine Übersicht der Sprechstunden, die in den offenen Arbeitsräumen der Fachschaft Mathematik, TU Darmstadt, angeboten werden">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

View file

@ -1,16 +1,13 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title>Sprechstunden</title>
{{template "head.html" .}}
</head>
<body>
{{define "title"}}Übersicht{{end}}
{{define "content"}}
<form method="GET" action="/getByCourse">
<label for="veranstaltung">Veranstaltung: </label>
<select name="veranstaltung" id="veranstaltung" size="1" onchange="document.forms[0].submit()">
<option value="">Alle</option>
{{range $course := .Courses}}
<option value="{{$course.Id}}"{{if eq $course.Id $.SelectedCourse}} selected{{end}}>{{$course.Name}}</option>{{end}}
<option value="{{$course.Id}}"{{if eq $course.Id $.SelectedCourse}} selected{{end}}>{{$course.Name}}</option>
{{end}}
</select>
<input type="submit" value="Auswählen" />
</form>
@ -19,11 +16,10 @@
<select name="raum" id="raum" size="1" onchange="document.forms[1].submit()">
<option value="">Alle</option>
{{range $room := .Rooms}}
<option value="{{$room.Id}}"{{if eq $room.Id $.SelectedRoom}} selected{{end}}>{{$room.Name}}</option>{{end}}
<option value="{{$room.Id}}"{{if eq $room.Id $.SelectedRoom}} selected{{end}}>{{$room.Name}}</option>
{{end}}
</select>
<input type="submit" value="Auswählen" />
</form>
{{.Timetable}}
{{template "footer.html" .}}
</body>
</html>
{{end}}

View file

@ -1,10 +1,6 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title>Anfrage bestätigen fehlgeschlagen</title>
{{template "head.html" .}}
</head>
<body>
{{define "title"}}Anfrage bestätigen fehlgeschlagen{{end}}
{{define "content"}}
<p>
Dieser Bestätigungscode ist nicht verfügbar. <br />
Bitte gib deinen Bestätigungscode hier ein.
@ -13,6 +9,4 @@
<label for="code">Bestätigungscode</label>: <input type="text" name="code" id="code"/>
<input type="submit" />
</form>
{{template "footer.html" .}}
</body>
</html>
{{end}}