def save_model(self, request, obj, form, change): lang = form.cleaned_data.get('lang') title = form.cleaned_data.get('title') if is_traditional(lang): Category.sync_simplified_copy(title) obj.title = convert(title.encode('utf-8'), config='zhs2zht.ini').decode('utf-8') return super(CategoryAdmin, self).save_model(request, obj, form, change)
def test_ensure_views_are_positive(self): """ ensure_views_are_positive should results True for categories where views are zero or positive """ cat = Category(name='test',views=-1, likes=0) cat.save() self.assertEqual((cat.views >= 0), True)
def test_slug_line_creation(self): """ slug_line_creation checks to make sure that when we add a category an appropriate slug line is created i.e. "Random Category String" -> "random-category-string" """ cat = Category(name='Random Category String') cat.save() self.assertEqual(cat.slug, 'random-category-string')
def categories(): id = request.values.get('id', default=0, type=int) id_user = current_user.get_id() category = session.query(Category).filter( Category.id == id and Category.id_user == id_user).one_or_none() if not category: category = Category() elif request.args.get('action', default='', type=str) == 'delete': session.delete(category) session.commit() flash( f"Category was deleted (id='{category.id}', name='{category.name}')" ) return redirect(url_for('categories')) form = CategoryForm(obj=category) categories = session.query(Category).filter( Category.id_user == id_user).order_by('id').all() tree = Tree(categories) form.parent_id.choices = tree.return_choises() if form.validate_on_submit(): if form.parent_id.data == "0": form.parent_id.data = None category.add_form_data(form) session.add(category) session.commit() if id: flash( f"Category was updated (id='{category.id}', name='{category.name}')" ) else: flash( f"Category was created (id='{category.id}', name='{category.name}')" ) return redirect(url_for('categories')) to_form = { "title": "Categories", "id": id, "form": form, "categories": categories, } return render_template("categories.html", **to_form)
def category_details(request, idk=None): if idk: category = get_object_or_404(Category, pk=idk) category_form = CategoryForm(request.POST or None, request.FILES or None, instance=category) else: category = Category() category_form = CategoryForm() if request.POST: category_form = CategoryForm(instance=category, data=request.POST, files=request.FILES) if category_form.is_valid(): category_form.save() return redirect('category_list') return render(request, 'category/category_details.html', {'category_form': category_form})
def create_category(): if 'id' in session: name = request.form["name"] img = request.files["image"] file_name = save_category_pic(img) new_category = Category(name=name, prof_img=file_name) try: db.session.add(new_category) db.session.commit() return redirect(url_for('Admin.show_category')) except Exception as e: print(e) db.session.rollback() flash("Category Not Created!!, Please try again", "warning") return redirect(url_for('Admin.show_category')) flash("You are not authorised", "danger") return redirect(url_for('Admin.log_in'))
def parse(request, documentPk): document = Document.objects.get(pk=documentPk) categories = Category.objects.all() runs = Run.objects.all() persons = Person.objects.all() results = Result.objects.all() xmlParser = Parser('/home/django/django_project' + document.xmlfile.url).select_parser() newRun = Run(name=xmlParser.event_data.get('name','NEPOMENOVANY BEH').strip(), date=xmlParser.event_data.get('date','1700-01-01' ), season=document.season) runAlreadyInDatabase = False for everyRun in runs: if everyRun.season == newRun.season and everyRun.name == newRun.name and str(everyRun.date) == newRun.date: runAlreadyInDatabase = True runInXML = everyRun break if runAlreadyInDatabase == False: runInXML = newRun newRun.save() for key in xmlParser.runners_data.keys(): categoryAlreadyInDatabase = False tmpCat = Category(name=key.strip()) for runnerCategory in categories: if tmpCat.name == runnerCategory.name: categoryAlreadyInDatabase = True break if categoryAlreadyInDatabase == False: tmpCat.save() for xmlPerson in xmlParser.runners_data[key]: try: newClub = Club.objects.get(name=unicode(xmlPerson.get('club','N/A')).strip()) except Club.DoesNotExist: newClub = Club(name=unicode(xmlPerson.get('club','N/A')).strip()) newClub.save() newPerson = Person(first_name = xmlPerson['meno'].strip(), last_name = xmlPerson['priezvisko'].strip(), category = Category.objects.get(name=key.strip()), club= Club.objects.get(name=unicode(xmlPerson.get('club','N/A')).strip())) newPerson.ccard=xmlPerson.get('ccard',0) newPerson.person_id=xmlPerson.get('person_id',"0") if len(newPerson.person_id) >= 3: for club in Club.objects.all(): pass if newPerson.ccard is None: newPerson.ccard = 0 personAlreadyInDatabase = False for everyPerson in persons: if newPerson.first_name == everyPerson.first_name and newPerson.last_name == everyPerson.last_name and newPerson.category == everyPerson.category: personAlreadyInDatabase = True personInXML = everyPerson break if personAlreadyInDatabase == False: personInXML = newPerson newPerson.save() newRes = Result(person=Person.objects.get(pk=personInXML.pk), run=Run.objects.get(pk=runInXML.pk),start_time=xmlPerson.get('starttime','00:00:00'),finish_time=xmlPerson.get('finishtime','00:00:00'), status=xmlPerson['status'], points=Decimal("0"), position_run=xmlPerson.get('position',0)) result_time=xmlPerson.get('time','00:00:00') if result_time.count(":") == 1: result_time = "00:" + result_time if result_time.count(":") > 0: time = result_time.encode('ascii', 'ignore') result_time = timeSum(time) newRes.result_time = result_time resultAlreadyInDatabase = False for everyResult in results: if personInXML == everyResult.person and runInXML == everyResult.run: resultAlreadyInDatabase = True break if resultAlreadyInDatabase == False: newRes.save() return HttpResponseRedirect(reverse('web:databasePerson'))
def merge(request): data = pickle.loads(request.session['data']) model = pickle.loads(request.session['model']) if request.method == "POST": if str(model) == 'club': form = ClubForm(request.POST) if form.is_valid(): newClub = Club(name = request.POST['name']) newClub.save() clubsToChange = Club.objects.filter(pk__in = data) runnersToChange = Person.objects.filter(club__in = clubsToChange) for runner in runnersToChange: runner.club = newClub runner.save() #deleting clubs which are now obsolete for club in clubsToChange: club.delete() return HttpResponseRedirect(reverse('admin:index')) elif str(model) == 'category': form = CategoryForm(request.POST) if form.is_valid(): newCategory = Category(name = request.POST['name']) newCategory.save() categoriesToChange = Category.objects.filter(pk__in = data) runnersToChange = Person.objects.filter(category__in = categoriesToChange) for runner in runnersToChange: runner.category = newCategory runner.save() #deleting categories which are now obsolete for category in categoriesToChange: category.delete() return HttpResponseRedirect(reverse('admin:index')) elif str(model) == 'person': form = PersonForm(request.POST) if form.is_valid(): newPerson = Person(first_name = request.POST['first_name'],last_name = request.POST['last_name'],category = Category.objects.get(pk=request.POST['category']),person_id = request.POST['person_id'],club= Club.objects.get(pk=request.POST['club']),ccard = request.POST['ccard']) newPerson.save() runnersToChange = Person.objects.filter(pk__in = data) for oldRunner in runnersToChange: for oldRunnerRun in oldRunner.runs.all(): oldResult = Result.objects.get(person=oldRunner.pk,run = oldRunnerRun.pk) renewResult = oldResult renewResult.person = newPerson renewResult.save() #deleting categories which are now obsolete for runner in runnersToChange: runner.delete() return HttpResponseRedirect(reverse('admin:index')) return HttpResponseRedirect(reverse('web:index')) else: if str(model) == 'club': zoznam = Club.objects.filter(pk__in = data) form = ClubForm(initial={'name':zoznam[0].name, 'shortcut':zoznam[0].shortcut}) elif str(model) == 'person': zoznam = Person.objects.filter(pk__in = data) form = PersonForm(initial={'first_name':zoznam[0].first_name,'last_name':zoznam[0].last_name, 'person_id':zoznam[0].person_id, 'club':zoznam[0].club, 'ccard':zoznam[0].ccard, 'category':zoznam[0].category}) elif str(model) == 'category': zoznam = Category.objects.filter(pk__in = data) form = CategoryForm(initial={'name':zoznam[0].name}) return render(request, 'web/merge.html', {'form':form, 'zoznam':zoznam, })