QDjango
 All Classes Functions Typedefs Enumerations Enumerator Modules Pages
QDjangoScript.h
1 /*
2  * Copyright (C) 2010-2014 Jeremy LainĂ©
3  * Contact: https://github.com/jlaine/qdjango
4  *
5  * This file is part of the QDjango Library.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  */
17 
18 #ifndef QDJANGO_SCRIPT_H
19 #define QDJANGO_SCRIPT_H
20 
21 #include <QtScript/QScriptValue>
22 #include <QtScript/QScriptEngine>
23 
24 #include "QDjango.h"
25 #include "QDjangoQuerySet.h"
26 #include "QDjangoScript_p.h"
27 
28 Q_DECLARE_METATYPE(QDjangoWhere)
29 
30 
35 class QDJANGO_EXPORT QDjangoScript
36 {
37 public:
38  template <class T>
39  static void registerModel(QScriptEngine *engine);
40  static void registerWhere(QScriptEngine *engine);
41 };
42 
47 template <class T>
48 void QDjangoScript::registerModel(QScriptEngine *engine)
49 {
50  QDjango::registerModel<T>();
51 
52  QScriptValue querysetProto = engine->newObject();
53  querysetProto.setProperty("all", engine->newFunction(QDjangoQuerySet_all<T>));
54  querysetProto.setProperty("at", engine->newFunction(QDjangoQuerySet_at<T>));
55  querysetProto.setProperty("count", engine->newFunction(QDjangoQuerySet_count<T>));
56  querysetProto.setProperty("exclude", engine->newFunction(QDjangoQuerySet_exclude<T>));
57  querysetProto.setProperty("filter", engine->newFunction(QDjangoQuerySet_filter<T>));
58  querysetProto.setProperty("get", engine->newFunction(QDjangoQuerySet_get<T>));
59  querysetProto.setProperty("limit", engine->newFunction(QDjangoQuerySet_limit<T>));
60  querysetProto.setProperty("remove", engine->newFunction(QDjangoQuerySet_remove<T>));
61  querysetProto.setProperty("size", engine->newFunction(QDjangoQuerySet_size<T>));
62  querysetProto.setProperty("toString", engine->newFunction(QDjangoQuerySet_toString<T>));
63  engine->setDefaultPrototype(qMetaTypeId< QDjangoQuerySet<T> >(), querysetProto);
64 
66  QScriptValue value = engine->newQMetaObject(&T::staticMetaObject, engine->newFunction(QDjangoModel_new<T>));
67  value.setProperty("objects", engine->toScriptValue(qs));
68  engine->globalObject().setProperty(T::staticMetaObject.className(), value);
69 }
70 
71 #endif
The QDjangoScript class provides static methods for making models scriptable.
Definition: QDjangoScript.h:35
The QDjangoWhere class expresses an SQL constraint.
Definition: QDjangoWhere.h:40
static void registerModel(QScriptEngine *engine)
Definition: QDjangoScript.h:48
The QDjangoQuerySet class is a template class for performing database queries.
Definition: QDjangoQuerySet.h:46