diff --git a/WorkFlowProvider/NpgsqlContentProvider.cs b/WorkFlowProvider/NpgsqlContentProvider.cs index 59e6fd66..b15132fa 100644 --- a/WorkFlowProvider/NpgsqlContentProvider.cs +++ b/WorkFlowProvider/NpgsqlContentProvider.cs @@ -230,9 +230,6 @@ namespace WorkFlowProvider cmd.CommandText = "insert into writtings (description, estimid, ucost, count, productid) VALUES (@dscr,@estid,@ucost,@count,@prdid) returning _id"; cmd.Parameters.Add ("@dscr", desc); - // cmd.Parameters.Add ("@prdid", productid); - // cmd.Parameters.Add("@ucost", ucost); - // cmd.Parameters.Add("@mult", count); cmd.Parameters.Add("@estid", estid); cmd.Parameters.Add("@ucost", ucost); diff --git a/web/Controllers/BlogsController.cs b/web/Controllers/BlogsController.cs index 6b5d0f8a..1fb77c45 100644 --- a/web/Controllers/BlogsController.cs +++ b/web/Controllers/BlogsController.cs @@ -143,7 +143,7 @@ namespace Yavsc.Controllers return UserPost (BlogManager.GetPost (postid)); } } - string prevstr = App_GlobalResources.LocalizedText.Preview; + string prevstr = LocalizedText.Preview; return UserPost (BlogManager.GetPost (user, title)); } diff --git a/web/Controllers/FrontOfficeApiController.cs b/web/Controllers/FrontOfficeApiController.cs index 69927b78..7135d8fa 100644 --- a/web/Controllers/FrontOfficeApiController.cs +++ b/web/Controllers/FrontOfficeApiController.cs @@ -88,9 +88,12 @@ namespace Yavsc.ApiControllers [AcceptVerbs("GET")] public HttpResponseMessage GetEstimTex(long estimid) { + string texest = getEstimTex (estimid); + if (texest == null) + throw new HttpRequestValidationException ("Not an estimation id:"+estimid); return new HttpResponseMessage () { Content = new ObjectContent (typeof(string), - getEstimTex (estimid), + texest, new SimpleFormatter ("text/x-tex")) }; } @@ -101,12 +104,21 @@ namespace Yavsc.ApiControllers Estimate e = WorkFlowManager.GetEstimate (estimid); tmpe.Session = new Dictionary(); tmpe.Session.Add ("estim", e); - Profile pr = AccountController.GetProfile (e.Responsible); - tmpe.Session.Add ("from", pr); - tmpe.Session.Add ("to", pr); + + Profile prpro = new Profile(ProfileBase.Create(e.Responsible)); + if (!prpro.IsBankable) + throw new Exception ("NotBankable:"+e.Responsible); + + Profile prcli = new Profile(ProfileBase.Create(e.Client)); + if (!prcli.IsBillable) + throw new Exception ("NotBillable:"+e.Client); + tmpe.Session.Add ("from", prpro); + tmpe.Session.Add ("to", prcli); tmpe.Init (); return tmpe.TransformText (); } + + /// /// Gets the estimate in pdf format from tex generation. /// @@ -115,14 +127,7 @@ namespace Yavsc.ApiControllers public HttpResponseMessage GetEstimPdf(long estimid) { Estimate estim = WorkFlowManager.GetEstimate (estimid); - - Profile prpro = new Profile(ProfileBase.Create(estim.Responsible)); - if (!prpro.IsBankable) - throw new Exception ("NotBankable:"+estim.Responsible); - - Profile prcli = new Profile(ProfileBase.Create(estim.Client)); - if (!prcli.IsBillable) - throw new Exception ("NotBillable:"+estim.Client); + //TODO better with pro.IsBankable && cli.IsBillable return new HttpResponseMessage () { Content = new ObjectContent ( diff --git a/web/Controllers/HomeController.cs b/web/Controllers/HomeController.cs index e3c56b66..4855377c 100644 --- a/web/Controllers/HomeController.cs +++ b/web/Controllers/HomeController.cs @@ -9,8 +9,8 @@ using System.Web.Mvc; using System.Web.Mvc.Ajax; using Yavsc; using System.Reflection; -using Yavsc.App_GlobalResources; using System.Resources; +using Yavsc.Model; namespace Yavsc.Controllers { diff --git a/web/Controllers/WorkFlowController.cs b/web/Controllers/WorkFlowController.cs index 5571a8b9..60f9e7da 100644 --- a/web/Controllers/WorkFlowController.cs +++ b/web/Controllers/WorkFlowController.cs @@ -2,12 +2,13 @@ using System.Collections.Generic; using System.Linq; using System.Web; - -using System.Web.Http; using WorkFlowProvider; using Yavsc.Model.WorkFlow; using System.Web.Http.Controllers; using System.Web.Security; +using System.Web.Http.ModelBinding; +using System.Net.Http; +using System.Web.Http; namespace Yavsc.ApiControllers { @@ -38,18 +39,8 @@ namespace Yavsc.ApiControllers { WorkFlowManager.DropWritting (wrid); } - class Error {} - [Authorize] - [AcceptVerbs("POST")] - public object UpdateWritting([FromBody] Writting model) - { - if (!ModelState.IsValid) { - return ModelState.Where ( k => k.Value.Errors.Count>0) ; - } - WorkFlowManager.UpdateWritting (model); - return null; - } + [HttpGet] [Authorize] @@ -67,17 +58,54 @@ namespace Yavsc.ApiControllers return new { test=string.Format("Hello {0}!",username) }; } + private HttpResponseMessage CreateModelStateErrorResponse () { + // strip exceptions + Dictionary errs = new Dictionary (); + + foreach (KeyValuePair st + in ModelState.Where (x => x.Value.Errors.Count > 0)) + errs.Add(st.Key, st.Value.Errors.Select(x=>x.ErrorMessage).ToArray()); + + return Request.CreateResponse(System.Net.HttpStatusCode.BadRequest, + errs); + } + + [Authorize] + [AcceptVerbs("POST")] + [ValidateAjax] + public HttpResponseMessage UpdateWritting([FromBody] Writting wr) + { + WorkFlowManager.UpdateWritting (wr); + return Request.CreateResponse (System.Net.HttpStatusCode.OK); + } [AcceptVerbs("POST")] [Authorize] + [ValidateAjax] /// /// Adds the specified imputation to the given estimation by estimation id. /// /// Estimation identifier /// Imputation to add - public long Write ([FromUri] long estid, Writting wr) { - return WorkFlowManager.Write(estid, wr.Description, - wr.UnitaryCost, wr.Count, wr.ProductReference); + public HttpResponseMessage Write ([FromUri] long estid, [FromBody] Writting wr) { + if (estid <= 0) { + ModelState.AddModelError ("EstimationId", "Spécifier un identifiant d'estimation valide"); + return Request.CreateResponse (System.Net.HttpStatusCode.BadRequest, + ValidateAjaxAttribute.GetErrorModelObject (ModelState)); + } + try { + return Request.CreateResponse(System.Net.HttpStatusCode.OK, + WorkFlowManager.Write(estid, wr.Description, + wr.UnitaryCost, wr.Count, wr.ProductReference)); + } + catch (Exception ex) { + return Request.CreateResponse ( + System.Net.HttpStatusCode.InternalServerError, + "Internal server error:" + ex.Message + "\n" + ex.StackTrace); + + } } } + + } diff --git a/web/Formatters/EstimToPdfFormatter.cs b/web/Formatters/EstimToPdfFormatter.cs index a752cf64..9f626c78 100644 --- a/web/Formatters/EstimToPdfFormatter.cs +++ b/web/Formatters/EstimToPdfFormatter.cs @@ -69,15 +69,17 @@ namespace Yavsc.Formatters var pbc = ProfileBase.Create (e.Client); Profile prcli = new Profile (pbc); - if (!prpro.IsBankable) - throw new Exception ("This provider is not bankable."); + + if (!prpro.IsBankable || !prcli.IsBillable) + throw new Exception("not bankable or not billable."); + tmpe.Session.Add ("from", prpro); tmpe.Session.Add ("to", prcli); tmpe.Init (); string content = tmpe.TransformText (); - string name = string.Format ("tmpestimtex{0}", e.Id); + string name = string.Format ("tmpestimtex-{0}", e.Id); string fullname = Path.Combine ( HttpRuntime.CodegenDir, name); FileInfo fi = new FileInfo(fullname + ".tex"); @@ -86,17 +88,19 @@ namespace Yavsc.Formatters { sw.Write (content); } - using (Process p = new Process ()) { + using (Process p = new Process ()) { p.StartInfo.WorkingDirectory = HttpRuntime.CodegenDir; p.StartInfo = new ProcessStartInfo (); p.StartInfo.UseShellExecute = false; p.StartInfo.FileName = "/usr/bin/texi2pdf"; p.StartInfo.Arguments = - string.Format ("--batch -o {0} {1}", + string.Format ("--batch --build-dir={2} -o {0} {1}", fo.FullName, - fi.FullName); + fi.FullName,HttpRuntime.CodegenDir); p.Start (); p.WaitForExit (); + if (p.ExitCode != 0) + throw new Exception ("Pdf generation failed with exit code:" + p.ExitCode); } using (StreamReader sr = new StreamReader (fo.FullName)) { diff --git a/web/Formatters/TexFormatter.cs b/web/Formatters/SimpleFormatter.cs similarity index 100% rename from web/Formatters/TexFormatter.cs rename to web/Formatters/SimpleFormatter.cs diff --git a/web/Global.asax.cs b/web/Global.asax.cs index c361fc87..a47881b3 100644 --- a/web/Global.asax.cs +++ b/web/Global.asax.cs @@ -52,23 +52,6 @@ namespace Yavsc ); RegisterRoutes (RouteTable.Routes); - Error += HandleError; } - - void HandleError (object sender, EventArgs e) - { - if (Server.GetLastError ().GetBaseException () is System.Web.HttpRequestValidationException) { - Response.Clear (); - Response.Write ("Invalid characters.
"); - Response.Write ("You may want to use your " + - "browser to go back to your form. " + - "You also can hit the url referrer."); - Response.StatusCode = 200; - Response.End (); - } - } - } } diff --git a/web/Models/App.master b/web/Models/App.master index eb8e7726..8a1a2f6d 100644 --- a/web/Models/App.master +++ b/web/Models/App.master @@ -35,6 +35,8 @@