def update_user_inbox_for_reminders(uid): """ Updates user's inbox with any reminders that should have arrived @param uid: user id @return: integer number of new expired reminders """ now = convert_datestruct_to_datetext(localtime()) reminder_status = CFG_WEBMESSAGE_STATUS_CODE['REMINDER'] new_status = CFG_WEBMESSAGE_STATUS_CODE['NEW'] expired_reminders = db.session.query(UserMsgMESSAGE.id_msgMESSAGE).\ join(UserMsgMESSAGE.message).\ filter(db.and_( UserMsgMESSAGE.id_user_to==uid, UserMsgMESSAGE.status.like(reminder_status), MsgMESSAGE.received_date<=datetime.now() #MsgMESSAGE.received_date<=db.func.current_timestamp() )).all() if len(expired_reminders): filter = db.and_( UserMsgMESSAGE.id_user_to==uid, UserMsgMESSAGE.id_msgMESSAGE.in_( [i for i, in expired_reminders])) res = UserMsgMESSAGE.query.filter(filter).\ update({UserMsgMESSAGE.status: new_status}, synchronize_session='fetch') return res
def reviews(recid): """Display reviews.""" from invenio.modules.access.local_config import VIEWRESTRCOLL from invenio.modules.access.mailcookie import mail_cookie_create_authorize_action from .api import check_user_can_view_comments auth_code, auth_msg = check_user_can_view_comments(current_user, recid) if auth_code and current_user.is_guest: cookie = mail_cookie_create_authorize_action(VIEWRESTRCOLL, {"collection": g.collection}) url_args = {"action": cookie, "ln": g.ln, "referer": request.referrer} flash(_("Authorization failure"), "error") return redirect(url_for("webaccount.login", **url_args)) elif auth_code: flash(auth_msg, "error") abort(401) comments = ( CmtRECORDCOMMENT.query.filter( db.and_( CmtRECORDCOMMENT.id_bibrec == recid, CmtRECORDCOMMENT.in_reply_to_id_cmtRECORDCOMMENT == 0, CmtRECORDCOMMENT.star_score > 0, ) ) .order_by(CmtRECORDCOMMENT.date_creation) .all() ) return render_template("comments/reviews.html", comments=comments)
def comments(recid): """Display comments.""" from invenio.modules.access.local_config import VIEWRESTRCOLL from invenio.modules.access.mailcookie import \ mail_cookie_create_authorize_action from .api import check_user_can_view_comments auth_code, auth_msg = check_user_can_view_comments(current_user, recid) if auth_code and current_user.is_guest: cookie = mail_cookie_create_authorize_action(VIEWRESTRCOLL, { 'collection': g.collection}) url_args = {'action': cookie, 'ln': g.ln, 'referer': request.referrer} flash(_("Authorization failure"), 'error') return redirect(url_for('webaccount.login', **url_args)) elif auth_code: flash(auth_msg, 'error') abort(401) # FIXME check restricted discussion comments = CmtRECORDCOMMENT.query.filter(db.and_( CmtRECORDCOMMENT.id_bibrec == recid, CmtRECORDCOMMENT.in_reply_to_id_cmtRECORDCOMMENT == 0, CmtRECORDCOMMENT.star_score == 0 )).order_by(CmtRECORDCOMMENT.date_creation).all() return render_template('comments/comments.html', comments=comments, option='comments')
def comments(recid): """Display comments.""" from invenio.modules.access.local_config import VIEWRESTRCOLL from invenio.modules.access.mailcookie import \ mail_cookie_create_authorize_action from .api import check_user_can_view_comments auth_code, auth_msg = check_user_can_view_comments(current_user, recid) if auth_code and current_user.is_guest: cookie = mail_cookie_create_authorize_action( VIEWRESTRCOLL, {'collection': g.collection}) url_args = {'action': cookie, 'ln': g.ln, 'referer': request.referrer} flash(_("Authorization failure"), 'error') return redirect(url_for('webaccount.login', **url_args)) elif auth_code: flash(auth_msg, 'error') abort(401) # FIXME check restricted discussion comments = CmtRECORDCOMMENT.query.filter( db.and_(CmtRECORDCOMMENT.id_bibrec == recid, CmtRECORDCOMMENT.in_reply_to_id_cmtRECORDCOMMENT == 0, CmtRECORDCOMMENT.star_score == 0)).order_by( CmtRECORDCOMMENT.date_creation).all() return render_template('comments/comments.html', comments=comments, option='comments')
def _make_field_fieldvalue(type_): return db.relationship( lambda: CollectionFieldFieldvalue, primaryjoin=lambda: db.and_( Collection.id == CollectionFieldFieldvalue.id_collection, CollectionFieldFieldvalue.type == type_), order_by=lambda: CollectionFieldFieldvalue.score)
def _collection_type(type_): return db.relationship( Collection, primaryjoin=lambda: db.and_( CollectionExternalcollection.id_collection == Collection.id, CollectionExternalcollection.type == type_), backref='_externalcollections_{0}'.format(str(type_)))
def expand(self, id_user): """Expand comment beloging to user.""" CmtCOLLAPSED.query.filter( db.and_(CmtCOLLAPSED.id_bibrec == self.id_bibrec, CmtCOLLAPSED.id_cmtRECORDCOMMENT == self.id, CmtCOLLAPSED.id_user == id_user)).delete( synchronize_session=False)
def get_session(self, name, expired=False): """Return an instance of :class:`Session`.""" where = Session.session_key == name if expired: where = db.and_( where, Session.session_expiry >= db.func.current_timestamp()) return self.query.filter(where).one()
def get_kbr_values(self, searchkey="", searchvalue="", searchtype='s'): """ Return dicts of 'key' and 'value' from a knowledge base. :param kb_name the name of the knowledge base :param searchkey search using this key :param searchvalue search using this value :param searchtype s=substring, e=exact, sw=startswith :return a list of dictionaries [{'key'=>x, 'value'=>y},..] """ import warnings warnings.warn("The function is deprecated. Please use the " "`KnwKBRVAL.query_kb_mappings()` instead. " "E.g. [(kval.m_value,) for kval in " "KnwKBRVAL.query_kb_mappings(kb_id).all()]") # prepare filters if searchtype == 's': searchkey = '%'+searchkey+'%' if searchtype == 's' and searchvalue: searchvalue = '%'+searchvalue+'%' if searchtype == 'sw' and searchvalue: # startswith searchvalue = searchvalue+'%' if not searchvalue: searchvalue = '%' # execute query return db.session.execute( db.select([KnwKBRVAL.m_value], db.and_(KnwKBRVAL.id_knwKB.like(self.id), KnwKBRVAL.m_value.like(searchvalue), KnwKBRVAL.m_key.like(searchkey))))
def index(recid): linkbacks = LnkENTRY.query.filter(db.and_( LnkENTRY.id_bibrec == recid, LnkENTRY.status == CFG_WEBLINKBACK_STATUS['APPROVED'] )).all() return render_template('linkbacks/index.html', linkbacks=linkbacks)
def manage_portalboxes_order(): """Manage order of portalboxes.""" id_p = request.args.get('id', 0, type=int) id_collection = request.args.get('id_collection', 0, type=int) order = request.args.get('score', 0, type=int) collection = Collection.query.filter( Collection.id == id_collection).first_or_404() portalbox = \ CollectionPortalbox.query.filter(db.and_( CollectionPortalbox.id_portalbox == id_p, CollectionPortalbox.id_collection == id_collection)).first_or_404() position = portalbox.position p_order = portalbox.score db.session.delete(portalbox) collection.portal_boxes_ln.set( CollectionPortalbox(id_collection, id_p, g.ln, position, p_order), order ) db.session.commit() return ''
def get_kbr_values(self, searchkey="", searchvalue="", searchtype='s'): """ Return dicts of 'key' and 'value' from a knowledge base. :param kb_name the name of the knowledge base :param searchkey search using this key :param searchvalue search using this value :param searchtype s=substring, e=exact, sw=startswith :return a list of dictionaries [{'key'=>x, 'value'=>y},..] """ import warnings warnings.warn("The function is deprecated. Please use the " "`KnwKBRVAL.query_kb_mappings()` instead. " "E.g. [(kval.m_value,) for kval in " "KnwKBRVAL.query_kb_mappings(kb_id).all()]") # prepare filters if searchtype == 's': searchkey = '%' + searchkey + '%' if searchtype == 's' and searchvalue: searchvalue = '%' + searchvalue + '%' if searchtype == 'sw' and searchvalue: # startswith searchvalue = searchvalue + '%' if not searchvalue: searchvalue = '%' # execute query return db.session.execute( db.select([KnwKBRVAL.m_value], db.and_(KnwKBRVAL.id_knwKB.like(self.id), KnwKBRVAL.m_value.like(searchvalue), KnwKBRVAL.m_key.like(searchkey))))
def translation(self, lang): try: return db.object_session(self).query(Collectionname).\ with_parent(self).filter(db.and_(Collectionname.ln == lang, Collectionname.type == 'ln')).first().value except: return ""
def note_is_collapsed(id_bibrec, path): """Checks if a note category is collapsed.""" from .models import CmtNOTECOLLAPSED return CmtNOTECOLLAPSED.query.filter(db.and_( CmtNOTECOLLAPSED.id_bibrec == id_bibrec, CmtNOTECOLLAPSED.path == path, CmtNOTECOLLAPSED.id_user == current_user.get_id())).count() > 0
def note_is_collapsed(id_bibrec, path): """Checks if a note category is collapsed.""" from .models import CmtNOTECOLLAPSED return CmtNOTECOLLAPSED.query.filter( db.and_(CmtNOTECOLLAPSED.id_bibrec == id_bibrec, CmtNOTECOLLAPSED.path == path, CmtNOTECOLLAPSED.id_user == current_user.get_id())).count() > 0
def note_expand(id_bibrec, path): """Expands note category for user.""" from .models import CmtNOTECOLLAPSED CmtNOTECOLLAPSED.query.filter(db.and_( CmtNOTECOLLAPSED.id_bibrec == id_bibrec, CmtNOTECOLLAPSED.path == path, CmtNOTECOLLAPSED.id_user == current_user.get_id())).\ delete(synchronize_session=False)
def _collection_type(type_): return db.relationship( Collection, primaryjoin=lambda: db.and_( CollectionExternalcollection.id_collection == Collection.id, CollectionExternalcollection.type == type_), backref='_externalcollections_{0}'.format(str(type_)) )
def reset_rec_cache(output_format, get_record, split_by=1000): """It either stores or does not store the output_format. If CFG_BIBUPLOAD_SERIALIZE_RECORD_STRUCTURE is changed, this function will adapt the database to either store or not store the output_format.""" import sys try: from six.moves import cPickle as pickle except: import pickle from itertools import islice from intbitset import intbitset from invenio.legacy.bibsched.cli import server_pid, pidfile from invenio.ext.sqlalchemy import db from invenio.modules.records.models import Record as Bibrec from invenio.modules.formatter.models import Bibfmt pid = server_pid(ping_the_process=False) if pid: print("ERROR: bibsched seems to run with pid {0}, according to {1}.". format(pid, pidfile), file=sys.stderr) print(" Please stop bibsched before running this procedure.", file=sys.stderr) sys.exit(1) if current_app.config.get('CFG_BIBUPLOAD_SERIALIZE_RECORD_STRUCTURE'): print( ">>> Searching records which need %s cache resetting; this may take a while..." % (output_format, )) all_recids = intbitset(db.session.query(Bibrec.id).all()) #TODO: prevent doing all records? recids = all_recids print(">>> Generating %s cache..." % (output_format, )) tot = len(recids) count = 0 it = iter(recids) while True: rec_group = tuple(islice(it, split_by)) if not len(rec_group): break Bibfmt.query.filter( db.and_(Bibfmt.id_bibrec.in_(rec_group), Bibfmt.format == output_format)).delete( synchronize_session=False) db.session.commit() #TODO: Update the cache or wait for the first access map(get_record, rec_group) count += len(rec_group) print(" ... done records %s/%s" % (count, tot)) if len(rec_group) < split_by or count >= tot: break print(">>> %s cache generated successfully." % (output_format, )) else: print(">>> Cleaning %s cache..." % (output_format, )) Bibfmt.query.filter(Bibfmt.format == output_format).delete( synchronize_session=False) db.session.commit()
def modified_records(self, user_ids): # Get all records that are already associated to this rule try: associated_records = zip( *db.session .query(CheckerRecord.id_bibrec) .filter( CheckerRecord.name_checker_rule==self['name'] ).all() )[0] except IndexError: associated_records = [] # Store requested records that were until now unknown to this rule requested_ids = self.query.requested_ids(user_ids) for requested_id in requested_ids: if requested_id not in associated_records: new_record = CheckerRecord(id_bibrec=requested_id, name_checker_rule=self['name']) db.session.add(new_record) db.session.commit() # Figure out which records have been edited since the last time we ran # this rule try: return zip( *db.session .query(CheckerRecord.id_bibrec) .outerjoin(Bibrec) .filter( db.and_( CheckerRecord.id_bibrec.in_(requested_ids), CheckerRecord.name_checker_rule == self['name'], db.or_( CheckerRecord.last_run < Bibrec.modification_date, db.and_( CheckerRecord.last_run > Bibrec.modification_date, CheckerRecord.expecting_modification == True ) ) ) ) )[0] except IndexError: return []
def delete_all_messages(uid): """ Delete all messages of a user (except reminders) @param uid: user id @return: the number of messages deleted """ reminder_status = CFG_WEBMESSAGE_STATUS_CODE['REMINDER'] msg_ids = map(lambda (x, ): x, db.session.query(UserMsgMESSAGE.id_msgMESSAGE).\ filter(db.and_(UserMsgMESSAGE.id_user_to==uid, UserMsgMESSAGE.status!=reminder_status)).all()) nb_messages = UserMsgMESSAGE.query.\ filter(db.and_(UserMsgMESSAGE.id_user_to==uid, UserMsgMESSAGE.status!=reminder_status)).\ delete(synchronize_session=False) if len(msg_ids) > 0: check_if_need_to_delete_message_permanently(msg_ids) return nb_messages
def title(self): try: return db.object_session(self).query(LnkENTRYURLTITLE).\ filter(db.and_( LnkENTRYURLTITLE.url==self.origin_url, LnkENTRYURLTITLE.title<>"", LnkENTRYURLTITLE.broken==0)).first().title except: return self.origin_url
def __str__(self): uid = current_user.get_id() dbquery.update_user_inbox_for_reminders(uid) unread = db.session.query(db.func.count(UserMsgMESSAGE.id_msgMESSAGE)).\ filter(db.and_( UserMsgMESSAGE.id_user_to == uid, UserMsgMESSAGE.status == cfg['CFG_WEBMESSAGE_STATUS_CODE']['NEW'] )).scalar() return render_template_to_string("messages/menu_item.html", unread=unread)
def translation(self, lang): """Get the translation according to the language code.""" try: return db.object_session(self).query(Collectionname).\ with_parent(self).filter(db.and_( Collectionname.ln == lang, Collectionname.type == 'ln' )).first().value except Exception: return ""
def can_perform_action(self, action=None): cond = ( CmtACTIONHISTORY.id_user == self.uid if self.uid > 0 else CmtACTIONHISTORY.client_host == socket.inet_aton(request.remote_addr) ) if action in cfg["CFG_WEBCOMMENT_ACTION_CODE"]: cond = db.and_(cond, CmtACTIONHISTORY.action_code == cfg["CFG_WEBCOMMENT_ACTION_CODE"][action]) return CmtACTIONHISTORY.query.filter(CmtACTIONHISTORY.id_cmtRECORDCOMMENT == self.id, cond).count() == 0
def get_collectionbox_name(self, ln=None, box_type="r"): """Return collection-specific labelling subtrees. - 'Focus on': regular collection - 'Narrow by': virtual collection - 'Latest addition': boxes If translation for given language does not exist, use label for CFG_SITE_LANG. If no custom label is defined for CFG_SITE_LANG, return default label for the box. :param ln: the language of the label :param box_type: can be 'r' (=Narrow by), 'v' (=Focus on), 'l' (=Latest additions) """ if ln is None: ln = g.ln collectionboxnamequery = db.object_session(self).query( Collectionboxname).with_parent(self) try: collectionboxname = collectionboxnamequery.filter( db.and_( Collectionboxname.ln == ln, Collectionboxname.type == box_type, )).one() except Exception: try: collectionboxname = collectionboxnamequery.filter( db.and_( Collectionboxname.ln == ln, Collectionboxname.type == box_type, )).one() except Exception: collectionboxname = None if collectionboxname is None: # load the right message language _ = gettext_set_language(ln) return _(Collectionboxname.TYPES.get(box_type, '')) else: return collectionboxname.value
def reset_rec_cache(output_format, get_record, split_by=1000): """It either stores or does not store the output_format. If CFG_BIBUPLOAD_SERIALIZE_RECORD_STRUCTURE is changed, this function will adapt the database to either store or not store the output_format.""" import sys try: from six.moves import cPickle as pickle except: import pickle from itertools import islice from intbitset import intbitset from invenio.legacy.bibsched.cli import server_pid, pidfile from invenio.ext.sqlalchemy import db from invenio_records.models import Record as Bibrec from invenio.modules.formatter.models import Bibfmt pid = server_pid(ping_the_process=False) if pid: print("ERROR: bibsched seems to run with pid {0}, according to {1}.".format(pid, pidfile), file=sys.stderr) print(" Please stop bibsched before running this procedure.", file=sys.stderr) sys.exit(1) if current_app.config.get("CFG_BIBUPLOAD_SERIALIZE_RECORD_STRUCTURE"): print(">>> Searching records which need %s cache resetting; this may take a while..." % (output_format,)) all_recids = intbitset(db.session.query(Bibrec.id).all()) # TODO: prevent doing all records? recids = all_recids print(">>> Generating %s cache..." % (output_format,)) tot = len(recids) count = 0 it = iter(recids) while True: rec_group = tuple(islice(it, split_by)) if not len(rec_group): break Bibfmt.query.filter(db.and_(Bibfmt.id_bibrec.in_(rec_group), Bibfmt.format == output_format)).delete( synchronize_session=False ) db.session.commit() # TODO: Update the cache or wait for the first access map(get_record, rec_group) count += len(rec_group) print(" ... done records %s/%s" % (count, tot)) if len(rec_group) < split_by or count >= tot: break print(">>> %s cache generated successfully." % (output_format,)) else: print(">>> Cleaning %s cache..." % (output_format,)) Bibfmt.query.filter(Bibfmt.format == output_format).delete(synchronize_session=False) db.session.commit()
def mark_recids_as_checked(self, recids): now = datetime.now() db.session.query(CheckerRecord).filter( db.and_( CheckerRecord.id_bibrec == recids, CheckerRecord.name_checker_rule == self.name, ) ).update( { "last_run": now, }, synchronize_session=False )
def get_collectionbox_name(self, ln=None, box_type="r"): """Return collection-specific labelling subtrees. - 'Focus on': regular collection - 'Narrow by': virtual collection - 'Latest addition': boxes If translation for given language does not exist, use label for CFG_SITE_LANG. If no custom label is defined for CFG_SITE_LANG, return default label for the box. :param ln: the language of the label :param box_type: can be 'r' (=Narrow by), 'v' (=Focus on), 'l' (=Latest additions) """ if ln is None: ln = g.ln collectionboxnamequery = db.object_session(self).query( Collectionboxname).with_parent(self) try: collectionboxname = collectionboxnamequery.filter(db.and_( Collectionboxname.ln == ln, Collectionboxname.type == box_type, )).one() except Exception: try: collectionboxname = collectionboxnamequery.filter(db.and_( Collectionboxname.ln == ln, Collectionboxname.type == box_type, )).one() except Exception: collectionboxname = None if collectionboxname is None: # load the right message language _ = gettext_set_language(ln) return _(Collectionboxname.TYPES.get(box_type, '')) else: return collectionboxname.value
def menu(): uid = current_user.get_id() dbquery.update_user_inbox_for_reminders(uid) # join: msgMESSAGE -> user_msgMESSAGE, msgMESSAGE -> users # filter: all messages from user AND filter form # order: sorted by one of the table column messages = db.session.query(MsgMESSAGE, UserMsgMESSAGE).\ join(MsgMESSAGE.user_from, MsgMESSAGE.sent_to_users).\ filter(db.and_(dbquery.filter_all_messages_from_user(uid))).\ order_by(db.desc(MsgMESSAGE.received_date)).limit(5) #return dict(messages=messages.all()) return render_template('messages/menu.html', messages=messages.all())
def modifycollectiontree(): """ Handler of the tree changing operations triggered by the drag and drop operation. """ # Get the requests parameters id_son = request.form.get("id_son", 0, type=int) id_dad = request.form.get("id_dad", 0, type=int) id_new_dad = request.form.get("id_new_dad", 0, type=int) score = request.form.get("score", 0, type=int) # if id_dad == id_new_dad: # score = score + 1 type = request.form.get("type", "r") # Check if collection exits. Collection.query.get_or_404(id_son) if id_dad > 0: # Get only one record cc = CollectionCollection.query.filter( db.and_(CollectionCollection.id_son == id_son, CollectionCollection.id_dad == id_dad) ).one() dad = Collection.query.get_or_404(id_dad) dad._collection_children.remove(cc) dad._collection_children.reorder() else: cc = CollectionCollection(id_dad=id_new_dad, id_son=id_son, type=type) db.session.add(cc) if id_new_dad == 0: db.session.delete(cc) else: new_dad = Collection.query.get_or_404(id_new_dad) cc.id_dad = id_new_dad try: descendants = Collection.query.get(id_son).descendants_ids ancestors = new_dad.ancestors_ids print(descendants, ancestors) if descendants & ancestors: raise except: ## Cycle has been detected. db.session.rollback() abort(406) new_dad._collection_children.reorder() new_dad._collection_children.insert(score, cc) # FIXME add dbrecs rebuild for modified trees. db.session.commit() return "done"
def modifycollectiontree(): """ Handler of the tree changing operations triggered by the drag and drop operation. """ # Get the requests parameters id_son = request.form.get('id_son', 0, type=int) id_dad = request.form.get('id_dad', 0, type=int) id_new_dad = request.form.get('id_new_dad', 0, type=int) score = request.form.get('score', 0, type=int) #if id_dad == id_new_dad: # score = score + 1 type = request.form.get('type', 'r') # Check if collection exits. Collection.query.get_or_404(id_son) if id_dad > 0: # Get only one record cc = CollectionCollection.query.filter( db.and_(CollectionCollection.id_son == id_son, CollectionCollection.id_dad == id_dad)).one() dad = Collection.query.get_or_404(id_dad) dad._collection_children.remove(cc) dad._collection_children.reorder() else: cc = CollectionCollection(id_dad=id_new_dad, id_son=id_son, type=type) db.session.add(cc) if id_new_dad == 0: db.session.delete(cc) else: new_dad = Collection.query.get_or_404(id_new_dad) cc.id_dad = id_new_dad try: descendants = Collection.query.get(id_son).descendants_ids ancestors = new_dad.ancestors_ids print(descendants, ancestors) if descendants & ancestors: raise except: ## Cycle has been detected. db.session.rollback() abort(406) new_dad._collection_children.reorder() new_dad._collection_children.insert(score, cc) #FIXME add dbrecs rebuild for modified trees. db.session.commit() return 'done'
def can_perform_action(self, action=None): cond = CmtACTIONHISTORY.id_user == self.uid \ if self.uid > 0 else \ CmtACTIONHISTORY.client_host == \ socket.inet_aton(request.remote_addr) if action in cfg['CFG_WEBCOMMENT_ACTION_CODE']: cond = db.and_( cond, CmtACTIONHISTORY.action_code == cfg['CFG_WEBCOMMENT_ACTION_CODE'][action]) return CmtACTIONHISTORY.query.filter( CmtACTIONHISTORY.id_cmtRECORDCOMMENT == self.id, cond).\ count() == 0
def decorated_function(*args, **kwargs): if not model or not columns: return f(*args, **kwargs) where = [] for column, op in iteritems(columns): try: values = request.values.getlist(column) if not values: continue column_keys = column.split('.') if hasattr(model, column_keys[0]): cond = reduce( lambda x, y: getattr(x.property.table.columns, y), column_keys[1:], getattr(model, column_keys[0])) current_app.logger.debug("Filtering by: %s = %s" % (cond, values)) # Multi-values support if len(values) > 0: # Ignore empty values when using start with, # contains or similar. # FIXME: add per field configuration values = [ value for value in values if len(value) > 0 or filter_empty ] if op == operators.eq: where.append(db.in_(values)) else: or_list = [] for value in values: or_list.append(op(cond, value)) where.append(db.or_(*or_list)) else: where.append(op(cond, value)) except: flash( g._("Invalid filtering key '%(x_key)s'.", x_key=column)) if form is not None: filter_form = form(request.values) @register_template_context_processor def inject_filter_form(): return dict(filter_form=filter_form) # Generate ClauseElement for filtered columns. kwargs['filter'] = db.and_(*where) return f(*args, **kwargs)
def index(sort=False, filter=None): uid = current_user.get_id() dbquery.update_user_inbox_for_reminders(uid) # join: msgMESSAGE -> user_msgMESSAGE, msgMESSAGE -> users # filter: all messages from user AND filter form # order: sorted by one of the table column messages = db.session.query(MsgMESSAGE, UserMsgMESSAGE).\ join(MsgMESSAGE.user_from, MsgMESSAGE.sent_to_users).\ filter(db.and_(dbquery.filter_all_messages_from_user(uid), (filter))).\ order_by(sort) return dict(messages=messages.all(), nb_messages=dbquery.count_nb_messages(uid), no_quota=is_no_quota_user(uid))
def decorated_function(*args, **kwargs): if not model or not columns: return f(*args, **kwargs) where = [] for column, op in iteritems(columns): try: values = request.values.getlist(column) if not values: continue column_keys = column.split('.') if hasattr(model, column_keys[0]): cond = reduce(lambda x, y: getattr(x.property.table.columns, y), column_keys[1:], getattr(model, column_keys[0])) current_app.logger.debug("Filtering by: %s = %s" % (cond, values)) # Multi-values support if len(values) > 0: # Ignore empty values when using start with, # contains or similar. # FIXME: add per field configuration values = [value for value in values if len(value) > 0 or filter_empty] if op == operators.eq: where.append(db.in_(values)) else: or_list = [] for value in values: or_list.append(op(cond, value)) where.append(db.or_(*or_list)) else: where.append(op(cond, value)) except: flash(g._("Invalid filtering key '%(x_key)s'.", x_key=column)) if form is not None: filter_form = form(request.values) @register_template_context_processor def inject_filter_form(): return dict(filter_form=filter_form) # Generate ClauseElement for filtered columns. kwargs['filter'] = db.and_(*where) return f(*args, **kwargs)
class AccAuthorization(db.Model): """Represent an authorization.""" __tablename__ = 'accROLE_accACTION_accARGUMENT' id = db.Column(db.Integer(15, unsigned=True), primary_key=True, autoincrement=True) id_accROLE = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(AccROLE.id), nullable=True, index=True) id_accACTION = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(AccACTION.id), nullable=True, index=True) _id_accARGUMENT = db.Column(db.Integer(15), nullable=True, name="id_accARGUMENT", index=True) argumentlistid = db.Column(db.MediumInteger(8), nullable=True) role = db.relationship(AccROLE, backref='authorizations') action = db.relationship(AccACTION, backref='authorizations') argument = db.relationship( AccARGUMENT, backref='authorizations', primaryjoin=db.and_(AccARGUMENT.id == _id_accARGUMENT, _id_accARGUMENT != -1, _id_accARGUMENT is not None), foreign_keys=_id_accARGUMENT, uselist=False, cascade="all, delete", ) @db.hybrid_property def id_accARGUMENT(self): """get id_accARGUMENT.""" return self._id_accARGUMENT @id_accARGUMENT.setter def id_accARGUMENT(self, value): """set id_accARGUMENT.""" self._id_accARGUMENT = value or None
def unsubscribe(recid=None): uid = current_user.get_id() if recid is None: recid = request.values.getlist("recid", type=int) else: recid = [recid] current_app.logger.info(recid) try: db.session.query(CmtSUBSCRIPTION).filter( db.and_(CmtSUBSCRIPTION.id_bibrec.in_(recid), CmtSUBSCRIPTION.id_user == uid) ).delete(synchronize_session=False) db.session.commit() flash(_("You have been successfully unsubscribed"), "success") except: flash(_("You are already unsubscribed"), "error") if len(recid) == 1: return redirect(url_for(".comments", recid=recid[0])) else: return redirect(url_for(".subscriptions"))
def widget(self): uid = current_user.get_id() unread = db.session.query(db.func.count(UserMsgMESSAGE.id_msgMESSAGE)).\ filter(db.and_( UserMsgMESSAGE.id_user_to == uid, UserMsgMESSAGE.status == current_app.config[ 'CFG_WEBMESSAGE_STATUS_CODE']['NEW'] )).scalar() total = db.session.query(db.func.count(UserMsgMESSAGE.id_msgMESSAGE)).\ filter( UserMsgMESSAGE.id_user_to == uid ).scalar() template = """ {{ _("You have %(x_num_new)d new messages out of %(x_num_total)d messages.", x_num_new=unread, x_num_total=total) }} """ return render_template_to_string(template, _from_string=True, unread=unread, total=total)
def sent_to_group_names(self, value): old_group_names = self.group_names self._sent_to_group_names = value groups_to_add = set(self.group_names)-set(old_group_names) groups_to_del = set(old_group_names)-set(self.group_names) if len(groups_to_del): to_del = set([u.nickname for u in User.query.\ join(User.usergroups).filter( Usergroup.name.in_(groups_to_del)).\ all()])-set(self.user_nicks) is_to_del = lambda u: u.nickname in to_del remove_old = filter(is_to_del, self.recipients) for u in remove_old: self.recipients.remove(u) if len(groups_to_add): for u in User.query.join(User.usergroups).filter(db.and_( Usergroup.name.in_(groups_to_add), db.not_(User.nickname.in_(self.user_nicks)))).all(): if u not in self.recipients: self.recipients.append(u)
def unsubscribe(recid=None): uid = current_user.get_id() if recid is None: recid = request.values.getlist('recid', type=int) else: recid = [recid] current_app.logger.info(recid) try: db.session.query(CmtSUBSCRIPTION).filter( db.and_(CmtSUBSCRIPTION.id_bibrec.in_(recid), CmtSUBSCRIPTION.id_user == uid)).delete( synchronize_session=False) db.session.commit() flash(_('You have been successfully unsubscribed'), 'success') except: flash(_('You are already unsubscribed'), 'error') if len(recid) == 1: return redirect(url_for('.comments', recid=recid[0])) else: return redirect(url_for('.subscriptions'))
def sent_to_group_names(self, value): old_group_names = self.group_names self._sent_to_group_names = value groups_to_add = set(self.group_names) - set(old_group_names) groups_to_del = set(old_group_names) - set(self.group_names) if len(groups_to_del): to_del = set([u.nickname for u in User.query.\ join(User.usergroups).filter( Usergroup.name.in_(groups_to_del)).\ all()])-set(self.user_nicks) is_to_del = lambda u: u.nickname in to_del remove_old = filter(is_to_del, self.recipients) for u in remove_old: self.recipients.remove(u) if len(groups_to_add): for u in User.query.join(User.usergroups).filter( db.and_(Usergroup.name.in_(groups_to_add), db.not_(User.nickname.in_( self.user_nicks)))).all(): if u not in self.recipients: self.recipients.append(u)
def manage_portalboxes_order(): id_p = request.args.get("id", 0, type=int) collection_id = request.args.get("id_collection", 0, type=int) order = request.args.get("score", 0, type=int) collection = Collection.query.filter(Collection.id == collection_id).first_or_404() portalbox = CollectionPortalbox.query.filter( db.and_(CollectionPortalbox.id_portalbox == id_p, CollectionPortalbox.id_collection == collection_id) ).first_or_404() position = portalbox.position p_order = portalbox.score db.session.delete(portalbox) # p = portalboxes.pop(portalbox) collection.portal_boxes_ln.set(CollectionPortalbox(collection_id, id_p, g.ln, position, p_order), order) db.session.commit() return ""
def _set_done(obj, eng, rule_names, recids): """Update the database that a rule run has completed.""" # FIXME: Don't re-get extra data. It was already pulled out before this # funciton call extra_data = obj.get_extra_data() now = datetime.now() for recid in recids: record = _load_record(extra_data, recid) expecting_modification = _record_has_changed(obj, eng, record, extra_data) db.session.query(CheckerRecord).filter( db.and_( CheckerRecord.id_bibrec == recids, CheckerRecord.name_checker_rule.in_((rule_names)) ) ).update( { "last_run": now, "expecting_modification": expecting_modification }, synchronize_session=False) db.session.commit()
def update_translations(id): """Update translations if the value is altered or not void.""" collection = Collection.query.filter(Collection.id == id).first_or_404() for (lang, lang_long) in language_list_long(): collection_name = Collectionname.query.filter( db.and_(Collectionname.id_collection == id, Collectionname.ln == lang, Collectionname.type == 'ln')).first() if collection_name: if collection_name.value != request.form.get(lang): collection_name.value = request.form.get(lang) db.session.commit() else: if request.form.get(lang) != '': collection_name = Collectionname(collection, lang, 'ln', request.form.get(lang)) db.session.add(collection_name) db.session.commit() flash(_('Collection was updated on n languages:'), "info") return redirect(url_for('.manage_collection', name=collection.name))
id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), nullable=False, primary_key=True) id_accROLE = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(AccROLE.id), nullable=False, primary_key=True) expiration = db.Column(db.DateTime, nullable=False, server_default='9999-12-31 23:59:59') user = db.relationship(User, backref='roles') role = db.relationship(AccROLE, backref='users') User.active_roles = db.relationship( UserAccROLE, lazy="dynamic", primaryjoin=db.and_( User.id == UserAccROLE.id_user, UserAccROLE.expiration >= db.func.now() ) ) User.has_admin_role = property(lambda self: self.has_super_admin_role or db.object_session(self).query( db.func.count(User.id)>0 ).join( User.active_roles, UserAccROLE.role, AccROLE.authorizations ).filter( AccAuthorization.id_accACTION.in_( db.select([AccACTION.id]).where( AccACTION.name.in_(CFG_ACC_ACTIVITIES_URLS.keys()) )
user = db.relationship(User, backref=db.backref('usergroups')) usergroup = db.relationship(Usergroup, backref=db.backref( 'users', cascade="all, delete-orphan")) def is_admin(self): """Return True if user is a admin.""" return self.user_status == self.USER_STATUS['ADMIN'] # define query to get admins Usergroup.admins = db.relationship( UserUsergroup, lazy="dynamic", primaryjoin=db.and_( Usergroup.id == UserUsergroup.id_usergroup, UserUsergroup.user_status == UserUsergroup.USER_STATUS['ADMIN'])) class UserEXT(db.Model): """Represent a UserEXT record.""" __tablename__ = 'userEXT' id = db.Column(db.VARBINARY(255), primary_key=True, nullable=False) method = db.Column(db.String(50), primary_key=True, nullable=False) id_user = db.Column(db.Integer(15, unsigned=True), db.ForeignKey(User.id), nullable=False) user = db.relationship(User, backref="external_identifiers")
def is_collapsed(self, id_user): """Return true if the comment is collapsed by user.""" return CmtCOLLAPSED.query.filter( db.and_(CmtCOLLAPSED.id_bibrec == self.id_bibrec, CmtCOLLAPSED.id_cmtRECORDCOMMENT == self.id, CmtCOLLAPSED.id_user == id_user)).count() > 0