def create_list(W_count, U_list): L = [] U_count = len(U_list) while W_count > 0: for this_Unit in U_list: U = Units.objects(Title=this_Unit).first() li = U.List if W_count / U_count > len(li): random.shuffle(li) for this_word in li: L.append(Words.objects(Title=this_word).first().Meaning) W_count = W_count - len(li) U_count = U_count - 1 else: k = W_count / U_count while k > 0: wo = random.choice(li) Mean = Words.objects(Title=wo).first().Meaning if Mean in L: continue else: k = k - 1 W_count = W_count - 1 L.append(Mean) return L
def trans_out(T): Lin = '' U = Units.objects(Title=T).first() for i in U.List: W = Words.objects(Title=i).first() Lin = Lin + str(W.Title) + "\n" return Lin
def ones(T): U = Units.objects(Title=T).first() Woli = U.List Li = [] for i in Woli: l = Words.objects(Title=i).first() Li.append({'Title': l.Title, 'Meaning': l.Meaning}) return render_template('U/ones.html', title=T, L=Li)
def rename(T): F = CreateForm() U = Units.objects(Title=T).first() if U == None: abort(404) if F.validate_on_submit(): U.update(Title=F.Title.data) flash('Yeah!') return redirect(url_for('Units_views.ones', T=F.Title.data)) F.Title.data = U.Title return render_template('U/create.html', title='Rename', F=F)
def edit(T): U = Units.objects(Title=T).first() if U == None: abort(404) F = UnitsForm() if F.validate_on_submit(): Eor = trans_in(F.Introduction.data, T) if Eor: flash(u'以下词汇不存在词库中,请先批量加入词库中,\n %s' % Eor) return redirect(url_for('Units_views.ones', T=T)) F.Introduction.data = str(trans_out(T)) return render_template('U/editor.html', title=T, F=F)
def delete(T, N): U = Units.objects(Title=T).first() if U == None: abort(404) if N in U.List: try: L = U.List L.remove(N) U.update(List=L) flash('Success!') except: flash('Fail!') else: abort(404) return redirect(url_for('Units_views.ones', T=T))
def trans_in(Lin, T): U = Units.objects(Title=T).first() L = U.List Lin = Lin.split('\r\n') Eor = [] for i in Lin: title = i if Words.objects(Title=title).first() == None: Eor.append(title) continue if title not in L: L.append(title) U.update(List=L) return Eor
def validate_Units_list(self, field): l = field.data.split(',') for i in l: if Units.objects(Title=i).first() == None: return ValidationError("Can't find %s in database" % i)