def related_users(user_id, location_id, limit=6): from ututi.model import User users = {} counts = {} user = User.get(user_id, location_id) # group mates for group in user.groups: for member in group.members: id = member.user.id counts.setdefault(id, 0) counts[id] += 1 users[id] = member.user # subject followers for subject in user.watched_subjects: for monitor in subject.watching_users: id = monitor.user.id counts.setdefault(id, 0) counts[id] += 1 users[id] = monitor.user # remove self if user.id in counts: del counts[user.id] if user.id in users: del users[user.id] # sort by counts pairs = [(counts[id], users[id]) for id in users.keys()] pairs.sort() pairs.reverse() pairs = pairs[:limit] return [{'id': user.id, 'title': user.fullname, 'url': user.url(), 'logo_url': user.url(action='logo', width=45), 'logo_small_url': user.url(action='logo', width=30)} for count, user in pairs]
def test_setup(test): """Create some models needed for the tests.""" ututi.tests.setUp(test) setUpUser() u = User.get('*****@*****.**', LocationTag.get(u'uni')) meta.set_active_user(u.id) meta.Session.add(Subject(u'subject', u'Subject', LocationTag.get(u'uni'), u'')) meta.Session.commit() meta.set_active_user(u.id)
def test_setup(test): """Create some models needed for the tests.""" ututi.tests.setUp(test) setUpUser() uni = LocationTag.get(u'uni') u = User.get('*****@*****.**', uni) meta.set_active_user(u.id) dept1 = LocationTag(title=u'dept1', title_short=u'dept1', parent=uni, member_policy='PUBLIC') dept2 = LocationTag(title=u'dept2', title_short=u'dept2', parent=uni, member_policy='PUBLIC') other_uni = LocationTag(title=u'other_uni', title_short=u'other_uni', parent=None, member_policy='PUBLIC') meta.Session.add_all([dept1, dept2, other_uni]) meta.Session.commit() meta.set_active_user(u.id) subj1 = Subject(u'subject1', u'Subject1', dept1, u'') subj2 = Subject(u'subject2', u'Subject2', dept2, u'') meta.Session.add_all([subj1, subj2]) u = User.get('*****@*****.**', uni) u.location = LocationTag.get(u'uni/dept2') meta.Session.add(u) meta.Session.commit() meta.Session.commit() meta.set_active_user(u.id)
def make_email_invitations(emails, inviter, location): invalid = [] already = [] invited = [] for email in filter(bool, map(strip, emails)): try: email = TranslatedEmailValidator.to_python(email) except Invalid: invalid.append(email) else: if User.get(email, location.root): already.append(email) else: invitee = UserRegistration.create_or_update(location, email) invitee.inviter = inviter invited.append(invitee) return invited, invalid, already
def test_setup(test): """Create some models for this test.""" ututi.tests.setUp(test) # The following c&p from model tests. Maybe should be put to base set up. #a user needs a university uni = LocationTag(u'U-niversity', u'uni', u'', member_policy='PUBLIC') meta.Session.add(uni) meta.Session.commit() #the user meta.Session.execute("insert into authors (type, fullname) values ('user', 'Administrator of the university')") meta.Session.execute("insert into users (id, location_id, username, password)" " (select authors.id, tags.id, '*****@*****.**', 'xnIVufqLhFFcgX+XjkkwGbrY6kBBk0vvwjA7'" " from tags, authors where title_short = 'uni' and fullname = 'Administrator of the university');") meta.Session.execute("insert into emails (id, email, confirmed)" " (select users.id, users.username, true from users where username = '******')") meta.Session.commit() u = User.get('*****@*****.**', uni) user = User(u"a new user", "*****@*****.**", uni, "his password") meta.Session.add(user) user.emails.append(Email("*****@*****.**")) user.phone_number = '+37060000000' user.phone_confirmed = False meta.Session.commit() meta.Session.execute("SET LOCAL ututi.active_user TO %d" % u.id) g = Group('moderators', u'Moderatoriai', LocationTag.get(u'vu'), date.today(), u'U2ti moderatoriai.') role = GroupMembershipType.get('administrator') gm = GroupMember() gm.user = u gm.group = g gm.role = role meta.Session.add(g) meta.Session.add(gm) meta.Session.commit() meta.set_active_user(u.id)
def _send_group_invitations(self, group, emails, message=None): invites, invalid, already = make_email_invitations(emails, c.user, group.location) # invites are registration objects! for email in already: user = User.get(email, group.location.root) if self._check_handshakes(group, user) == 'request': # Automatically accept requests to become group member. group.add_member(user) self._clear_requests(group, c.user) h.flash(_('New member %s added.') % user.fullname) else: invitation = group.create_pending_invitation(email, c.user) send_group_invitation_for_user(invitation, email, message) for registration in invites: invitation = group.create_pending_invitation(registration.email, c.user) send_group_invitation_for_non_user(invitation, registration, message) if invites: h.flash(_("Users invited.")) if invalid != []: h.flash(_("Invalid email addresses detected: %s") % ', '.join(invalid)) meta.Session.commit()
def ftest_setUp(test): ututi.tests.setUp(test) # U-niversity and D-epartment # should be used in writing new functional tests uni = LocationTag(u'U-niversity', u'uni', u'', member_policy='PUBLIC') dep = LocationTag(u'D-epartment', u'dep', u'', uni, member_policy='PUBLIC') meta.Session.add(uni) meta.Session.add(dep) meta.Session.commit() # Admin user, named 'Adminas Adminovix' for backward compatibility create_user('Adminas Adminovix', '*****@*****.**', 'xnIVufqLhFFcgX+XjkkwGbrY6kBBk0vvwjA7', 'uni') # Below are old locations, users, groups and subjects for backward compatibility # Note: objects that didn't have their location specified are now assigned to U-niversity vu = LocationTag(u'Vilniaus universitetas', u'vu', u'Seniausias universitetas Lietuvoje.', member_policy='PUBLIC') ef = LocationTag(u'Ekonomikos fakultetas', u'ef', u'', vu, member_policy='PUBLIC') gf = LocationTag(u'Geografijos fakultetas', u'gf', u'', vu, member_policy='PUBLIC') meta.Session.add(vu) meta.Session.add(ef) meta.Session.add(gf) region = Region(u'Mazowieckie', u'lt') meta.Session.add(region) # Users: first = User(u'Alternative user', '*****@*****.**', uni, 'password', True) meta.Session.add(first) email = Email('*****@*****.**') email.confirmed = True first.emails.append(email) second = User(u'Second user', '*****@*****.**', uni, 'password', True) meta.Session.add(second) email = Email('*****@*****.**') email.confirmed = True second.emails.append(email) second.phone_number = '+37067412345' second.phone_confirmed = True admin = User.get('*****@*****.**', uni) admin.phone_number = '+37067812375' admin.phone_confirmed = True # Third user has hist email uncofirmed third = User(u'Third user', '*****@*****.**', uni, 'password', True) meta.Session.add(third) email = Email('*****@*****.**') email.confirmed = False third.emails.append(email) # A verified teacher Benas benas = Teacher(fullname=u'Benas', username='******', location=uni, password='******', gen_password=True) benas.teacher_verified = True meta.Session.add(benas) email = Email('*****@*****.**') email.confirmed = True benas.emails.append(email) # Groups: meta.Session.execute("SET LOCAL ututi.active_user TO %d" % admin.id) moderators = Group('moderators', u'Moderatoriai', uni, date(date.today().year, 1, 1), u'U2ti moderatoriai.') meta.Session.add(moderators) moderators.add_member(admin, True) moderators.add_member(third) testgroup = Group('testgroup', u'Testing group', LocationTag.get(u'vu'), date(date.today().year, 1, 1), u'Testing group') meta.Session.add(testgroup) testgroup.mailinglist_enabled = False testgroup.add_member(admin, True) testgroup.add_member(second) # Subjects: math = Subject(u'mat_analize', u'Matematin\u0117 analiz\u0117', LocationTag.get(u'vu'), u'prof. E. Misevi\u010dius') meta.Session.add(math) third.watchSubject(math) # Tags: tag = SimpleTag(u'simple_tag') meta.Session.add(tag) meta.Session.commit()