def _page_action(self, id, tags, page_id): try: page_id = int(page_id) except ValueError: abort(404) location = LocationTag.get(tags) if location is None: abort(404) subject = Subject.get(location, id) if subject is None: abort(404) page = Page.get(page_id) if page is None: abort(404) c.page = page c.subject = subject c.similar_subjects = find_similar_subjects(location.id, id) c.object_location = subject.location c.security_context = subject c.tabs = subject_menu_items() c.theme = location.get_theme() return method(self, subject, page)
def create(self, id, tags): if not hasattr(self, 'form_result'): redirect(url.current(action='add')) location = LocationTag.get(tags) subject = Subject.get(location, id) page = Page(self.form_result['page_title'], self.form_result['page_content']) subject.pages.append(page) meta.Session.add(page) meta.Session.commit() redirect(url.current(action='index', page_id=page.id))
def _subject_action(self, id, tags): location = LocationTag.get(tags) subject = Subject.get(location, id) if subject is None: abort(404) c.security_context = subject c.object_location = subject.location c.subject = subject c.similar_subjects = find_similar_subjects(location.id, id) c.tabs = subject_menu_items() c.breadcrumbs = [{'link': subject.url(), 'title': subject.title}] c.theme = subject.location.get_theme() return method(self, subject)
def find_similar_subjects(location_id, id, n=5): """Find 5 similar subjects to the one given.""" location = LocationTag.get(location_id) subject = Subject.get(location, id) def filter_out(query): return query.filter(SearchItem.content_item_id != subject.id) results = search(text=subject.title, obj_type='subject', disjunctive=False, limit=n, extra=filter_out, language=location.language) if not results: results = search(text=subject.title, obj_type='subject', tags=subject.location.hierarchy(), disjunctive=True, limit=5, extra=filter_out, rank_cutoff=0.1, language=location.language) return [item.object.info_dict() for item in results]
def update(self, subject): if not hasattr(self, 'form_result'): redirect(url(controller='subject', action='add')) #check if we need to regenerate the id clash = Subject.get(self.form_result.get('location', None), subject.subject_id) if clash is not None and clash is not subject: subject.subject_id = subject.generate_new_id() subject.title = self.form_result['title'] subject.lecturer = self.form_result['lecturer'] subject.location = self.form_result['location'] subject.description = self.form_result.get('description', None) subject.sub_department_id = self.form_result.get('sub_department_id', None) #check to see what kind of tags we have got tags = [tag.strip().lower() for tag in self.form_result.get('tagsitem', []) if len(tag.strip().lower()) < 250 and tag.strip() != ''] if tags == []: tags = [tag.strip().lower() for tag in self.form_result.get('tags', '').split(',') if len(tag.strip()) < 250 and tag.strip() != ''] subject.tags = [] for tag in tags: subject.tags.append(SimpleTag.get(tag)) # update subject permissions if check_crowds(["teacher", "moderator", "root"], c.user, subject) and 'subject_visibility' in self.form_result: subject.visibility = self.form_result['subject_visibility'] subject.edit_settings_perm = self.form_result['subject_edit'] subject.post_discussion_perm = self.form_result['subject_post_discussions'] # remove subject from watched list for users who can't view subject anymore if subject.visibility != 'everyone': crowd_fn = is_university_member if subject.visibility == 'university_members' else is_department_member for watcher in subject.watching_users: if not crowd_fn(watcher.user, subject): watcher.user.unwatchSubject(subject) meta.Session.commit() redirect(url(controller='subject', action='home', id=subject.subject_id, tags=subject.location_path))
def _subject_action(self, id, tags, file_id): location = LocationTag.get(tags) subject = Subject.get(location, id) if subject is None: abort(404) file = File.get(file_id) if file is None: abort(404) if (file not in subject.files and not file.can_write()): abort(404) c.object_location = subject.location c.security_context = file c.subject = subject c.theme = subject.location.get_theme() return method(self, subject, file)
def _to_python(self, value, state): obj = None # check if this is a plain numeric id try: num_id = int(value) obj = ContentItem.get(num_id) except ValueError: pass # check if the id is a path to a subject if obj is None and value.startswith('subject'): path = value.split('/')[1:] location = LocationTag.get(path[:-1]) obj = Subject.get(location, path.pop()) # check if the object is a group if obj is None and value.startswith('group'): id = value.split('/') obj = Group.get(id.pop()) return obj
def _getSubject(self): location_id = request.GET['subject_location_id'] location = meta.Session.query(LocationTag).filter_by(id=location_id).one() subject_id = request.GET['subject_id'] return Subject.get(location, subject_id)