public static string RunScript(string dbname, string script)
        {
            var engine = Python.CreateEngine();
            var ms     = new MemoryStream();
            var sw     = new StreamWriter(ms);

            engine.Runtime.IO.SetOutput(ms, sw);
            engine.Runtime.IO.SetErrorOutput(ms, sw);
            var sc = engine.CreateScriptSourceFromString(script);

            try
            {
                var code  = sc.Compile();
                var scope = engine.CreateScope();
                var pe    = new PythonEvents(dbname);
                scope.SetVariable("model", pe);
                var qf = new QueryFunctions(pe.db);
                scope.SetVariable("q", qf);
                code.Execute(scope);
                pe.db.SubmitChanges();
                ms.Position = 0;
                var sr = new StreamReader(ms);
                var s  = sr.ReadToEnd();
                return(s);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
        public string CallScript(string scriptname)
        {
            var script = db.ContentOfTypePythonScript(scriptname);
            var engine = Python.CreateEngine();
            var ms     = new MemoryStream();
            var sw     = new StreamWriter(ms);

            engine.Runtime.IO.SetOutput(ms, sw);
            engine.Runtime.IO.SetErrorOutput(ms, sw);
            var sc    = engine.CreateScriptSourceFromString(script);
            var code  = sc.Compile();
            var scope = engine.CreateScope();
            var pe    = new PythonEvents(db.Host);

            scope.SetVariable("model", pe);
            var qf = new QueryFunctions(pe.db);

            scope.SetVariable("q", qf);
            code.Execute(scope);
            pe.db.SubmitChanges();
            ms.Position = 0;
            var sr = new StreamReader(ms);
            var s  = sr.ReadToEnd();

            return(s);
        }
Beispiel #3
0
        public string RenderTemplate(string source, object data)
        {
            PythonEvents.RegisterHelpers(db);
            var template = Handlebars.Compile(source);
            var result   = template(data);

            return(result);
        }
        public static string RunScript(string dbname, string script, DateTime time)
        {
            var pe = new PythonEvents(dbname)
            {
                ScheduledTime = time.ToString("HHmm")
            };

            return(ExecutePython(script, pe));
        }
Beispiel #5
0
 public static string RunScript(CMSDataContext db, string script)
 {
     var engine = Python.CreateEngine();
     var ms = new MemoryStream();
     var sw = new StreamWriter(ms);
     engine.Runtime.IO.SetOutput(ms, sw);
     engine.Runtime.IO.SetErrorOutput(ms, sw);
     var sc = engine.CreateScriptSourceFromString(script);
     var code = sc.Compile();
     var scope = engine.CreateScope();
     var pe = new PythonEvents(db);
     scope.SetVariable("model", pe);
     code.Execute(scope);
     return ms.ToString();
 }
        private static string ExecutePython(string scriptContent, PythonEvents model)
        {
            var engine = Python.CreateEngine();

            using (var ms = new MemoryStream())
                using (var sw = new StreamWriter(ms))
                {
                    engine.Runtime.IO.SetOutput(ms, sw);
                    engine.Runtime.IO.SetErrorOutput(ms, sw);

                    try
                    {
                        var sc   = engine.CreateScriptSourceFromString(scriptContent);
                        var code = sc.Compile();

                        var scope = engine.CreateScope();
                        scope.SetVariable("model", model);
                        scope.SetVariable("Data", model.Data);

                        var qf = new QueryFunctions(model.db, model.dictionary);
                        scope.SetVariable("q", qf);
                        code.Execute(scope);

                        model.db.SubmitChanges();

                        ms.Position = 0;

                        using (var sr = new StreamReader(ms))
                        {
                            var s = sr.ReadToEnd();
                            return(s);
                        }
                    }
                    catch (Exception ex)
                    {
                        var err = engine.GetService <ExceptionOperations>().FormatException(ex);
                        throw new Exception(err);
                    }
                }
        }
Beispiel #7
0
        private static string ExecutePython(string scriptContent, PythonEvents model)
        {
            var engine = Python.CreateEngine();

            using (var ms = new MemoryStream())
            using (var sw = new StreamWriter(ms))
            {
                engine.Runtime.IO.SetOutput(ms, sw);
                engine.Runtime.IO.SetErrorOutput(ms, sw);

                try
                {
                    var sc = engine.CreateScriptSourceFromString(scriptContent);
                    var code = sc.Compile();

                    var scope = engine.CreateScope();
                    scope.SetVariable("model", model);

                    var qf = new QueryFunctions(model.db);
                    scope.SetVariable("q", qf);
                    code.Execute(scope);

                    model.db.SubmitChanges();

                    ms.Position = 0;

                    using (var sr = new StreamReader(ms))
                    {
                        var s = sr.ReadToEnd();
                        return s;
                    }
                }
                catch (Exception ex)
                {
                    var err = engine.GetService<ExceptionOperations>().FormatException(ex);
                    throw new Exception(err);
                }
            }
        }
Beispiel #8
0
 public ActionResult RunScript(string id)
 {
     try
     {
         var script = DbUtil.Db.Content(id);
         var pe = new PythonEvents(DbUtil.Db, id, script.Body);
         pe.instance.Run();
     }
     catch (Exception e)
     {
         return Content(e.Message);
     }
     return Content("done");
 }
Beispiel #9
0
        public ActionResult VoteLinkSg(string id, string message, bool? confirm, FormCollection formCollection)
        {
            var li = new LinkInfo(votelinkSTR, confirmSTR, id);
            if (li.error.HasValue())
                return Message(li.error);

            try
            {
                var smallgroup = li.a[4];

                if (!li.oid.HasValue)
                    throw new Exception("orgid missing");

                if (!li.pid.HasValue)
                    throw new Exception("peopleid missing");

                var q = (from pp in DbUtil.Db.People
                         where pp.PeopleId == li.pid
                         let org = DbUtil.Db.Organizations.SingleOrDefault(oo => oo.OrganizationId == li.oid)
                         let om = DbUtil.Db.OrganizationMembers.SingleOrDefault(oo => oo.OrganizationId == li.oid && oo.PeopleId == li.pid)
                         select new {p = pp, org, om}).Single();

                if (q.org == null && DbUtil.Db.Host == "trialdb")
                {
                    var oid = li.oid + Util.TrialDbOffset;
                    q = (from pp in DbUtil.Db.People
                         where pp.PeopleId == li.pid
                         let org = DbUtil.Db.Organizations.SingleOrDefault(oo => oo.OrganizationId == oid)
                         let om = DbUtil.Db.OrganizationMembers.SingleOrDefault(oo => oo.OrganizationId == oid && oo.PeopleId == li.pid)
                         select new {p = pp, org, om}).Single();
                }

                if (q.org == null)
                {
                    throw new Exception("org missing, bad link");
                }
                if ((q.org.RegistrationTypeId ?? RegistrationTypeCode.None) == RegistrationTypeCode.None)
                    throw new Exception("votelink is no longer active");

                if (q.om == null && q.org.Limit <= q.org.RegLimitCount(DbUtil.Db))
                    throw new Exception("sorry, maximum limit has been reached");

                if (q.om == null &&
                    (q.org.RegistrationClosed == true || q.org.OrganizationStatusId == OrgStatusCode.Inactive))
                    throw new Exception("sorry, registration has been closed");

                var setting = DbUtil.Db.CreateRegistrationSettings(li.oid.Value);
                if (IsSmallGroupFilled(setting, li.oid.Value, smallgroup))
                    throw new Exception("sorry, maximum limit has been reached for " + smallgroup);

                var omb = OrganizationMember.InsertOrgMembers(DbUtil.Db,
                    li.oid.Value, li.pid.Value, MemberTypeCode.Member, DateTime.Now, null, false);

                if (q.org.AddToSmallGroupScript.HasValue())
                {
                    var script = DbUtil.Db.Content(q.org.AddToSmallGroupScript);
                    if (script != null && script.Body.HasValue())
                    {
                        try
                        {
                            var pe = new PythonEvents(Util.Host, "RegisterEvent", script.Body);
                            pe.instance.AddToSmallGroup(smallgroup, omb);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                omb.AddToGroup(DbUtil.Db, smallgroup);
                li.ot.Used = true;
                DbUtil.Db.SubmitChanges();

                DbUtil.LogActivity($"{votelinkSTR}{confirmSTR}: {smallgroup}", li.oid, li.pid);

                if (confirm == true)
                {
                    var subject = Util.PickFirst(setting.Subject, "no subject");
                    var msg = Util.PickFirst(setting.Body, "no message");
                    msg = APIOrganization.MessageReplacements(DbUtil.Db, q.p, q.org.DivisionName, q.org.OrganizationId, q.org.OrganizationName, q.org.Location, msg);
                    msg = msg.Replace("{details}", smallgroup);
                    var NotifyIds = DbUtil.Db.StaffPeopleForOrg(q.org.OrganizationId);

                    try
                    {
                        DbUtil.Db.Email(NotifyIds[0].FromEmail, q.p, subject, msg); // send confirmation
                    }
                    catch (Exception ex)
                    {
                        DbUtil.Db.Email(q.p.FromEmail, NotifyIds,
                            q.org.OrganizationName,
                            "There was a problem sending confirmation from org: " + ex.Message);
                    }
                    DbUtil.Db.Email(q.p.FromEmail, NotifyIds,
                        q.org.OrganizationName,
                        $"{q.p.Name} has registered for {q.org.OrganizationName}<br>{smallgroup}<br>(from votelink)");
                }
            }
            catch (Exception ex)
            {
                DbUtil.LogActivity($"{votelinkSTR}{confirmSTR}Error: {ex.Message}", li.oid, li.pid);
                return Message(ex.Message);
            }

            return Message(message);
        }
Beispiel #10
0
        public ActionResult VoteLinkSg(string id, string message, bool? confirm)
        {
            if (!id.HasValue())
                return Content("bad link");

            var guid = id.ToGuid();
            if (guid == null)
                return Content("invalid link");
            var ot = DbUtil.Db.OneTimeLinks.SingleOrDefault(oo => oo.Id == guid.Value);
            if (ot == null)
                return Content("invalid link");
            if (ot.Used)
                return Content("link used");
            if (ot.Expires.HasValue && ot.Expires < DateTime.Now)
                return Content("link expired");
            var a = ot.Querystring.SplitStr(",", 5);
            var oid = a[0].ToInt();
            var pid = a[1].ToInt();
            var emailid = a[2].ToInt();
            var pre = a[3];
            var smallgroup = a[4];
            var q = (from pp in DbUtil.Db.People
                     where pp.PeopleId == pid
                     let org = DbUtil.Db.Organizations.SingleOrDefault(oo => oo.OrganizationId == oid)
                     let om = DbUtil.Db.OrganizationMembers.SingleOrDefault(oo => oo.OrganizationId == oid && oo.PeopleId == pid)
                     select new { p = pp, org = org, om = om }).Single();

            if (q.org == null)
                return Content("org missing, bad link");

            if (q.org.RegistrationTypeId == RegistrationTypeCode.None)
                return Content("votelink is no longer active");

            if (q.om == null && q.org.Limit <= q.org.MemberCount)
                return Content("sorry, maximum limit has been reached");

            if (q.om == null && (q.org.RegistrationClosed == true || q.org.OrganizationStatusId == OrgStatusCode.Inactive))
                return Content("sorry, registration has been closed");

            var setting = new Settings(q.org.RegSetting, DbUtil.Db, oid);
            if (IsSmallGroupFilled(setting, oid, smallgroup))
                return Content("sorry, maximum limit has been reached for " + smallgroup);

            var omb = q.om;
            omb = OrganizationMember.InsertOrgMembers(DbUtil.Db,
                 oid, pid, MemberTypeCode.Member, DateTime.Now, null, false);
            //DbUtil.Db.UpdateMainFellowship(oid);

            if (q.org.AddToSmallGroupScript.HasValue())
            {
                var script = DbUtil.Db.Content(q.org.AddToSmallGroupScript);
                if (script != null && script.Body.HasValue())
                {
                    try
                    {
                        var pe = new PythonEvents(DbUtil.Db, "RegisterEvent", script.Body);
                        pe.instance.AddToSmallGroup(smallgroup, omb);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            omb.AddToGroup(DbUtil.Db, smallgroup);
            omb.AddToGroup(DbUtil.Db, "emailid:" + emailid);
            ot.Used = true;
            DbUtil.Db.SubmitChanges();
            DbUtil.LogActivity("Votelink: {0}".Fmt(q.org.OrganizationName));

            if (confirm == true)
            {
                var subject = Util.PickFirst(setting.Subject, "no subject");
                var msg = Util.PickFirst(setting.Body, "no message");
                msg = CmsData.API.APIOrganization.MessageReplacements(DbUtil.Db, q.p, q.org.DivisionName, q.org.OrganizationName, q.org.Location, msg);
                msg = msg.Replace("{details}", smallgroup);
                var NotifyIds = DbUtil.Db.StaffPeopleForOrg(q.org.OrganizationId);

                try
                {
                    DbUtil.Db.Email(NotifyIds[0].FromEmail, q.p, subject, msg); // send confirmation
                }
                catch (Exception ex)
                {
                    DbUtil.Db.Email(q.p.FromEmail, NotifyIds,
                                         q.org.OrganizationName,
                                         "There was a problem sending confirmation from org: " + ex.Message);
                }
                DbUtil.Db.Email(q.p.FromEmail, NotifyIds,
                          q.org.OrganizationName,
                          "{0} has registered for {1}<br>{2}<br>(from votelink)".Fmt(q.p.Name, q.org.OrganizationName, smallgroup));
            }

            return Content(message);
        }
Beispiel #11
0
        public ActionResult PyScriptForm()
        {
            try
            {
                var pe = new PythonEvents(Util.Host);
                foreach (var key in Request.Form.AllKeys)
                    pe.DictionaryAdd(key, Request.Form[key]);
                pe.HttpMethod = "post";

                var script = FetchPyScriptForm(pe.Dictionary("pyscript"));
                return Content(pe.RunScript(script));
            }
            catch (Exception ex)
            {
                return RedirectShowError(ex.Message);
            }
        }
Beispiel #12
0
        private static string ExecutePython(string scriptContent, PythonEvents model)
        {
            // we could consider only passing in an explicit IPythonApi to the script so that only things defined
            // on the interface are accessible to the script; however, I'm worried that we may have some scripts
            // that are using functions not defined on the API docs site (which is what the interface is based on)

            if (scriptContent.StartsWith("@# pyhtml #@"))
                scriptContent = RunPyHtml(scriptContent, model.pythonPath, model.pyrazorPath);

            var engine = Python.CreateEngine();

            using (var ms = new MemoryStream())
            using (var sw = new StreamWriter(ms))
            {
                engine.Runtime.IO.SetOutput(ms, sw);
                engine.Runtime.IO.SetErrorOutput(ms, sw);

                try
                {
                    var sc = engine.CreateScriptSourceFromString(scriptContent);
                    var code = sc.Compile();

                    var scope = engine.CreateScope();
                    scope.SetVariable("model", model);

                    var qf = new QueryFunctions(model.db);
                    scope.SetVariable("q", qf);
                    code.Execute(scope);

                    model.db.SubmitChanges();

                    ms.Position = 0;

                    using (var sr = new StreamReader(ms))
                    {
                        var s = sr.ReadToEnd();
                        return s;
                    }
                }
                catch (Exception ex)
                {
                    var err = engine.GetService<ExceptionOperations>().FormatException(ex);
                    throw new Exception(err);
                }
            }
        }
Beispiel #13
0
 public void AddToSmallGroup(CMSDataContext Db, OrganizationMember om, PythonEvents pe)
 {
     if (om == null)
         return;
     if (pe != null)
     {
         pe.instance.AddToSmallGroup(SmallGroup, om);
         om.Person.LogChanges(Db, om.PeopleId);
     }
     om.AddToGroup(Db, SmallGroup);
     if (MeetingTime.HasValue)
         Attend.MarkRegistered(Db, om.OrganizationId, om.PeopleId, MeetingTime.Value, 1);
 }
Beispiel #14
0
        public ActionResult Test(string id)
        {
            var script = System.IO.File.ReadAllText(Server.MapPath("~/test2.py"));
            var pe = new PythonEvents(Util.Host);
            pe.Data.value = "test";
            var s = pe.RunScript(script);

            return Content(s, "text/plain");
        }
Beispiel #15
0
 public static string RunScript(CMSDataContext db, string script)
 {
     var engine = Python.CreateEngine();
     var ms = new MemoryStream();
     var sw = new StreamWriter(ms);
     engine.Runtime.IO.SetOutput(ms, sw);
     engine.Runtime.IO.SetErrorOutput(ms, sw);
     var sc = engine.CreateScriptSourceFromString(script);
     var code = sc.Compile();
     var scope = engine.CreateScope();
     var pe = new PythonEvents(db);
     scope.SetVariable("model", pe);
     var qf = new QueryFunctions(db);
     scope.SetVariable("q", qf);
     code.Execute(scope);
     ms.Position = 0;
     var sr = new StreamReader(ms);
     var s = sr.ReadToEnd();
     return s;
 }
Beispiel #16
0
 public static string RunScript(string dbname, string script, DateTime time)
 {
     var pe = new PythonEvents(dbname) {ScheduledTime = time.ToString("HHmm")};
     return ExecutePython(script, pe);
 }
Beispiel #17
0
        public ActionResult PyScript(string name, string p1, string p2, string v1, string v2)
        {
            try
            {
                var script = DbUtil.Db.ContentOfTypePythonScript(name);
                if (!script.HasValue())
                    return Message("no script named " + name);

                if (script.Contains("model.Form"))
                    return Redirect("/PyScriptForm/" + name);
                script = script.Replace("@P1", p1 ?? "NULL")
                    .Replace("@P2", p2 ?? "NULL")
                    .Replace("V1", v1 ?? "None")
                    .Replace("V2", v2 ?? "None");

                var pe = new PythonEvents(Util.Host);

                foreach (var key in Request.QueryString.AllKeys)
                {
                    pe.DictionaryAdd(key, Request.QueryString[key]);
                }

                pe.RunScript(script);

                return View(pe);
            }
            catch (Exception ex)
            {
                return RedirectShowError(ex.Message);
            }
        }
Beispiel #18
0
        public void ParseSettings()
        {
            //            if (HttpContext.Current.Items.Contains("RegSettings"))
            //                return;
            var list = new Dictionary<int, Settings>();
            if (masterorgid.HasValue)
            {
                var q = from o in UserSelectClasses(masterorg)
                        select new {o.OrganizationId, o.RegSetting};
                foreach (var i in q)
                    list[i.OrganizationId] = new Settings(i.RegSetting, DbUtil.Db, i.OrganizationId);
                list[masterorg.OrganizationId] = new Settings(masterorg.RegSetting, DbUtil.Db, masterorg.OrganizationId);
            }
            else if (orgid == null)
                return;
            else if (org != null)
                list[orgid.Value] = new Settings(org.RegSetting, DbUtil.Db, orgid.Value);
            //            if (HttpContext.Current.Items.Contains("RegSettings"))
            //                return;
            HttpContext.Current.Items["RegSettings"] = list;

            if (org != null && org.AddToSmallGroupScript.HasValue())
            {
                var script = DbUtil.Db.Content(org.AddToSmallGroupScript);
                if (script != null && script.Body.HasValue())
                {
                    try
                    {
                        var pe = new PythonEvents(DbUtil.Db, "RegisterEvent", script.Body);
                        HttpContext.Current.Items["PythonEvents"] = pe;
                    }
                    catch (Exception ex)
                    {
                        org.AddToExtraData("Python.errors", ex.Message);
                        throw;
                    }
                }
            }
        }
Beispiel #19
0
        public ActionResult PyScriptForm(string name)
        {
            try
            {
                var script = FetchPyScriptForm(name);

                if (!script.HasValue())
                    return Message("no script named " + name);
                var pe = new PythonEvents(Util.Host);
                foreach (var key in Request.QueryString.AllKeys)
                    pe.DictionaryAdd(key, Request.QueryString[key]);
                pe.DictionaryAdd("pyscript", name);
                pe.HttpMethod = "get";
                pe.RunScript(script);
                return View(pe);
            }
            catch (Exception ex)
            {
                return RedirectShowError(ex.Message);
            }
        }