Example #1
0
func fetchBlog(res *wcg.Response, req *wcg.Request, includePosts bool) (*models.Blog, *supports.Page, error) {
	// default query for posts.
	id := req.Param("blog_id")
	driver := models.NewBlogDriver(gae.NewContext(req), req.Logger)
	blog, query, err := driver.PostQuery(id)
	if err != nil {
		if err == models.ErrBlogNotFound {
			res.WriteHeader(404)
			res.WriteString("Not found")
			res.End()
			return nil, nil, err
		}
		res.RenderInternalError(err.Error())
		return nil, nil, err
	}

	if !includePosts {
		return blog, supports.EmptyPage, nil
	}
	query = query.Filter("IsNew =", false).Filter("IsDraft =", false).Order("-PostDate").Order("-UpdatedAt")
	per_page := wcg.ParseInt(req.Query("num"), AppConfig.DefaultPostsFetched, 0, AppConfig.MaxPostsFetched)
	current := wcg.ParseInt(req.Query("p"), 0, 0, wcg.ParseIntMax)
	page, err := supports.NewPage(current, per_page, query)
	if err != nil {
		res.RenderInternalError("Post pagination error: %v", err)
		return nil, nil, err
	}
	return blog, page, nil
}
Example #2
0
func queryShows(res *wcg.Response, req *wcg.Request, q *datastore.Query) (*showQueryResult, error) {
	var appCtx = lib.NewAppContextFromRequest(req)
	var showList []event.Show
	var basePath = req.HttpRequest().URL.Path
	per_page := 12 // Restict to 12 due to view rendering.
	page := wcg.ParseInt(req.Query("page"), 0, 0, wcg.ParseIntMax)
	if _, err := q.Offset(page * per_page).Limit(per_page).GetAll(&showList); err != nil {
		return nil, err
	}
	if showList == nil {
		return &showQueryResult{
			Shows: make([]EventShow, 0),
		}, nil
	}

	if list, err := NewEventShowList(appCtx, showList); err != nil {
		return nil, err
	} else {
		p := &showQueryResult{
			Shows:   list,
			Current: fmt.Sprintf("%s?page=%d&n=%d", basePath, page, per_page),
		}

		// prev url
		if page > 0 {
			p.Previous = fmt.Sprintf("%s?page=%d&n=%d", basePath, page-1, per_page)
		}

		// next url
		if len(p.Shows) == per_page {
			p.Next = fmt.Sprintf("%s?page=%d&n=%d", basePath, page+1, per_page)
		}
		return p, nil
	}
}
Example #3
0
func Redirect(res *wcg.Response, req *wcg.Request) {
	urlStr := req.Query("u")
	if urlobj, err := url.Parse(urlStr); err == nil && validateUrl(urlobj) {
		// TODO: Check blacklist
		res.Redirect(urlStr, 302)
		return
	}
	res.TemplatesWithStatus(
		404,
		nil,
		"404.html", "header.html", "footer.html",
	)
	return
}
Example #4
0
func historyInsights(res *wcg.Response, req *wcg.Request, app *App) {
	var appCtx = lib.NewAppContextFromRequest(req)
	member, ok := app.Members[req.Param("member")]
	if !ok {
		lib.NotFound(res, req)
		return
	}
	var insights amebloHistoryInsights
	mckey := fmt.Sprintf(MC_KEY_HISTORY, app.Key, member.Name)
	dent := NewAmebloEntryDriver(appCtx)
	dref := NewAmebloRefDriver(appCtx)
	mc := appCtx.NewMemcacheDriver()
	err := mc.CachedObject(mckey, &insights, func() (interface{}, error) {
		return getAmebloHistoryInsights(member, dent, dref)
	}, req.Query("force") == "1")
	if err != nil {
		lib.Error(res, req, err)
		return
	}
	res.WriteJson(insights)
}
Example #5
0
func listTvChannels(res *wcg.Response, req *wcg.Request) ([]*tv.TvChannel, error) {
	var list []*tv.TvChannel
	app := lib.GetCurrentApp(req)
	ctx := gae.NewContext(req)
	d := NewTvChannelDriver(app.Key, ctx, req.Logger)
	mc := memcache.NewDriver(ctx, req.Logger)
	err := mc.CachedObject(MC_KEY_CHANNELS, &list, func() (interface{}, error) {
		return d.AllAsList()
	}, req.Query("force") == "1")
	if err != nil {
		return nil, err
	} else {
		if len(list) == 0 {
			req.Logger.Warn("No channel is defined. Reset the configuraiton.")
			d.AddChannelList(defaultChannels)
			mc.Delete(MC_KEY_CHANNELS)
			mc.Set(MC_KEY_CHANNELS, defaultChannels)
			res.WriteJson(defaultChannels)
			return defaultChannels, nil
		} else {
			return list, nil
		}
	}
}
Example #6
0
func queryPosts(res *wcg.Response, req *wcg.Request, per_page int) (*postQueryResult, error) {
	var list []blog.Post
	var basePath = req.HttpRequest().URL.Path
	is_admin := (lib.GetUserKind(req) == lib.Admin) && (req.Query("is_admin") == "true")
	if per_page == 0 {
		per_page = wcg.ParseInt(req.Query("n"), 5, 0, 20) // default 5, max 20
	}
	page := wcg.ParseInt(req.Query("page"), 0, 0, wcg.ParseIntMax)
	q := NewPostDriver(lib.NewAppContextFromRequest(req)).NewQuery()
	q = q.Order("-PublishAt").Order("-CreatedAt")
	if !is_admin {
		q = q.Filter("PublishAt <=", time.Now()).Filter("IsDraft =", false).Filter("IsHidden =", false)
	}

	_, err := q.Offset(page * per_page).Limit(per_page).GetAll(&list)
	if err != nil {
		return nil, err
	}
	if list == nil {
		list = make([]blog.Post, 0)
	}

	p := &postQueryResult{
		Posts: list,
	}

	// prev url
	if page > 0 {
		p.Previous = fmt.Sprintf("%s?page=%d&n=%d", basePath, page-1, per_page)
		if is_admin {
			p.Previous = fmt.Sprintf("%s&is_admin=true", p.Previous)
		}
	}

	// next url
	if len(list) == per_page {
		p.Next = fmt.Sprintf("%s?page=%d&n=%d", basePath, page+1, per_page)
		if is_admin {
			p.Next = fmt.Sprintf("%s&is_admin=true", p.Next)
		}
	}
	return p, nil
}