def listCategoryItems(category_id): """ List Items for the category using provided category ID Params: category_id: category ID Returns: if category ID is valid, a web-page presentation displaying all items for the category. Otherwise, a redirect to the list categories is done. """ a_category = session.query(Category).filter_by(id=category_id).first() if a_category: print "Found category "+a_category.name items = session.query(CategoryItem).filter_by( category=a_category).order_by(CategoryItem.title).all() return render_template("categoryItems.html", categories=categories, category=a_category, categoryItems=items) else: print "Invalid category ID "+str(category_id) return redirect(url_for('listCategories'))
def categoryItemModify(category_id, item_id): """ Modify existing item for the category. User is required to be logged. If not, the method will redirect to a login page. Params: category_id: category ID item_id: ID of the item to be modified Returns: on GET, return a web-page presentation to modify the item. on POST with Save button pressed, update database fields that changed and redirect to a list items for the category web-page. on other POST requests, redirect to a list items for the category web-page without database update. """ if 'user_id' not in login_session: return redirect('/login') editedItem = session.query(CategoryItem).filter_by(id=item_id).first() if editedItem: if editedItem.user_id != login_session['user_id']: print editedItem.title+" cannot be edited - wrong user!" return redirect(url_for('listCategoryItems', category_id=category_id)) if request.method == 'POST': if 'Save' in request.form: if request.form['title']: editedItem.title = request.form['title'] if request.form['description']: editedItem.description = request.form['description'] session.add(editedItem) session.commit() print "category item "+editedItem.title+" edited!" return redirect(url_for('listCategoryItems', category_id=category_id)) else: return render_template('categoryItemModify.html', item=editedItem) else: return redirect(url_for('listCategoryItems', category_id=category_id))
def setupUi(self, Dialog): Dialog.setObjectName("Connect") Dialog.resize(400, 300) self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(325, 275, 75, 23)) self.pushButton.setObjectName("pushButton") self.pushButton1 = QtWidgets.QPushButton(Dialog) self.pushButton1.setGeometry(QtCore.QRect(225, 275, 75, 23)) self.pushButton1.setObjectName("pushButton1") self.pushButton2 = QtWidgets.QPushButton(Dialog) self.pushButton2.setGeometry(QtCore.QRect(310, 15, 75, 23)) self.pushButton2.setObjectName("pushButton2") self.textEteil1 = QtWidgets.QTableWidget(Dialog) self.textEteil1.setGeometry(QtCore.QRect(15, 40, 370, 220)) self.textEteil1.setObjectName('textEteil1') self.textEteil1.setShowGrid(True) projects = session.query('Project') for project in projects: i = len(project['name']) self.textEteil1.setRowCount(i) self.textEteil1.setColumnCount(2) self.textEteil1.setHorizontalHeaderLabels(['Name', 'Type']) self.textEteil1.horizontalHeader().setSectionResizeMode( QHeaderView.Stretch) self.textEteil1.setEditTriggers(QAbstractItemView.NoEditTriggers) self.textEteil1.setSelectionBehavior(QAbstractItemView.SelectRows) self.textEteil = QtWidgets.QLineEdit(Dialog) self.textEteil.setGeometry(QtCore.QRect(15, 15, 260, 23)) self.textEteil.setObjectName('textEteil') self.textEteil.setPlaceholderText('Ftrack') self.textEteil.setReadOnly(True) self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog)
def categoryItemDelete(category_id, item_id): """ Delete the item for the category. User is required to be logged. If not, the method will redirect to a login page. Params: category_id: category ID item_id: ID of the item to be deleted Returns: on GET, return a web-page presentation to confirm the item deletion. on POST with Delete button pressed, delete item from the database and redirect to a list items for the category web-page. on other POST requests, redirect to a list items for the category web-page without user deletion. """ if 'user_id' not in login_session: return redirect('/login') deletedItem = session.query(CategoryItem).filter_by(id=item_id).first() if deletedItem: if deletedItem.user_id != login_session['user_id']: print deletedItem.title+" cannot be deleted - wrong user!" return redirect(url_for('listCategoryItems', category_id=category_id)) if request.method == 'POST': if 'Delete' in request.form: session.delete(deletedItem) session.commit() print "category item "+deletedItem.title+" deleted!" return redirect(url_for('listCategoryItems', category_id=category_id)) else: return render_template('categoryItemDelete.html', item=deletedItem) else: return redirect(url_for('listCategoryItems', category_id=category_id))
def menuItemJson(category_id, item_id): item = session.query(CategoryItem).filter_by(id=item_id).first() if item: return jsonify(categoryItem=item.serialize) return make_response(json.dumps('Not Found'), 404)
def categoryItemsJson(category_id): items = session.query(CategoryItem).filter_by( category_id=category_id).order_by(CategoryItem.title).all() if items: return jsonify(categoryItems=[i.serialize for i in items]) return make_response(json.dumps('Not Found'), 404)
url_for, request, redirect, jsonify, make_response ) from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import cgi import urllib import json from database_setup import Category, CategoryItem from login import app, session, login_session # categories do not change, can be read once at the app startup categories = session.query(Category).order_by(Category.name).all() for category in categories: print 'name=' + category.name + ';id=' + str(category.id) ########################################################################## # JSON APIs @app.route('/JSON') @app.route('/JSON/categories') def categoriesJson(): return jsonify(categories=[i.serialize for i in categories]) @app.route('/JSON/categories/<int:category_id>') def categoryJson(category_id): item = session.query(Category).filter_by(id=category_id).first() if item: