common

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 5, 2020 License: BSD-3-Clause Imports: 18 Imported by: 0

Documentation

Overview

Package common provides routines common to all handlers in the ledger webapp.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// Error for date being wrong when adding an entry.
	ErrDateMayBeWrong         = errors.New("Date may be wrong, proceed anyway?")
	ErrConcurrentModification = errors.New(
		"Someone else already updated this entry. Click cancel and try again.")
)
View Source
var (
	ErrXsrf = errors.New("Page had grown stale. Please resubmit.")
)
View Source
var (
	// Represents the combo box for recurring units.
	// Implements http_util.SelectionModel
	RecurringUnitComboBox = RecurringUnitComboBoxType{
		fin.Months, fin.Years, fin.Days, fin.Weeks}
)

Functions

func ActiveCatDetails

func ActiveCatDetails(
	cds categories.CatDetailStore,
	catPopularity fin.CatPopularity,
	showAccounts bool) []categories.CatDetail

ActiveCatDetails returns the active cat details prepended with the most popular ones. catPopularity is the popularity of the categories. If showAccounts is true, ActiveCatDetails returns values for accounts too. If catPopularity is nil, ActiveCatDetails simply returns the active cat details without prepending the most popular ones.

func EntryMutation

func EntryMutation(values url.Values) (
	mutation fin.EntryUpdater, err error)

EntryMutation converts the form values from a single entry page into a mutation and returns that mutation or an error if the form values were invalid. Returned filter always returns true.

func NewGorillaSession

func NewGorillaSession(
	sessionStore sessions.Store, r *http.Request) (*sessions.Session, error)

NewGorillaSession creates a gorilla session for the finance app

func NewTemplate

func NewTemplate(name, templateStr string) *template.Template

NewTemplate returns a new template instance. name is the name of the template; templateStr is the template string. Returned template has FormatDate and FormatUSD defined.

func NewXsrfToken

func NewXsrfToken(r *http.Request, action string) string

NewXsrfToken creates a new xsrf token for given action.

func NormalizeYMDStr

func NormalizeYMDStr(dateStr string) string

NormalizeYMDStr normalizes a date string.

Example
package main

import (
	"fmt"
	"github.com/keep94/finance/apps/ledger/common"
)

func main() {
	fmt.Printf("%s %s %s\n",
		common.NormalizeYMDStr("2012"),
		common.NormalizeYMDStr("201203"),
		common.NormalizeYMDStr("20120305"))
}
Output:

20120101 20120301 20120305

func VerifyXsrfToken

func VerifyXsrfToken(r *http.Request, action string) bool

VerifyXsrfToken verifies the xsrf token for given action. VerifyXsrfToken looks for the token under "xsrf" in request.

func VerifyXsrfTokenExplicit

func VerifyXsrfTokenExplicit(
	xsrf string, r *http.Request, action string) bool

VerifyXsrfTokenExplicit verifies an explicit xsrf token for given action.

Types

type AccountLinker

type AccountLinker struct {
}

AccountLinker creates URLs to account pages

func (a AccountLinker) AccountLink(id int64) *url.URL

AccountLink returns a URL to the account page with given account Id.

func (a AccountLinker) RecurringLink(id int64) *url.URL

RecurringLink returns a URL to the recurring entries page for a given account Id.

func (a AccountLinker) UnreconciledLink(id int64) *url.URL

UnreconiledLink returns a URL to the unreconciled page with given account Id.

func (a AccountLinker) UploadLink(id int64) *url.URL

UploadLink returns a URL to the upload page for a given account Id.

type CatDisplayer

type CatDisplayer struct {
	categories.CatDetailStore
}

CatDisplayer is used to display categories.

func (CatDisplayer) AccountSelectModel

func (c CatDisplayer) AccountSelectModel() http_util.SelectModel

AccountSelectModel returns the model for account dropdowns.

func (CatDisplayer) AcctName

func (c CatDisplayer) AcctName(cp *fin.CatPayment) string

Returns the account name to display for an entry. cp is the CatPayment record in the entry.

func (CatDisplayer) CatName

