def get(self, user_id): user = User.query.filter_by(id=user_id).first_or_404() form = self.form(user) member_group = db.and_(*[ db.not_(getattr(Group, p)) for p in ['admin', 'mod', 'super_mod', 'banned', 'guest'] ]) filt = db.or_(Group.id.in_(g.id for g in current_user.groups), member_group) if Permission(IsAtleastSuperModerator, identity=current_user): filt = db.or_(filt, Group.mod) if Permission(IsAdmin, identity=current_user): filt = db.or_(filt, Group.admin, Group.super_mod) if Permission(CanBanUser, identity=current_user): filt = db.or_(filt, Group.banned) group_query = Group.query.filter(filt) form.primary_group.query = group_query form.secondary_groups.query = group_query return render_template('management/user_form.html', form=form, title=_('Edit User'))
def _update_counts(self): if self.hidden: clauses = [Post.hidden != True, Post.id != self.id] else: clauses = [db.or_(Post.hidden != True, Post.id == self.id)] user_post_clauses = clauses + [ Post.user_id == self.user.id, Topic.id == Post.topic_id, Topic.hidden != True, ] # Update the post counts self.user.post_count = Post.query.filter(*user_post_clauses).count() if self.topic.hidden: self.topic.post_count = 0 else: topic_post_clauses = clauses + [ Post.topic_id == self.topic.id, ] self.topic.post_count = Post.query.filter( *topic_post_clauses).count() forum_post_clauses = clauses + [ Post.topic_id == Topic.id, Topic.forum_id == self.topic.forum.id, Topic.hidden != True, ] self.topic.forum.post_count = Post.query.filter( *forum_post_clauses).count()
def post(self, user_id): user = User.query.filter_by(id=user_id).first_or_404() member_group = db.and_(*[ db.not_(getattr(Group, p)) for p in ['admin', 'mod', 'super_mod', 'banned', 'guest'] ]) filt = db.or_(Group.id.in_(g.id for g in current_user.groups), member_group) if Permission(IsAtleastSuperModerator, identity=current_user): filt = db.or_(filt, Group.mod) if Permission(IsAdmin, identity=current_user): filt = db.or_(filt, Group.admin, Group.super_mod) if Permission(CanBanUser, identity=current_user): filt = db.or_(filt, Group.banned) group_query = Group.query.filter(filt) form = EditUserForm(user) form.primary_group.query = group_query form.secondary_groups.query = group_query if form.validate_on_submit(): form.populate_obj(user) user.primary_group_id = form.primary_group.data.id # Don't override the password if form.password.data: user.password = form.password.data user.save(groups=form.secondary_groups.data) flash(_('User updated.'), 'success') return redirect(url_for('management.edit_user', user_id=user.id)) return render_template('management/user_form.html', form=form, title=_('Edit User'))
def _fix_post_counts(self, forum): clauses = [Topic.forum_id == forum.id] if self.hidden: clauses.extend([ Topic.id != self.id, Topic.hidden != True, ]) else: clauses.append(db.or_(Topic.id == self.id, Topic.hidden != True)) forum.topic_count = Topic.query.filter(*clauses).count() post_count = clauses + [ Post.topic_id == Topic.id, ] if self.hidden: post_count.append(Post.hidden != True) else: post_count.append( db.or_(Post.hidden != True, Post.id == self.first_post.id)) forum.post_count = Post.query.distinct().filter(*post_count).count()
def update_read(self, user, forumsread, topicsread): """Updates the ForumsRead status for the user. In order to work correctly, be sure that `topicsread is **not** `None`. :param user: The user for whom we should check if he has read the forum. :param forumsread: The forumsread object. It is needed to check if if the forum is unread. If `forumsread` is `None` and the forum is unread, it will create a new entry in the `ForumsRead` relation, else (and the forum is still unread) we are just going to update the entry in the `ForumsRead` relation. :param topicsread: The topicsread object is used in combination with the forumsread object to check if the forumsread relation should be updated and therefore is unread. """ if not user.is_authenticated or topicsread is None: return False read_cutoff = None if flaskpet_config['TRACKER_LENGTH'] > 0: read_cutoff = time_utcnow() - timedelta( days=flaskpet_config['TRACKER_LENGTH']) # fetch the unread posts in the forum unread_count = Topic.query.\ outerjoin(TopicsRead, db.and_(TopicsRead.topic_id == Topic.id, TopicsRead.user_id == user.id)).\ outerjoin(ForumsRead, db.and_(ForumsRead.forum_id == Topic.forum_id, ForumsRead.user_id == user.id)).\ filter(Topic.forum_id == self.id, Topic.last_updated > read_cutoff, db.or_(TopicsRead.last_read == None, # noqa: E711 TopicsRead.last_read < Topic.last_updated), db.or_(ForumsRead.last_read == None, # noqa: E711 ForumsRead.last_read < Topic.last_updated)).\ count() # No unread topics available - trying to mark the forum as read if unread_count == 0: logger.debug("No unread topics. Trying to mark the forum as read.") if forumsread and forumsread.last_read > topicsread.last_read: logger.debug("forumsread.last_read is newer than " "topicsread.last_read. Everything is read.") return False # ForumRead Entry exists - Updating it because a new topic/post # has been submitted and has read everything (obviously, else the # unread_count would be useless). elif forumsread: logger.debug( "Updating existing ForumsRead '{}' object.".format( forumsread)) forumsread.last_read = time_utcnow() forumsread.save() return True # No ForumRead Entry existing - creating one. logger.debug("Creating new ForumsRead object.") forumsread = ForumsRead() forumsread.user = user forumsread.forum = self forumsread.last_read = time_utcnow() forumsread.save() return True # Nothing updated, because there are still more than 0 unread # topicsread logger.debug("No ForumsRead object updated - there are still {} " "unread topics.".format(unread_count)) return False