GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Midgard Components Framework 3rd generation
Homepage: http://www.midgard-project.org
Clone URL: git://github.com/bergie/midcom.git
Python API example courtesy of Piotras
bergie (author)
Thu Mar 20 05:28:19 -0700 2008
commit  539c45db79cf0d06e9527db7e6eaffb05845b7ea
tree    b17014592a9e469799f0151933840555ae62af41
parent  4a5c48cf3b4a4caaacc6a9f1b6b22cbe8f21c56b
...
86
87
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
90
91
...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
0
@@ -86,6 +86,65 @@ These steps are all you need to do to have new Midgard objects at your disposal.
0
     /// etc
0
     ?>
0
 
0
+### And the same in Python
0
+
0
+Since Midgard is a multilingual framework, same content can be accessed between PHP, Python and C APIs. Here is the Python version of the PHP example above:
0
+
0
+ # -*- coding: utf-8 -*-
0
+ import os, sys
0
+ import _midgard as midgard
0
+
0
+ # new config instance
0
+ config = midgard.config()
0
+
0
+ # read default configuration
0
+ opened = config.read_file('midgard', True)
0
+
0
+ if opened == False:
0
+ raise SystemError("Failed to open default configuration file")
0
+
0
+ # try to connect
0
+ cnc = midgard.connection()
0
+ connected = cnc.open_config(config)
0
+
0
+ # Create a new person
0
+ reporter = midgard.mgdschema.midgard_person()
0
+ reporter.set_property("firstname", "John")
0
+ reporter.set_property("lastname", "Smith")
0
+
0
+ # Save it to the database
0
+ reporter.create()
0
+
0
+ # Now it has an UUID
0
+ reporter_guid = reporter.get_property("guid")
0
+ print(reporter_guid)
0
+
0
+ # And it can be fetched from the database
0
+ qb = midgard.query_builder("midgard_person")
0
+
0
+ # We have multiple ways to query objects
0
+ qb.add_constraint("lastname", "LIKE", "Smi%")
0
+ list = qb.execute()
0
+ print(list)
0
+
0
+ #Create a new article
0
+ article = midgard.mgdschema.midgard_article()
0
+ article.set_property("title", "MidCOM is cool")
0
+ metadata = article.get_property("metadata")
0
+ metadata.set_property("authors", reporter_guid)
0
+ article.create()
0
+
0
+ # Extend the article with new properties
0
+ article.set_parameter("namespace", "key", "value")
0
+
0
+ # Localize the article to Finnish
0
+ cnc.set_lang("fi")
0
+ article.set_property("title", "MidCOM on Šltsin magee")
0
+ article.update()
0
+
0
+ # Delete the Finnish translation
0
+ article.delete()
0
+
0
 ## A dynamic admin interface
0
 
0
 Midgard comes with [Asgard][18], an automated administrative interface which provides a full editing tool to all our installed MgdSchemas. If your define a tree model for your storage, it will even provide a nice navigation tree for them. For example:

Comments

    No one has commented yet.