ctucx.git: oeffi-web

[nimlang] oeffisearch fork that works without javascript

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
import json, asyncdispatch, options, strutils
import nimhafas
import ../asyncWebServer
import ../utils, ../translations, ../types, ../cache_types, ../cache
import ../formaters/renderJourneys

proc journeysHandler* (request: Request, server: ServerRef) {.async.} = 
  let urlParts      = request.url.path.split("/")
  let requestParams = parseQuery(request.url.query)
  var headers       = newHttpHeaders()
  var lang          = "de"
  var cacheObj:       CacheObject

  try:
    languageHandler()

    if not cacheExists(urlParts[1]): raise newException(notFoundException, "REQUEST_NOT_FOUND")
    cacheObj = getCacheObject(urlParts[1])

    if requestParams.hasKey("more"):
      var params = cacheObj.params
      let mode   = parseEnum[moreJourneysMode](requestParams["more"])

      if mode != later:
        params.earlierRef = some(cacheObj.earlierRef)
      else:
        params.laterRef   = some(cacheObj.laterRef)

      cacheObj = await updateJourneys(urlParts[1], mode, (await journeys(params)))

    var templateContext = renderJourneys(cacheObj)

    if not requestParams.hasKey("json"):
      await request.respTemplate(Http200, "journeys", templateContext, lang, headers)
    else:
      await request.respondJson(Http200, "success", "", templateContext)

  except notFoundException:
    if not requestParams.hasKey("json"):
      await request.respError(Http404, "Nichts gefunden!", "Für diese Anfrage gibts nix.")
    else:
      await request.respondJson(Http404, "error", getCurrentExceptionMsg().split("\n")[0], newJObject())