def publisherInsertQuery(request): #if not request.user.is_authenticated or request.user.username != 'admin': # return redirect('/login') # if POST request, process form data if request.method == 'POST': # create form instance and pass data to ir form = PublisherInsertForm(request.POST) if form.is_valid(): # is it valid? name = form.cleaned_data['name'] city = form.cleaned_data['city'] country = form.cleaned_data['country'] website = form.cleaned_data['website'] pub = Publisher.objects.filter(name__icontains=name) if not pub: publisher = Publisher(name=name, city=city, country=country, website=website) publisher.save() return render(request, 'publisherInserted.html', { 'pub': publisher, 'query': name }) else: return render(request, 'publisherInsertv2.html', { 'error': False, 'exists': True }) # if GET (or any other method), create blank form else: form = PublisherInsertForm() return render(request, 'publisherInsertv2.html', {'form': form})
def publisherInsert(request): if not request.user.is_authenticated or request.user.username != 'admin': return redirect('/login') if 'name' in request.POST: query = request.POST if query: pub = Publisher.objects.filter(name__icontains=query['name']) if not pub: publisher = Publisher(name=query['name'], city=query['city'], country=query['country'], website=query['website']) publisher.save() return render(request, 'publisherInserted.html', { 'pub': publisher, 'query': query['name'] }) else: return render(request, 'publisherInsert.html', { 'error': False, 'exists': True }) else: return render(request, 'publisherInsert.html', { 'error': True, 'exists': False }) else: return render(request, 'publisherInsert.html', { 'error': False, 'exists': False })
def create_api(app, swagger_host=None, swagger_port=5000): custom_swagger = { "info": {"title": "New Title"}, "securityDefinitions": {"ApiKeyAuth": {"type": "apiKey" , "in" : "header", "name": "My-ApiKey"}} } # Customized swagger will be merged api = SAFRSAPI(app, host=swagger_host, port=swagger_port, custom_swagger=custom_swagger) api.expose_object(Thing) api.expose_object(SubThing) for i in range(30): secret = hashlib.sha256(bytes(i)).hexdigest() reader = Person(name="Reader " + str(i), email="reader_email" + str(i), password=secret) author = Person(name="Author " + str(i), email="author_email" + str(i)) book = Book(title="book_title" + str(i)) review = Review( reader_id=reader.id, book_id=book.id, review="review " + str(i) ) if i % 4 == 0: publisher = Publisher(name="name" + str(i)) publisher.books.append(book) reader.books_read.append(book) author.books_written.append(book) for obj in [reader, author, book, publisher, review]: db.session.add(obj) db.session.commit() for model in [Person, Book, Review, Publisher]: # Create an API endpoint api.expose_object(model)
def addpublisher(request): if request.method == 'POST': form = AddPublisherForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] city = form.cleaned_data['city'] country = form.cleaned_data['country'] website = form.cleaned_data['website'] new_publisher = Publisher(name=name, city=city, country=country, website=website) new_publisher.save() return render(request, 'all_publishers.html', {'publishers': Publisher.objects.all()}) else: form = AddPublisherForm() return render(request, 'addpublisher.html', {'form': form})
def add_publisher(): form_publisher = Add_Publisher() if request.method == 'POST': publisherName = request.form['publisherName'] publisher = Publisher(publisherName=publisherName) db.session.add(publisher) db.session.commit() flash('New publisher name has been added!', 'success') elif form_publisher.validate_on_submit(): flash('Error: The publisher alredy exists!', 'danger ') form = Add_Book() return render_template('publisher/publisher_options.html', form=form)
def publisher_registration(): if request.method == 'POST': name = request.form.get('name') domain = request.form.get('domainName') email = request.form.get('email') rss = request.form.get('rssUrl') password = request.form.get('password') password_again = request.form.get('rPassword') img = request.files.get('photo-file') try: validate_name(name) validate_email(email) validate_and_hash_password(password, password_again) except Exception as e: return render_template('index.html') if img: ext = Path(img.filename).suffix file_name = name + ext path = os.path.join('app', 'static', 'media', secure_filename(file_name)) img.save(path) img = url_for('static', filename=f'media/{file_name}') else: img = '' if password == password_again: password = pbkdf2_sha256.hash(password) pub = Publisher(name=name, url=domain, rss=rss, image=img) user = User(first_name=name, last_name='', email=email, password=password, role=Role.PUBLISHER, publisher=pub) db.session.add(pub) db.session.add(user) db.session.commit() login_user(user) return redirect(url_for('index')) return render_template('index.html')
def get_article(url, domain=None, art_name='', art_date=date.today(), art_desc='', art_category=None): """ Fetches article from database, if article wasn't found, creates new article object :param url: Article url, used to fetch article from database :param domain: :return: article object """ article = Article.query.filter_by(url=url).first() if not article: publisher = Publisher.query.filter_by(url=domain).first() if not publisher: if domain: publisher = Publisher(name=domain, url=domain, rss=domain + '/rss') db.session.add(publisher) db.session.commit() else: publisher = Publisher.query.filter_by(name='mock').first() article = Article(url=url, publisher=publisher, name=art_name, category=art_category, date=art_date, description=art_desc) db.session.add(article) db.session.commit() return article
def create_api(app, swagger_host=None, swagger_port=5000): custom_swagger = { "info": { "title": "New Title" }, "securityDefinitions": { "ApiKeyAuth": { "type": "apiKey", "in": "header", "name": "My-ApiKey" } } } # Customized swagger will be merged api = SAFRSAPI(app, host=swagger_host, port=swagger_port, custom_swagger=custom_swagger, decorators=[safrs.test_decorator]) api.expose_object(Thing) api.expose_object(ThingWType) api.expose_object(SubThing) api.expose_object(ThingWOCommit) api.expose_object(ThingWCommit) api.expose_object(Test) api.expose_object(AuthUser) for i in range(10): secret = hashlib.sha256(bytes(i)).hexdigest() reader = Person(name="Reader " + str(i), email="reader_email" + str(i), password=secret) author = Person(name="Author " + str(i), email="author_email" + str(i)) book = Book(title="book_title" + str(i)) unexp_book = Book(title="unexp_book_title" + str(i)) review = Review(reader_id=reader.id, book_id=book.id, review="review " + str(i)) if i % 4 == 0: publisher = Publisher(name="name" + str(i)) publisher.books.append(book) publisher.books.append(unexp_book) reader.books_read.append(book) author.books_written.append(book) for obj in [reader, author, book, publisher, review]: db.session.add(obj) for i in range(20): item = PKItem(foo="item_" + str(i), bar="group_" + str((int)(i / 10)), pk_A=str(i), pk_B=str(i), id=8) user = UserWithPerms(name=f"name{i}", email="some@mail") db.session.add(item) db.session.add(user) db.session.commit() for model in [ Person, Book, Review, Publisher, PKItem, UserWithJsonapiAttr, UserWithPerms ]: # Create an API endpoint api.expose_object(model)
Book.objects.raw('..SQL..') ## raw() method to use raw SQL Q( ) ## Q() object, like an F object, encapsulates a SQL expression in a Python object. # In general, Q() objects make it possible to define and reuse conditions. This permits the # construction of complex database queries using | (OR) and & (AND) operators; in particular, # it is not otherwise possible to use OR in QuerySets. # 5-----------------------------Model Python API---------------------------------- # # operate DB models in Python code: from app.models import Publisher # create p = Publisher(name='xx') p.save() p = Publisher.objects.create(name='yy') # delete p.delete() Publisher.objects.all().delete() # update p.update(name='zz') Publisher.objects.all().update(country='USA') # select Publisher.objects.all() Publisher.objects.filter(name='vv') Publisher.objects.filter(name__contains="x")