func (c CatDisplayer) CatName(cp *fin.CatPayment) string

Returns the category name to display for an entry. cp is the CatPayment record in the entry.

func (CatDisplayer) CatSelectModel

func (c CatDisplayer) CatSelectModel() http_util.SelectModel

CatSelectModel returns the model for category dropdowns.

type CatLinker

type CatLinker struct {
	ListEntries *url.URL
	Cds         categories.CatDetailStore
}

CatLinker displays categories as a series of hyperlinks

func (c *CatLinker) AccountNameLink(cp *fin.CatPayment) LinkText

AccountNameLink returns the account name as a link. If c.ListEntries is nil, returns account name as plain text.

func (c *CatLinker) CatLink(cp *fin.CatPayment) []LinkText

CatLink returns the category name as series of hyper links. If c.ListEntries is nil, just returns the category full name as plain text.

type EntryLinker

type EntryLinker struct {

	// The current URL
	URL *url.URL

	// Item selected in left navigation
	Sel Selecter
}

EntryLinker creates URLs to the edit entry page.

func (e *EntryLinker) EntryLink(id int64) *url.URL

EntryLink returns a URL to the edit entry page. id is the id of the entry to be edited.

func (e *EntryLinker) NewEntryLink(paymentId int64) *url.URL

NewEntryLink returns a URL to the edit entry page for creating a new entry. paymentId is the account ID of the default payment type. Passing 0 or a negative number means that user will have to select payment in the edit entry page.

type EntrySplitType

type EntrySplitType int

EntrySplitType represents the display for a single split of an entry.

func (EntrySplitType) AmountParam

func (s EntrySplitType) AmountParam() string

AmountParam returns the name of the amount parameter for this split

func (EntrySplitType) CatParam

func (s EntrySplitType) CatParam() string

CatParam returns the name of the category parameter for this split

func (EntrySplitType) ReconcileParam

func (s EntrySplitType) ReconcileParam() string

ReconcileParam returns the name of the reconciled parameter for this split

type Global

type Global struct {

	// The title of the website
	Title string

	// True if website has an icon at /images/favicon.ico
	Icon bool
}

Global contains static information global to all pages in website.

type LeftNav

type LeftNav struct {
	Cdc   categoriesdb.Getter
	Clock date_util.Clock
}

LeftNav is for creating the left navigation bar.

func (*LeftNav) Generate

func (l *LeftNav) Generate(
	w http.ResponseWriter, r *http.Request, sel Selecter) template.HTML

Generate generates the html for the left navigation bar including the div tags. sel indicates which item in the left navigation bar will be selected. If Generate can't generate the html, it returns the empty string, writes an error message to w, and writes the error to stderr.

type LinkText

type LinkText struct {

	// nil if plain text
	Link *url.URL

	Text string
}

LinkText can be hyperlink text or plain text

type RecurringEntryLinker

type RecurringEntryLinker struct {
	URL *url.URL
	Sel Selecter
}

RecurringEntryLinker creates URLs to the edit recurring entry page.

func (e RecurringEntryLinker) EntryLink(id int64) *url.URL

EntryLink returns a URL to the edit recurring entry page. id is the id of the recurring entry to be edited.

func (e RecurringEntryLinker) NewEntryLink(paymentId int64) *url.URL

NewEntryLink returns a URL to the edit recurring entry page for creating a new recurring entry. paymentId is the account ID of the default payment type. Passing 0 or a negative number means that user will have to select payment in the edit recurring entry page.

type RecurringUnitComboBoxType

type RecurringUnitComboBoxType []fin.RecurringUnit

func (RecurringUnitComboBoxType) ToSelection

type Selecter

type Selecter struct {
	// contains filtered or unexported fields
}

Selecter indicates the item to be selected in the left navigation bar.

func ParseSelecter

func ParseSelecter(s string) (Selecter, error)

func SelectAccount

func SelectAccount(id int64) Selecter

func SelectChpasswd

func SelectChpasswd() Selecter

func SelectExport

func SelectExport() Selecter

func SelectManage

func SelectManage() Selecter

func SelectNone

func SelectNone() Selecter

