Пример #1
0
def newChapter():
    if request.method == 'POST':
        dt = datetime.strptime(request.form['release_date'], '%Y-%m-%d')
        #ADDING CHAPTER
        chapter = Chapter(
            title = request.form['title'],
            manga_id = request.form['manga'],
            number = request.form['number'],
            release_date = dt,
        )

        
        db.session.add(chapter)

        db.session.flush() #cheat to get the Id after insert, 
        chapterId = chapter.id

        db.session.commit()
        #ADDING CHAPTER

        #ADDING PAGES TO CHAPTER
        count = 0
        uploaded_files = request.files.getlist("page[]")
        manga = Manga.query.filter_by(id=request.form['manga']).first()        

        for file in uploaded_files:
            count += 1
            
            filename = file.filename     
            
            #Each manga needs a folder, then each chapter needs one, then we insert the pages in
            mangaTitle = manga.title.replace(" ", "-")

            target = os.path.join(app.config['UPLOAD_FOLDER'], 'mangas')        

            target = os.path.join(target, mangaTitle)     

            if not os.path.isdir(target):
                os.mkdir(target)   

            target = os.path.join(target, 'chapter-' + request.form['number'])   

            if not os.path.isdir(target):
                os.mkdir(target)       

            title = 'page'+str(count)
            ext = os.path.splitext(filename)[1]
            destination = "/".join([target, title+ext])
            file.save(destination)

            page = Page(
                chapter_id = chapterId,
                page_number = count,
                image = 'mangas/' + mangaTitle + '/chapter-' + request.form['number'] + "/" + title + ext
            )

            db.session.add(page)
            db.session.commit()            
       

        flash('Chapter Registered!')
        return redirect(url_for('manga.read', id=manga.id))        

        #ADDING PAGES TO CHAPTER

    return render_template('/manga/chapter/new.html')
Пример #2
0
def import_rechem():
    con = sqlite3.connect('app/rechem_listings.db')
    con.row_factory = sqlite3.Row  # returns sqlite3 query result as dict rather than tuple
    c = con.cursor()
    c.execute("SELECT * FROM country")
    countries = c.fetchall()
    new_countries = []
    for row in countries:
        country = Country(name=row['name'], c2=row['c2'])
        if not Country.query.filter_by(name=country.name).first():
            new_countries.append(country)
    db.session.add_all(new_countries)
    db.session.commit()
    c.execute("SELECT * FROM drug")
    drugs = c.fetchall()
    new_drugs = []
    for row in drugs:
        drug = Drug(name=row['name'])
        if not Drug.query.filter_by(name=drug.name).first():
            new_drugs.append(drug)
    db.session.add_all(new_drugs)
    db.session.commit()
    c.execute("SELECT * FROM market")
    markets = c.fetchall()
    new_markets = []
    for row in markets:
        market = Market(name=row['name'])
        if not Market.query.filter_by(name=market.name).first():
            new_markets.append(market)
    db.session.add_all(new_markets)
    db.session.commit()
    c.execute("SELECT * FROM listing")
    listings = c.fetchall()
    rechem_listings = [
        listing for listing in listings if listing['market_id'] == '4'
    ]
    rechem_ids = [d['id'] for d in rechem_listings]
    new_listings = []
    for row in rechem_listings:
        new_market_id = Market.query.filter_by(name="Rechem").first().id

        drug_name = [d for d in drugs
                     if d['id'] == int(row['drug_id'])][0]['name']
        new_drug_id = Drug.query.filter_by(name=drug_name).first().id

        # TODO: this is required for sqlite, but may or may not work with sqlalchemy
        time_format = "%Y-%m-%d %H:%M:%S.%f"
        timestamp = datetime.strptime(row['timestamp'], time_format)

        listing = Listing(url=row['url'],
                          seller=None,
                          timestamp=timestamp,
                          market_id=new_market_id,
                          drug_id=new_drug_id,
                          origin_id=None)
        if not Listing.query.filter_by(url=listing.url).first():
            new_listings.append(listing)
    db.session.add_all(new_listings)
    db.session.commit()
    c.execute("SELECT * FROM page")
    pages = c.fetchall()
    rechem_pages = [page for page in pages if page['listing_id'] in rechem_ids]
    new_pages = []
    for row in rechem_pages:
        listing_url = [
            d for d in listings if d['id'] == int(row['listing_id'])
        ][0]['url']
        new_listing_id = Listing.query.filter_by(url=listing_url).first().id

        # TODO: this is required for sqlite, but may or may not work with sqlalchemy
        time_format = "%Y-%m-%d %H:%M:%S.%f"
        timestamp = datetime.strptime(row['timestamp'], time_format)

        page = Page(name=row['name'],
                    html=row['html'],
                    timestamp=timestamp,
                    listing_id=new_listing_id)
        if not Page.query.filter_by(listing_id=page.listing_id,
                                    timestamp=page.timestamp).first():
            new_pages.append(page)
        else:
            print("page already found:")
    db.session.add_all(new_pages)
    db.session.commit()

    return "", 204