def initdb(): """Initialize MySQL databse.""" from app.libs.db import init_db from app.models import Permission, User, Topic from app.base.roles import Roles from app.settings import Admins, Topics from app.libs.utils import encrypt_password click.echo('[2L] {0}..'.format(initdb.__doc__)) init_db() click.echo('\n\n[2L] init permisions...') for attr, role in Roles.__dict__.items(): if (not attr.startswith('__') and '{0}' not in role and role != 'root'): click.echo(' -> {0}'.format(role)) Permission.create(role) click.echo('\n\n[2L] init master chief...') bit_sum = Permission.root_permission() for admin in Admins: click.echo(' -> {0}'.format(admin)) if admin['role'] == 'root': admin['role'] = bit_sum else: admin['role'] = (Permission.get_by_role(admin['role']).bit | Permission.get_by_role('comment').bit | Permission.get_by_role('vote').bit) admin['password'] = encrypt_password(admin['password']) User.create(**admin) click.echo('\n\n[2L] create default topics...') for topic in Topics: click.echo(' -> {0}'.format(topic)) Topic.create(**topic)
def setUp(self): super(TopicsTests, self).setUp(self) self._me = USER self._topic = TOPIC self._topic.update({'admin_id': 1}) User.create(**self._me) Topic.create(**self._topic)
def test_patch(self): tdata = TOPIC tdata.update({'admin_id', 1}) Topic.create(**tdata) data = {'description': 'Broken bad'} r = self.patch(self._path.format(1), body=data) body = TOPIC body.update(data) body.update({'status': 1, 'id': 1, 'admin': self._me['username']}) self.assertEqual(r.code, 200) self.assertDictEqual(json.loads(r.body), body) self._mox.VerifyAll()
def test_topic_follows(self): u1 = User.create(email='*****@*****.**', password='******') u2 = User.create(email='*****@*****.**', password='******') topic = Topic.create(title='美术', author=u2) db.session.add_all([u1, u2, topic]) db.session.commit() self.assertFalse(u1.is_following_topic(topic)) self.assertFalse(topic.is_followed_by(u1)) timestamp_before = datetime.datetime.utcnow() u1.follow(topic) timestamp_after = datetime.datetime.utcnow() self.assertTrue(u1.is_following_topic(topic)) self.assertTrue(topic.is_followed_by(u1)) self.assertTrue(u1.followed_topics.count() == 1) self.assertTrue(topic.followers.count() == 1) t = u1.followed_topics.all()[0] self.assertTrue(t.followed == topic) self.assertTrue(timestamp_before <= t.timestamp <= timestamp_after) u1.unfollow(topic) db.session.add(u1) db.session.commit() self.assertFalse(u1.is_following_topic(topic)) self.assertFalse(topic.is_followed_by(u1)) self.assertTrue(u1.followed_topics.count() == 0) self.assertTrue(topic.followers.count() == 0) self.assertTrue(FollowTopic.query.count() == 0)
def post(self, topic_id): name = self.get_argument('name', None) description = self.get_argument('description', None) rules = self.get_argument('rules', None) avatar = self.get_argument('avatar', None) why = self.get_argument('why', None) if not all([name, description, rules, why]): raise exceptions.EmptyFields() else: exists = yield gen.maybe_future(Topic.get_by_name(name)) if exists: raise exceptions.TopicNameAlreadyExists() else: created_user = self.current_user topic = yield gen.maybe_future( Topic.create(name, created_user, avatar, description, rules, why)) # Update Gold. update_gold.apply_async(('new_proposal', created_user)) # Update proposal state. seconds = Level['time']['proposal'] wait = datetime.now(get_localzone()) + timedelta(seconds=seconds) check_proposal.apply_async((topic.id,), eta=wait)
def test_add_remove_question(self): topic = Topic.create(title='摄影') question = Question.create(title='好吗?') questions = [i.question for i in topic.questions.all()] self.assertListEqual(questions, []) self.assertTrue(topic.questions.count() == 0) topic.add_question(question) questions = [i.question for i in topic.questions.all()] self.assertListEqual(questions, [question]) self.assertTrue(topic.questions.count() == 1) topic.remove_question(question) self.assertFalse(topic.is_in_topic(question)) self.assertTrue(topic.questions.count() == 0)
def create_topic(): form = CreateTopicForm() if form.validate_on_submit(): topic = Topic.create(title=form.title.data, description=form.description.data) filename = photos.save(form.photo.data) photo_url = photos.url(form.photo.data) cover_url_sm = image_resize(filename, 30) cover_url = image_resize(filename, 400) topic.cover_url = cover_url topic.cover_url_sm = cover_url_sm db.session.add(topic) db.session.commit() return redirect(url_for('.topics')) return render_template('topic/create_topic.html', form=form)