root/trunk/midgard/apis/python/py_midgard_user.c

Revision 24812, 10.1 kB (checked in by piotras, 2 months ago)

Register delete method. Refs #1582

Line 
1 /*
2  * Copyright (C) 2007, 2009 Piotr Pokora <piotrek.pokora@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #define NO_IMPORT_PYGOBJECT
24 #include "py_midgard.h"
25
26 PyTypeObject G_GNUC_INTERNAL Pymidgard_user_Type;
27
28 #define USER_DEBUG(__name) \
29         CHECK_MGD; \
30         CLASS_METHOD_DEBUG(Pymidgard_user_Type.tp_name, __name);
31
32 static int __pyobject_to_person(PyObject *arg, gpointer *ptr)
33 {
34         if(!PyObject_IsTrue(arg))
35                 goto THROW_EXCEPTION;
36
37         PyGObject *pobj = (PyGObject *)arg;
38         if(pobj && pobj->obj != NULL) {
39                
40                 if(!G_IS_OBJECT(pobj->obj))
41                         goto THROW_EXCEPTION;
42
43                 const gchar *cname =
44                         G_OBJECT_TYPE_NAME(G_OBJECT(pobj->obj));       
45                 if(!g_str_equal(cname, "midgard_person"))
46                         goto THROW_EXCEPTION;
47                        
48         } else {
49                
50                 goto THROW_EXCEPTION;
51         }
52
53         return 1;
54
55 THROW_EXCEPTION:
56         PyErr_SetString(PyExc_TypeError, "Expected object");
57         return 0;
58 }
59
60 static int
61 __user_constructor(PyGObject *self, PyObject *args, PyObject *kwargs)
62 {
63         USER_DEBUG("__init__");
64
65         PyObject *props = NULL;
66         if(!PyArg_ParseTuple(args, "|O", &props))
67                 return -1;
68
69         guint n_params;
70         GParameter *params = _py_midgard_parameters_from_args(props, &n_params);
71         MidgardConnection *mgd = _py_midgard_connection_singleton_get();
72
73         MidgardUser *user = midgard_user_new (mgd, n_params, params);
74         FREE_PARAMETERS (n_params, params);
75
76         if(!user) return -1;
77
78         self->obj = G_OBJECT(user);
79
80         return 0;
81 }
82
83 static PyObject *
84 pymidgard_user_auth(PyGObject *self, PyObject *args)
85 {
86         USER_DEBUG("auth");
87         const gchar *login, *pass, *sitegroup;
88         gboolean trusted = FALSE;
89         if(!PyArg_ParseTuple(args, "ss|zb", &login, &pass, &sitegroup, &trusted))
90                 return NULL;
91
92         MidgardConnection *mgd =
93                 _py_midgard_connection_singleton_get();
94
95         MidgardUser *user =
96                 midgard_user_auth(mgd, login, pass, sitegroup, trusted);
97
98         if(user == NULL)
99                 Py_RETURN_NONE;
100        
101         GValue gval = {0, };
102         g_value_init(&gval, G_TYPE_OBJECT);
103         g_value_set_object(&gval, G_OBJECT(user));
104         PyObject *pval = pyg_value_as_pyobject((const GValue *)&gval, FALSE);
105        
106         return pval;
107 }
108
109 static PyObject *
110 pymidgard_user_is_user(PyGObject *self, PyObject *args)
111 {
112         USER_DEBUG("is_user");
113         if(!PyArg_ParseTuple(args, ""))
114                 return NULL;
115
116         MidgardUser *user = MIDGARD_USER(self->obj);
117
118         g_assert(user != NULL);
119
120         if(midgard_user_is_user(user))
121                 Py_RETURN_TRUE;
122
123         Py_RETURN_FALSE;
124 }
125
126 static PyObject *
127 pymidgard_user_is_admin(PyGObject *self, PyObject *args)
128 {
129         USER_DEBUG("is_admin");
130         if(!PyArg_ParseTuple(args, ""))
131                 return NULL;
132
133         MidgardUser *user = MIDGARD_USER(self->obj);
134
135         g_assert(user != NULL);
136
137         if(midgard_user_is_admin(user))
138                 Py_RETURN_TRUE;
139
140         Py_RETURN_FALSE;
141 }
142
143 static PyObject *
144 pymidgard_user_set_active(PyGObject *self, PyObject *args)
145 {
146         USER_DEBUG("set_active");
147         gboolean flag = FALSE;
148         if(!PyArg_ParseTuple(args, "b", &flag))
149                 return NULL;
150
151         MidgardUser *user = MIDGARD_USER(self->obj);
152
153         g_assert(user != NULL);
154
155         if(midgard_user_set_active(user, flag))
156                 Py_RETURN_TRUE;
157
158         Py_RETURN_FALSE;
159 }
160
161 static PyObject *
162 pymidgard_user_get_person(PyGObject *self, PyObject *args)
163 {
164         USER_DEBUG("get_person");
165        
166         if(!PyArg_ParseTuple(args, ""))
167                 return NULL;
168        
169         MidgardObject *person = midgard_user_get_person(MIDGARD_USER(self->obj));
170        
171         if(person == NULL)
172                 Py_RETURN_NONE;
173
174         GValue gval = {0, };
175         g_value_init(&gval, G_TYPE_OBJECT);
176         g_value_set_object(&gval, G_OBJECT(person));
177         PyObject *pval = pyg_value_as_pyobject((const GValue *)&gval, FALSE);
178        
179         return pval;
180 }
181
182 static PyObject *
183 pymidgard_user_get(PyGObject *self, PyObject *args)
184 {
185         USER_DEBUG("get");
186
187         PyObject *props;
188         if(!PyArg_ParseTuple(args, "O", &props))
189                 return NULL;
190
191         guint n_params;
192         GParameter *params = _py_midgard_parameters_from_args(props, &n_params);
193         MidgardConnection *mgd = _py_midgard_connection_singleton_get();
194
195         MidgardUser *user = midgard_user_get (mgd, n_params, params);
196         FREE_PARAMETERS (n_params, params);
197
198         if(user == NULL)
199                 Py_RETURN_NONE;
200
201         GValue gval = {0, };
202         g_value_init(&gval, G_TYPE_OBJECT);
203         g_value_set_object(&gval, G_OBJECT(user));
204         PyObject *pval = pyg_value_as_pyobject((const GValue *)&gval, FALSE);
205
206         return pval;
207 }
208
209 static PyObject *
210 pymidgard_user_log_in(PyGObject *self, PyObject *args)
211 {
212         USER_DEBUG("login");
213 g_print ("LOGIN ");     
214         MidgardUser *user = MIDGARD_USER(self->obj);
215
216         if(midgard_user_login(user))
217                 Py_RETURN_TRUE;
218
219         Py_RETURN_FALSE;
220 }
221
222 static PyObject *
223 pymidgard_user_logout(PyGObject *self, PyObject *args)
224 {
225         USER_DEBUG("logout");
226        
227         MidgardUser *user = MIDGARD_USER(self->obj);
228
229         if(midgard_user_logout(user))
230                 Py_RETURN_TRUE;
231
232         Py_RETURN_FALSE;
233 }
234
235 static PyObject *
236 pymidgard_user_query(PyGObject *self, PyObject *args)
237 {
238         USER_DEBUG("query");
239      
240         PyObject *props;       
241         if(!PyArg_ParseTuple(args, "O", &props))
242                         return NULL;
243
244         // MidgardObject *mobject = MIDGARD_OBJECT(self->obj);
245         guint n_params;
246         GParameter *params = _py_midgard_parameters_from_args(props, &n_params);
247         MidgardConnection *mgd = _py_midgard_connection_singleton_get();
248        
249         MidgardUser **users = midgard_user_query(mgd, n_params, params);
250         FREE_PARAMETERS(n_params, params);
251
252         PyObject *list = PyTuple_New(n_params);
253
254         if(!users)
255                 return list;
256                        
257         OBJECTS2LIST(users, list);
258         g_free(users);
259
260         return list;
261 }
262
263 static PyObject *
264 pymidgard_user_create(PyGObject *self, PyObject *args)
265 {
266         USER_DEBUG("create");
267
268         MidgardUser *user = MIDGARD_USER(self->obj);
269
270         if(midgard_user_create(user))
271                 Py_RETURN_TRUE;
272
273         Py_RETURN_FALSE;
274 }
275
276 static PyObject *
277 pymidgard_user_update(PyGObject *self, PyObject *args)
278 {
279         USER_DEBUG("update");
280
281         MidgardUser *user = MIDGARD_USER(self->obj);
282
283         if(midgard_user_update(user))
284                 Py_RETURN_TRUE;
285
286         Py_RETURN_FALSE;
287 }
288
289 static PyObject *
290 pymidgard_user_delete (PyGObject *self, PyObject *args)
291 {
292         USER_DEBUG ("delete");
293
294         MidgardUser *user = MIDGARD_USER (self->obj);
295
296         if(midgard_user_delete (user))
297                 Py_RETURN_TRUE;
298
299         Py_RETURN_FALSE;
300 }
301
302 static PyObject *
303 pymidgard_user_set_person(PyGObject *self, PyObject *args)
304 {
305         USER_DEBUG("set_person");
306
307         PyObject *pobject;
308         if(!PyArg_ParseTuple(args, "O", &pobject))
309                 return NULL;
310
311         MidgardUser *user = MIDGARD_USER(self->obj);
312         MidgardObject *person = MIDGARD_OBJECT (((PyGObject *)pobject)->obj);
313
314         if (!person)
315                 Py_RETURN_FALSE;
316
317         if (midgard_user_set_person(user, person))
318                 Py_RETURN_TRUE;
319
320         Py_RETURN_FALSE;
321 }
322
323 static PyMethodDef pymidgard_user_methods[] = {
324         { "get", (PyCFunction)pymidgard_user_get, METH_VARARGS | METH_STATIC },
325         { "log_in", (PyCFunction)pymidgard_user_log_in, METH_NOARGS },
326         { "logout", (PyCFunction)pymidgard_user_logout, METH_NOARGS },
327         { "query", (PyCFunction)pymidgard_user_query, METH_VARARGS | METH_STATIC },
328         { "create", (PyCFunction)pymidgard_user_create, METH_NOARGS },
329         { "update", (PyCFunction)pymidgard_user_update, METH_NOARGS },
330         { "delete", (PyCFunction)pymidgard_user_delete, METH_NOARGS },
331         { "auth", (PyCFunction)pymidgard_user_auth, METH_VARARGS | METH_STATIC },
332         { "is_user", (PyCFunction)pymidgard_user_is_user, METH_VARARGS },
333         { "is_admin", (PyCFunction)pymidgard_user_is_admin, METH_VARARGS },
334         { "set_active", (PyCFunction)pymidgard_user_set_active, METH_VARARGS },
335         { "set_person", (PyCFunction)pymidgard_user_set_person, METH_VARARGS },
336         { "get_person", (PyCFunction)pymidgard_user_get_person, METH_VARARGS },
337         { NULL, NULL, 0 }
338 };
339
340 PyTypeObject G_GNUC_INTERNAL Pymidgard_user_Type = {
341     PyObject_HEAD_INIT(NULL)
342     0,                                 /* ob_size */
343     "user",                   /* tp_name */
344     sizeof(PyGObject),          /* tp_basicsize */
345     0,                                 /* tp_itemsize */
346     /* methods */
347     (destructor)_py_midgard_gobject_destructor,        /* tp_dealloc */
348     (printfunc)0,                      /* tp_print */
349
350     (getattrfunc)0,       /* tp_getattr */
351     (setattrfunc)0,       /* tp_setattr */
352     (cmpfunc)0,           /* tp_compare */
353     (reprfunc)0,             /* tp_repr */
354     (PyNumberMethods*)0,     /* tp_as_number */
355     (PySequenceMethods*)0, /* tp_as_sequence */
356     (PyMappingMethods*)0,   /* tp_as_mapping */
357     (hashfunc)0,             /* tp_hash */
358     (ternaryfunc)0,          /* tp_call */
359     (reprfunc)0,              /* tp_str */
360     _py_midgard_get_object_attribute, /* tp_getattro */
361     _py_midgard_set_object_attribute, /* tp_setattro */
362     (PyBufferProcs*)0,  /* tp_as_buffer */
363     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,                      /* tp_flags */
364     NULL,                        /* Documentation string */
365     (traverseproc)0,     /* tp_traverse */
366     (inquiry)0,             /* tp_clear */
367     (richcmpfunc)0,   /* tp_richcompare */
368     offsetof(PyGObject, weakreflist),             /* tp_weaklistoffset */
369     (getiterfunc)0,          /* tp_iter */
370     (iternextfunc)0,     /* tp_iternext */
371     pymidgard_user_methods, /* tp_methods */
372     (struct PyMemberDef*)0,              /* tp_members */
373     (struct PyGetSetDef*)0,  /* tp_getset */
374     NULL,                              /* tp_base */
375     NULL,                              /* tp_dict */
376     (descrgetfunc)0,    /* tp_descr_get */
377     (descrsetfunc)0,    /* tp_descr_set */
378     offsetof(PyGObject, inst_dict),                 /* tp_dictoffset */
379     (initproc)__user_constructor,              /* tp_init */
380     (allocfunc)0,           /* tp_alloc */
381     (newfunc)0,               /* tp_new */
382     (freefunc)0,             /* tp_free */
383     (inquiry)0              /* tp_is_gc */
384 };
385
386 void py_midgard_user_register_class(
387                 PyObject *d, gpointer pygobject_type)
388 {
389         pygobject_register_class(d,
390                         "user",
391                         MIDGARD_TYPE_USER,
392                         &Pymidgard_user_Type,
393                         Py_BuildValue("(O)", pygobject_type));
394 }
Note: See TracBrowser for help on using the browser.