def get(self): form = forms.HelloForm() a = A.select().where(A.id == 1).get() blist = [(b, C.select().where(C.parent == b)) for b in B.select().where(B.parent == a)] context = {'a': a, 'blist': blist, 'form': form} alist = A.select().order_by(A.id) context.update(alist=alist) self.render('index.html', **context)
def get(self, aid, bid): b = B.select().where(B.id == bid).get() if b.parent.id != int(aid): aid = b.parent.id self.redirect('/%s_%s/' % (b.parent.id, bid)) a = A.select().where(A.id == aid).get() chapters = [(b, C.select().where(C.parent == b))] context = {'book': a, 'chapter': b, 'chapters': chapters} books = A.select().order_by(A.id) context.update(books=books) self.render("index.html", **context)
def get(self, aid): try: a = A.select().where(A.id == aid).get() chapters = [(b, C.select().where(C.parent == b)) for b in B.select().where(B.parent == a)] context = {'a': a, 'blist': chapters} books = A.select().order_by(A.id) context.update(books=books) self.render("index.html", **context) except A.DoesNotExist: raise tornado.web.HTTPError(404)
def get(self): #print repr(self.application.sitename) user = self.get_current_user() if user: logger.error(str(user)) if user['uid'] > MAX_ADMIN_UID: raise tornado.web.HTTPError(404, 'Page Not Found!') users_total = yield self.get_users_total() users = User.select() channels = A.select() try: config = Config.select().where(Config.id == 1).get() except Config.DoesNotExist: config = Config() msg = self.get_argument('msg', '') page = int(self.get_argument('page', '1')) page_size = 100 offset = page_size * (page - 1) kwargs = { 'msg': msg, 'users': users, 'channels': channels, 'users_total': users_total, 'config': config, } self.render("admin/channel.html", **kwargs)
def send_data(): global sent_initial_data if not sent_initial_data: Controller.subscribe_all() sent_initial_data = True try: if random.random() < 0.5: obj = A(None, x=random.randint(0, 10)) else: obj = random.choice(list(A.objects.values())) print('*** random choice object') obj.x = random.randint(0, 10) except Exception as e: print('-----------error:', e) obj = A(None, x=random.randint(0, 10)) obj.save()
def get(self, aid=None): if aid is None: aid = self.aid form = forms.HelloForm() a = A.select().where(A.id == aid).get() blist = [(b, C.select().where(C.parent == b)) for b in B.select().where(B.parent == a)] context = {'a': a, 'blist': blist, 'form': form} self.render('index.html', **context)
def get(self, aid, bid, cid): try: b = B.select().where(B.id == bid).get() except B.DoesNotExist: raise tornado.web.HTTPError(404) b = B.select().where(B.id == bid).get() if b.parent.id != int(aid): aid = b.parent.id self.redirect('/%s_%s/%s.html' % (b.parent.id, bid, cid)) a = A.select().where(A.id == aid).get() try: c = C.select().where(C.id == cid).get() except C.DoesNotExist: raise tornado.web.HTTPError(404) try: nextone = C.select().where(C.id > c.id, C.parent == b).order_by(C.id).get() except C.DoesNotExist: nextone = None try: previous = C.select().where(C.id < c.id, C.parent == b).order_by( C.id.desc()).get() except C.DoesNotExist: previous = None context = { 'book': a, 'chapter': b, 'article': c, 'previous': previous, 'next': nextone } books = A.select().order_by(A.id) context.update(books=books) print context self.render("detail.html", **context)
def test_collected_objects_by_model_delete(self): ## Test the usage of CollectedObjects by Model.delete() # Due to the way that transactions work in the test harness, # doing m.delete() here can work but fail in a real situation, # since it may delete all objects, but not in the right order. # So we manually check that the order of deletion is correct. # Also, it is possible that the order is correct 'accidentally', due # solely to order of imports etc. To check this, we set the order # that 'get_models()' will retrieve to a known 'nice' order, and # then try again with a known 'tricky' order. Slightly naughty # access to internals here :-) # If implementation changes, then the tests may need to be simplified: # - remove the lines that set the .keyOrder and clear the related # object caches # - remove the second set of tests (with a2, b2 etc) # Nice order cache.app_models['delete'].keyOrder = ['a', 'b', 'c', 'd'] clear_rel_obj_caches([A, B, C, D]) a1 = A() a1.save() b1 = B(a=a1) b1.save() c1 = C(b=b1) c1.save() d1 = D(c=c1, a=a1) d1.save() o = CollectedObjects() a1._collect_sub_objects(o) self.assertQuerysetEqual(o.keys(), ["<class 'modeltests.delete.models.D'>", "<class 'modeltests.delete.models.C'>", "<class 'modeltests.delete.models.B'>", "<class 'modeltests.delete.models.A'>"]) a1.delete() # Same again with a known bad order cache.app_models['delete'].keyOrder = ['d', 'c', 'b', 'a'] clear_rel_obj_caches([A, B, C, D]) a2 = A() a2.save() b2 = B(a=a2) b2.save() c2 = C(b=b2) c2.save() d2 = D(c=c2, a=a2) d2.save() o = CollectedObjects() a2._collect_sub_objects(o) self.assertQuerysetEqual(o.keys(), ["<class 'modeltests.delete.models.D'>", "<class 'modeltests.delete.models.C'>", "<class 'modeltests.delete.models.B'>", "<class 'modeltests.delete.models.A'>"]) a2.delete()
def process(filepath, verbose=False): if not os.path.exists(filepath): raise Exception('filepath(%s) does not exists!' % filepath) with open(filepath,'r') as ff: chapter_flag=False articles=[] chapter_title='' last_chapter_title='' for line in ff.readlines(): line = line.strip('\n') line = line.decode('gbk') if line=='': continue if line[0] != '\t': chapter_flag=True chapter_title=line if last_chapter_title: if not articles: # 当作全书的书名录入, 请注意只录一行, 不要弄多行, 且必须弄一行标题, 不然后面的都挂了 a = A(title=last_chapter_title) a.save() else: b=B(parent= a, title=last_chapter_title) b.save() #b=B.select().where(B.title==last_chapter_title, B.parent==a).get() data_source=[] for article_title in articles: data={ 'title':article_title, 'parent':b, 'content':'', } data_source.append(data) with db.atomic(): C.insert_many(data_source).execute() if verbose: print 'chapter_title', last_chapter_title print 'articles','\n '.join(articles) articles=[] last_chapter_title=chapter_title else: chapter_flag=False articles.append(line.strip()) #cut tab key if last_chapter_title: b=B(parent= a, title=last_chapter_title) b.save() #b=B.select().where(B.title==last_chapter_title, B.parent==a).get() data_source=[] for article_title in articles: data={ 'title':article_title, 'parent':b, 'content':'', } data_source.append(data) with db.atomic(): C.insert_many(data_source).execute() if verbose: print 'chapter_title', last_chapter_title print 'articles','\n '.join(articles)