func SelectRecurring

func SelectRecurring() Selecter

func SelectReports

func SelectReports() Selecter

func SelectSearch

func SelectSearch() Selecter

func SelectTotals

func SelectTotals() Selecter

func SelectTrends

func SelectTrends() Selecter

func SelectUnreviewed

func SelectUnreviewed() Selecter

func (Selecter) String

func (s Selecter) String() string

type SingleEntryView

type SingleEntryView struct {
	http_util.Values
	CatDisplayer
	Splits        []EntrySplitType
	Error         error
	Xsrf          string
	ExistingEntry bool
	Global        *Global
	LeftNav       template.HTML
	// contains filtered or unexported fields
}

View for single entry pages.

func ToSingleEntryView

func ToSingleEntryView(
	entry *fin.Entry,
	xsrf string,
	cds categories.CatDetailStore,
	catPopularity fin.CatPopularity,
	global *Global,
	leftnav template.HTML) *SingleEntryView

ToSingleEntryView creates a view from a particular entry. The caller may safely add additional name value pairs to the Values field of returned view. The caller must refrain from making other types of changes to returned view.

func ToSingleEntryViewFromForm

func ToSingleEntryViewFromForm(
	existingEntry bool,
	values url.Values,
	xsrf string,
	cds categories.CatDetailStore,
	catPopularity fin.CatPopularity,
	global *Global,
	leftnav template.HTML,
	err error) *SingleEntryView

ToSingleEntryViewFromForm creates a view from form data. existingEntry is true if the form data represents an existing entry or false if it represents a brand new entry. values are the form values. xsrf is the cross site request forgery token cds is the category detail store. catPopularity is the popularity of the categories. May be nil. global is the non changing global content of view. leftnav is the left navigation content err is the error from the form submission or nil if no error.

func (*SingleEntryView) ActiveCatDetails

func (v *SingleEntryView) ActiveCatDetails(
	showAccounts bool) []categories.CatDetail

func (*SingleEntryView) DateMayBeWrong

func (v *SingleEntryView) DateMayBeWrong() bool

DateMayBeWrong returns true if and only if the error for this view is ErrDateMayBeWrong.

type UserSession

type UserSession struct {
	session_util.UserIdSession
	*sessions.Session
	// User is the logged in user or nil if no user logged in
	User *fin.User

	// Main store for accessing entries, accounts, and users
	Store interface{}

	// The category cache
	Cache categoriesdb.Getter

	// Loads QFX files
	Uploaders map[string]autoimport.Loader
}

UserSession represents a session where user is logged in.

func CreateUserSession

func CreateUserSession(s *sessions.Session) *UserSession

CreateUserSession creates a UserSession instance from a gorilla session but does not populate the user field of the returned session.

func GetUserSession

func GetUserSession(r *http.Request) *UserSession

GetUserSession gets the UserSession associated with the request. It can only be called after successful completion of NewUserSession.

func NewUserSession

func NewUserSession(
	store findb.UserByIdRunner,
	sessionStore sessions.Store,
	r *http.Request) (*UserSession, error)

NewUserSession creates a UserSession and associates it with the request instance. If user not logged in, the User field in returned UserSession is nil. Caller must call context.Clear with request instance.

func (*UserSession) Batch

func (s *UserSession) Batch(acctId int64) autoimport.Batch

Batch returns the uploaded batch for a particular account ID. Batch returns nil if there is no pending batch.

func (*UserSession) CatPopularity

func (s *UserSession) CatPopularity() fin.CatPopularity

CatPopularity returns the category popularities

func (*UserSession) SetBatch

func (s *UserSession) SetBatch(acctId int64, batch autoimport.Batch)

SetBatch stores a batch in the session under a particular account ID. Passing nil for batch indicates there is no batch for the given account ID.

func (*UserSession) SetCatPopularity

func (s *UserSession) SetCatPopularity(cp fin.CatPopularity)

SetCatPopularity stores the category popularities in the session. Passing nil for cp indicates there are not category popularities.

func (*UserSession) SetUser

func (s *UserSession) SetUser(userPtr interface{})

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL