Esempio n. 1
0
    def _create_subject(self):
        title = self.form_result['title']
        id = ''.join(Random().sample(string.ascii_lowercase, 8))  # use random id first
        lecturer = self.form_result['lecturer']
        location = self.form_result['location']
        sub_department_id = self.form_result.get('sub_department_id', None)
        description = self.form_result['description']

        if lecturer == '':
            lecturer = None

        #check to see what kind of tags we have got
        tags = [tag.strip().lower() for tag in self.form_result.get('tagsitem', [])]
        if tags == []:
            tags = [tag.strip().lower() for tag in self.form_result.get('tags', '').split(',')]

        stags = []
        for tag in tags:
            stags.append(SimpleTag.get(tag))

        subj = Subject(id, title, location, lecturer, description, stags)
        subj.sub_department_id = sub_department_id

        meta.Session.add(subj)
        meta.Session.flush()

        newid = subj.generate_new_id()
        if newid is not None:
            subj.subject_id = newid

        return subj
Esempio n. 2
0
 def create_subject_wall_post(self):
     subject = Subject.get_by_id(self.form_result['subject_id'])
     if subject.post_discussion_perm != 'everyone' and not check_crowds(['teacher', 'moderator'], c.user, subject):
         abort(403)
     self._create_wall_post(subject=subject,
                            content=self.form_result['post'])
     self._redirect()
Esempio n. 3
0
 def create_wiki_js(self):
     target = Subject.get_by_id(self.form_result['rcpt_wiki'])
     page = self._create_wiki_page(target,
                                   self.form_result['page_title'],
                                   self.form_result['page_content'])
     evt = meta.Session.query(PageCreatedEvent).filter_by(page_id=page.id).one().wall_entry()
     return {'success': True, 'evt': evt}
Esempio n. 4
0
    def validate_python(self, value, state):
        if value == '':
            raise Invalid(self.message('invalid', state), value, state)

        s = Subject.get_by_id(int(value))
        if s is None:
            raise Invalid(self.message('invalid', state), value, state)
Esempio n. 5
0
    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)
Esempio n. 6
0
 def create_subject_wall_post_js(self):
     subject = Subject.get_by_id(self.form_result['subject_id'])
     if subject.post_discussion_perm != 'everyone' and not check_crowds(['teacher', 'moderator'], c.user, subject):
         abort(403)
     post = self._create_wall_post(subject=subject,
                                   content=self.form_result['post'])
     evt = meta.Session.query(SubjectWallPostEvent).filter_by(object_id=post.id).one().wall_entry()
     return {'success': True, 'evt': evt}
Esempio n. 7
0
 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))
Esempio n. 8
0
    def create_wiki(self):
        if not hasattr(self, 'form_result'):
            self._redirect()

        target = Subject.get_by_id(self.form_result['rcpt_wiki'])
        self._create_wiki_page(
            target,
            self.form_result['page_title'],
            self.form_result['page_content'])
        h.flash(_('Wiki page created.'))
        self._redirect()
Esempio n. 9
0
    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)
Esempio n. 10
0
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]
Esempio n. 11
0
    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))
Esempio n. 12
0
    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)
Esempio n. 13
0
    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
Esempio n. 14
0
def subject_page_count(subject_id):
    from ututi.model import Subject
    return len(Subject.get_by_id(subject_id).pages)
Esempio n. 15
0
 def _getSubject(self):
     subject_id = request.GET["subject_id"]
     return Subject.get_by_id(int(subject_id))
Esempio n. 16
0
 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)
Esempio n. 17
0
def test_setup(test):
    """Create some models needed for the tests."""
    ututi.tests.setUp(test)
    initialize_dictionaries(meta.engine)
    config = pylons.test.pylonsapp.config
    config['default_search_dict'] = 'public.universal'

    vu = LocationTag(u'Vilniaus universitetas', u'vu', u'', member_policy='PUBLIC')
    ef = LocationTag(u'Ekonomikos fakultetas', u'ef', u'', vu, member_policy='PUBLIC')

    meta.Session.add(vu)
    meta.Session.add(ef)

    # We need someone who can create subjects and groups
    user = User(u'User', '*****@*****.**', vu, 'password')
    meta.Session.add(user)
    meta.Session.commit()

    meta.Session.execute("SET default_text_search_config TO 'public.universal'")
    meta.set_active_user(user.id)

    l = LocationTag(u'Kauno technologijos universitetas', u'ktu', u'', member_policy='PUBLIC')
    f = LocationTag(u'Ekologijos fakultetas', u'ef', u'', l, member_policy='PUBLIC')

    mtag = SimpleTag(u'Ekologijos fakultetas') #a mixed tag

    meta.Session.add(l)
    meta.Session.add(f)

    g = Group('agroup', u'Ekologai', description=u'testas')
    g.location = f
    meta.Session.add(g)

    g = Group('new_group', u'Bioinformatikai', description=u'Grup\u0117 kurioje domimasi biologija ir informatika')
    meta.Session.add(g)

    # a tagged group
    g2 = Group('new_grp', u'Biology students', description=u'biologija matematika informatikos mokslas')
    g2.location = LocationTag.get(u'vu/ef')

    meta.Session.add(g2)
    tg = SimpleTag(u'test tag')
    g2.tags.append(tg)

    s = Subject(u'subj_id', u'Test subject', LocationTag.get(u'VU'))
    s.description = u'pagrindai'
    t = SimpleTag(u'a tag')
    meta.Session.add(t)
    s.tags.append(t)
    s.tags.append(mtag)
    meta.Session.add(s)
    p = Page(u'page title', u'Puslapio tekstas')
    meta.Session.add(p)
    s.pages.append(p)

    s = Subject('biologija', u'Biologijos pagrindai', LocationTag.get(u'vu'))
    p = Page(u'page title', u'Puslapio tekstas')
    s.pages.append(p)
    meta.Session.add(s)
    meta.Session.add(p)

    f = File(u'test.txt', u'geografija', 'text/txt')
    f.parent = s
    meta.Session.add(f)

    meta.Session.commit()
    meta.Session.execute("SET default_text_search_config TO 'public.universal'")