From d6132e6f70c0e284ac01cb1fdec49685ae1c339c Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Wed, 8 Oct 2014 10:58:51 +0200 Subject: [PATCH] Fix for another loc bug --- Site5/App_Code/CommonV5Base.cs | 40 +++++++++ Site5/App_Code/DataV5Base.cs | 40 +-------- Site5/App_Code/PageV5Base.cs | 27 +----- Site5/Automation/UploadFileHandler.ashx | 2 +- Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs | 89 +++++++++++-------- .../v5/Ledgers/Json-ResyncPreview.aspx.cs | 12 ++- 6 files changed, 100 insertions(+), 110 deletions(-) diff --git a/Site5/App_Code/CommonV5Base.cs b/Site5/App_Code/CommonV5Base.cs index 75e39fe1..918a0e20 100644 --- a/Site5/App_Code/CommonV5Base.cs +++ b/Site5/App_Code/CommonV5Base.cs @@ -4,6 +4,8 @@ using System.Globalization; using System.Linq; using System.Threading; using System.Web; +using Swarmops.Logic.Structure; +using Swarmops.Logic.Swarm; /// /// Summary description for CommonV5Base @@ -66,4 +68,42 @@ public class CommonV5Base Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; } + + public static AuthenticationData GetAuthenticationDataAndCulture(HttpContext suppliedContext) + { + // This function is called from static page methods in AJAX calls to get + // the current set of authentication data. Static page methods cannot access + // the instance data of PageV5Base. + + AuthenticationData result = new AuthenticationData(); + + // Find various credentials + + string identity = suppliedContext.User.Identity.Name; + string[] identityTokens = identity.Split(','); + + string userIdentityString = identityTokens[0]; + string organizationIdentityString = identityTokens[1]; + + int currentUserId = Convert.ToInt32(userIdentityString); + int currentOrganizationId = Convert.ToInt32(organizationIdentityString); + + result.CurrentUser = Person.FromIdentity(currentUserId); + result.CurrentOrganization = Organization.FromIdentity(currentOrganizationId); + + CommonV5Base.CulturePreInit(HttpContext.Current.Request); // OnPreInit() isn't called in the static methods calling this fn + + /* + string userCultureString = result.CurrentUser.PreferredCulture; + + if (!string.IsNullOrEmpty(userCultureString)) + { + CultureInfo userCulture = new CultureInfo(userCultureString); // may throw on invalid database data + Thread.CurrentThread.CurrentCulture = userCulture; + Thread.CurrentThread.CurrentUICulture = userCulture; + }*/ + + return result; + } + } \ No newline at end of file diff --git a/Site5/App_Code/DataV5Base.cs b/Site5/App_Code/DataV5Base.cs index f6399dac..a189e8fe 100644 --- a/Site5/App_Code/DataV5Base.cs +++ b/Site5/App_Code/DataV5Base.cs @@ -130,47 +130,9 @@ public class DataV5Base : System.Web.UI.Page protected static AuthenticationData GetAuthenticationDataAndCulture() { - return GetAuthenticationDataAndCulture(HttpContext.Current); + return CommonV5Base.GetAuthenticationDataAndCulture(HttpContext.Current); } - protected static AuthenticationData GetAuthenticationDataAndCulture(HttpContext suppliedContext) - { - // This function is called from static page methods in AJAX calls to get - // the current set of authentication data. Static page methods cannot access - // the instance data of PageV5Base. - - AuthenticationData result = new AuthenticationData(); - - // Find various credentials - - string identity = suppliedContext.User.Identity.Name; - string[] identityTokens = identity.Split(','); - - string userIdentityString = identityTokens[0]; - string organizationIdentityString = identityTokens[1]; - - int currentUserId = Convert.ToInt32(userIdentityString); - int currentOrganizationId = Convert.ToInt32(organizationIdentityString); - - result.CurrentUser = Person.FromIdentity(currentUserId); - result.CurrentOrganization = Organization.FromIdentity(currentOrganizationId); - - CommonV5Base.CulturePreInit(HttpContext.Current.Request); // OnPreInit() isn't called in the static methods calling this fn - - /* - string userCultureString = result.CurrentUser.PreferredCulture; - - if (!string.IsNullOrEmpty(userCultureString)) - { - CultureInfo userCulture = new CultureInfo(userCultureString); // may throw on invalid database data - Thread.CurrentThread.CurrentCulture = userCulture; - Thread.CurrentThread.CurrentUICulture = userCulture; - }*/ - - return result; - } - - protected static string JsonSanitize (string input) { return input.Replace("\"", "\\\"").Replace(" ", " ").Trim(); diff --git a/Site5/App_Code/PageV5Base.cs b/Site5/App_Code/PageV5Base.cs index d5997834..0f7026df 100644 --- a/Site5/App_Code/PageV5Base.cs +++ b/Site5/App_Code/PageV5Base.cs @@ -127,32 +127,7 @@ public class PageV5Base : System.Web.UI.Page // the current set of authentication data. Static page methods cannot access // the instance data of PageV5Base. - AuthenticationData result = new AuthenticationData(); - - // Find various credentials - - string identity = HttpContext.Current.User.Identity.Name; - string[] identityTokens = identity.Split(','); - - string userIdentityString = identityTokens[0]; - string organizationIdentityString = identityTokens[1]; - - int currentUserId = Convert.ToInt32(userIdentityString); - int currentOrganizationId = Convert.ToInt32(organizationIdentityString); - - result.CurrentUser = Person.FromIdentity(currentUserId); - result.CurrentOrganization = Organization.FromIdentity(currentOrganizationId); - - string userCultureString = result.CurrentUser.PreferredCulture; - - if (!string.IsNullOrEmpty(userCultureString)) - { - CultureInfo userCulture = new CultureInfo(userCultureString); // may throw on invalid database data - Thread.CurrentThread.CurrentCulture = userCulture; - Thread.CurrentThread.CurrentUICulture = userCulture; - } - - return result; + return CommonV5Base.GetAuthenticationDataAndCulture(HttpContext.Current); } /// diff --git a/Site5/Automation/UploadFileHandler.ashx b/Site5/Automation/UploadFileHandler.ashx index 8c50f00b..e480dd09 100644 --- a/Site5/Automation/UploadFileHandler.ashx +++ b/Site5/Automation/UploadFileHandler.ashx @@ -157,7 +157,7 @@ namespace Swarmops.Frontend.Automation HttpPostedFile file = context.Request.Files[i]; string fullName = Path.GetFileName (file.FileName); - AuthenticationData authData = GetAuthenticationDataAndCulture (context); + AuthenticationData authData = CommonV5Base.GetAuthenticationDataAndCulture (context); Person uploadingPerson = authData.CurrentUser; Organization currentOrg = authData.CurrentOrganization; diff --git a/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs b/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs index 327c2180..c24581fe 100644 --- a/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs +++ b/Site5/Pages/v5/Ledgers/AccountPlan.aspx.cs @@ -7,6 +7,7 @@ using System.Web.Services; using System.Web.UI; using System.Web.UI.WebControls; using Swarmops.Basic.Enums; +using Swarmops.Database; using Swarmops.Logic.Financial; using Swarmops.Logic.Security; using Swarmops.Logic.Support; @@ -212,47 +213,61 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers [WebMethod] public static ChangeAccountDataResult SetAccountBudget(int accountId, string budget) { - AuthenticationData authData = GetAuthenticationDataAndCulture(); - FinancialAccount account = FinancialAccount.FromIdentity(accountId); - - if (!PrepareAccountChange(account, authData, false)) + try { - return new ChangeAccountDataResult + AuthenticationData authData = GetAuthenticationDataAndCulture(); + FinancialAccount account = FinancialAccount.FromIdentity(accountId); + + if (!PrepareAccountChange(account, authData, false)) { - Result = ChangeAccountDataOperationsResult.NoPermission - }; + return new ChangeAccountDataResult + { + Result = ChangeAccountDataOperationsResult.NoPermission + }; + } + + Int64 newTreeBudget; + budget = budget.Replace("%A0", "%20"); // some very weird browser space-to-otherspace translation weirds out number parsing + budget = HttpContext.Current.Server.UrlDecode(budget); + + if (budget.Trim().Length > 0 && Int64.TryParse(budget, NumberStyles.Currency, CultureInfo.CurrentCulture, out newTreeBudget)) + { + newTreeBudget *= 100; // convert to cents + + int year = DateTime.Today.Year; + FinancialAccounts accountTree = account.GetTree(); + Int64 currentTreeBudget = accountTree.GetBudgetSumCents(year); + Int64 currentSingleBudget = account.GetBudgetCents(year); + Int64 suballocatedBudget = currentTreeBudget - currentSingleBudget; + + Int64 newSingleBudget = newTreeBudget - suballocatedBudget; + + account.SetBudgetCents(DateTime.Today.Year, newSingleBudget); + return new ChangeAccountDataResult + { + Result = ChangeAccountDataOperationsResult.Changed, + NewData = (newTreeBudget/100).ToString("N0", CultureInfo.CurrentCulture) + }; + } + else + { + return new ChangeAccountDataResult + { + Result = ChangeAccountDataOperationsResult.Invalid + }; + } + + } + catch (Exception weirdException) + { + // Exceptions are happening here in deployment ONLY. We're logging it to find which one and why. + // TODO: This really needs to be in Logic. DO NOT DO NOT DO NOT call Database layer directly from Site layer. + + SwarmDb.GetDatabaseForWriting().CreateExceptionLogEntry(DateTime.UtcNow, "AccountPlan-SetBudget", weirdException); + + throw; } - Int64 newTreeBudget; - budget = budget.Replace("%A0", "%20"); // some very weird browser space-to-otherspace translation weirds out number parsing - budget = HttpContext.Current.Server.UrlDecode(budget); - - if (budget.Trim().Length > 0 && Int64.TryParse(budget, NumberStyles.Currency, CultureInfo.CurrentCulture, out newTreeBudget)) - { - newTreeBudget *= 100; // convert to cents - - int year = DateTime.Today.Year; - FinancialAccounts accountTree = account.GetTree(); - Int64 currentTreeBudget = accountTree.GetBudgetSumCents(year); - Int64 currentSingleBudget = account.GetBudgetCents(year); - Int64 suballocatedBudget = currentTreeBudget - currentSingleBudget; - - Int64 newSingleBudget = newTreeBudget - suballocatedBudget; - - account.SetBudgetCents(DateTime.Today.Year, newSingleBudget); - return new ChangeAccountDataResult - { - Result = ChangeAccountDataOperationsResult.Changed, - NewData = (newTreeBudget/100).ToString("N0", CultureInfo.CurrentCulture) - }; - } - else - { - return new ChangeAccountDataResult - { - Result = ChangeAccountDataOperationsResult.Invalid - }; - } } [WebMethod] diff --git a/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs b/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs index fd90e01c..06f0e2e0 100644 --- a/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs +++ b/Site5/Pages/v5/Ledgers/Json-ResyncPreview.aspx.cs @@ -99,10 +99,8 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers } private int _year = 2012; - private CultureInfo _renderCulture; private AuthenticationData _authenticationData; - private Dictionary _hashedAccounts; - + // private Dictionary _hashedAccounts; private string PrintNullableCents (string currency, long cents) { @@ -135,7 +133,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers } } - + /* -- is this even used? private string GetAccountGroup (FinancialAccountType type, string localizedGroupName) { string childrenString = GetAccountsRecurse(type, 0); @@ -212,7 +210,7 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers } return String.Join(",", childStrings.ToArray()); - } + }*/ private Dictionary _singleBalanceLookup; private Dictionary _treeBalanceLookup; @@ -293,11 +291,11 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers { if (treeValue != 0 && singleValue == 0) { - return string.Format(_renderCulture, + return string.Format(CultureInfo.CurrentCulture, "Σ {1:N0} ", accountId, treeValue / 100.00); } - return string.Format(_renderCulture, + return string.Format(CultureInfo.CurrentCulture, "Σ {1:N0}{2:N0}", accountId, treeValue / 100.0, singleValue / 100.0); }