Exemplo n.º 1
0
func defaultPage(params martini.Params, u *user.User, c context.Context, w http.ResponseWriter, r *http.Request) {

	rpkey := model.NewRootPageKey(c)
	var rp model.RootPage
	if err := ds.Get(c, rpkey, &rp); err != nil {
		if errors.Root(err) == datastore.ErrNoSuchEntity {
			login(c, w, r)
		} else {
			handleError(c, w, err, http.StatusInternalServerError)
		}
		return
	}

	renderPage(strconv.FormatInt(rp.PageID, 10), u, c, w, r, false)
}
Exemplo n.º 2
0
func queryPages(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	rpkey := model.NewRootPageKey(c)

	var rp model.RootPage
	err := ds.Get(c, rpkey, &rp)
	if err != nil && errors.Root(err) != datastore.ErrNoSuchEntity {
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}

	pages, err := getPages(c)
	if err != nil {
		log.Errorf(c, err.Error())
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}

	jsonPages := make([]*model.JsonPage, len(pages))

	for i, p := range pages {
		jsonPages[i] = model.NewJsonPage(p, nil, p.Key.IntID() == rp.PageID)
	}

	b, err := json.Marshal(jsonPages)
	if err != nil {
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	_, err = w.Write(b)
	if err != nil {
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}
	return
}
Exemplo n.º 3
0
func queryProperty(params martini.Params, w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	pn := params["name"]
	if pn != "" {
		// get property from datastore
		c := appengine.NewContext(r)
		gpageKey := model.NewGlobalPageKey(c)
		gpropKey := model.NewPagePropertyKey(c, pn, gpageKey)

		var gpp model.PageProperty
		err := ds.Get(c, gpropKey, &gpp)
		if err != nil && errors.Root(err) != datastore.ErrNoSuchEntity {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		b, err := json.Marshal(gpp)
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}
		responseJson := string(b)

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		_, err = w.Write([]byte(responseJson))
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}
	} else {
		handleError(c, w, errors.New("error: property name not found."), http.StatusInternalServerError)
		return
	}

	return
}
Exemplo n.º 4
0
func putRootPage(c context.Context, w http.ResponseWriter, r *http.Request) {
	var rp model.RootPage
	if err := getRequestJson(w, r, &rp); err != nil {
		handleError(c, w, err, http.StatusBadRequest)
		return
	}

	if rp.PageID == 0 {
		handleError(c, w, errors.New("pageID not found."), http.StatusBadRequest)
		return
	}

	rootPageKey := model.NewRootPageKey(c)

	err := datastore.RunInTransaction(c, func(c context.Context) error {
		var rp2put model.RootPage
		if err := ds.Get(c, rootPageKey, &rp2put); err != nil {
			if errors.Root(err) != datastore.ErrNoSuchEntity {
				return err
			} else {
				rp2put = model.RootPage{}
				rp2put.Key = rootPageKey
			}
		}

		if rp2put.PageID != rp.PageID {
			rp2put.PageID = rp.PageID
			ds.Put(c, &rp2put)
		}

		return nil
	}, nil)
	if err != nil {
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}
}
Exemplo n.º 5
0
func getPage(params martini.Params, w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	if r.Method != "GET" {
		handleError(c, w, errors.New("error: illegal access."), http.StatusInternalServerError)
		return
	}

	keyIDStr := params["key"]
	if keyIDStr != "" {
		// get page from datastore
		intID, err := strconv.ParseInt(keyIDStr, 10, 64)
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		key := model.NewPageKey(c, intID)

		var p model.Page
		if err := ds.Get(c, key, &p); err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		props, err := getPageProperties(c, key)
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		rpkey := model.NewRootPageKey(c)

		var rp model.RootPage
		err = ds.Get(c, rpkey, &rp)
		if err != nil && errors.Root(err) != datastore.ErrNoSuchEntity {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		jsonPage := model.NewJsonPage(&p, props, intID == rp.PageID)

		b, err := json.Marshal(jsonPage)
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}
		pageJson := string(b)

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		_, err = w.Write([]byte(pageJson))
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}
	} else {
		handleError(c, w, errors.New("error: key string not found."), http.StatusInternalServerError)
		return
	}

	return
}
Exemplo n.º 6
0
func savePage(c context.Context, pageKey *datastore.Key, p *model.Page, props map[string]string) error {

	err := datastore.RunInTransaction(c, func(c context.Context) error {
		var p2 model.Page
		if !pageKey.Incomplete() {
			if err := ds.Get(c, pageKey, &p2); err != nil {
				if errors.Root(err) != datastore.ErrNoSuchEntity {
					return errors.WrapOr(err)
				} else {
					p2 = model.Page{}
					p2.Key = pageKey
				}
			}
		}

		oldAlias := p2.Alias

		p2.Name = p.Name
		p2.Alias = p.Alias
		p2.Leaf = p.Leaf

		aliasChanged := oldAlias != p2.Alias

		if err := ds.Put(c, &p2); err != nil {
			return errors.WrapOr(err)
		}

		if aliasChanged && p2.Alias != "" {
			newAliasKey := model.NewPageAliasKey(c, p2.Alias)

			var pa model.PageAlias
			if err := ds.Get(c, newAliasKey, &pa); err == nil {
				return ErrPageAliasAlreadyExists
			} else {
				newAlias := model.PageAlias{}
				newAlias.Key = newAliasKey
				newAlias.PageKey = pageKey

				if err := ds.Put(c, &newAlias); err != nil {
					return errors.WrapOr(err)
				}
			}
		}

		if aliasChanged && oldAlias != "" {
			oldAliasKey := model.NewPageAliasKey(c, oldAlias)

			err := ds.Delete(c, oldAliasKey)
			if err != nil {
				return err
			}
		}

		// TODO: put multi
		for name, value := range props {
			initial, _ := utf8.DecodeRune([]byte(name))
			global := unicode.IsUpper(initial)

			var pkey *datastore.Key
			if global {
				pkey = model.NewGlobalPageKey(c)
			} else {
				pkey = pageKey
			}
			propKey := model.NewPagePropertyKey(c, name, pkey)

			var prop model.PageProperty
			err := ds.Get(c, propKey, &prop)
			if err == nil {
				prop.Key = propKey
				prop.Value = value
			}

			if err != nil {
				if errors.Root(err) != datastore.ErrNoSuchEntity {
					return errors.WrapOr(err)
				} else {
					prop = model.PageProperty{}
					prop.Key = propKey
					prop.Value = value
				}
			}

			if err = ds.Put(c, &prop); err != nil {
				return errors.WrapOr(err)
			}
		}

		return nil
	}, &datastore.TransactionOptions{XG: true})

	if err != nil {
		return err
	}
	return nil
}
Exemplo n.º 7
0
func getAreaAndBlocksAsync(c context.Context, pkey *datastore.Key, areaName string) <-chan func() (*model.Area, []model.Block, error) {
	ch := make(chan func() (*model.Area, []model.Block, error))
	go func() {

		var area model.Area
		area.Key = model.NewAreaKey(c, areaName, pkey)

		err := datastore.RunInTransaction(c, func(c context.Context) error {
			err := ds.Get(c, area.Key, &area)
			if err != nil && errors.Root(err) == datastore.ErrNoSuchEntity {
				// create if not exist

				// TODO make blocks extendable & initially zero blocks
				var block model.HTMLBlock
				block.Key = model.NewHTMLBlockIncompleteKey(c, area.Key)
				block.Value = areaName + ": edit here."
				err = ds.Put(c, &block)
				if err != nil {
					return err
				}

				area.Name = areaName
				area.Blocks = []*datastore.Key{block.Key}
				err = ds.Put(c, &area)
				if err != nil {
					return err
				}
			}

			return err
		}, nil)
		if err != nil {
			ch <- func() (*model.Area, []model.Block, error) {
				return nil, nil, err
			}
			return
		}

		blockChans := []<-chan func() (*model.HTMLBlock, error){}

		for _, blockKey := range area.Blocks {
			blockCh := getHTMLBlockAsync(c, blockKey)
			blockChans = append(blockChans, blockCh)
		}

		blocks := []model.Block{}

		for _, blockCh := range blockChans {
			b, err := (<-blockCh)()
			if err != nil {
				// TODO block not found
				blocks = append(blocks, &model.HTMLBlock{Value: "block not found."})
			} else {
				blocks = append(blocks, b)
			}
		}

		ch <- func() (*model.Area, []model.Block, error) {
			return &area, blocks, nil
		}
	}()
	return ch
}