Example #1
0
def update_article_details():
    """Updates an article's details."""

    new_price = request.form.get('purchase-price')
    article_id = request.form.get('article-to-edit')
    tag_ids = request.form.getlist('article-tags')
    new_tag_string = request.form.get('new-tags')
    article = Article.query.filter_by(article_id=article_id).one()

    if new_price:
        article.update({'purchase_price': new_price})

    all_tags = []
    for tag_id in tag_ids:
        all_tags.append(Tag.query.filter_by(tag_id=tag_id).one())

    # TODO: Brute force method - remove all tags before appending
    # Better: Check for discrepancies; remove unchecked, then proceed

    # Any newly created tags should be added to this as well
    all_tags += Tag.parse_str_to_tag(new_tag_string, session['user_id'])

    # Then create all the tag relationships
    for tag in all_tags:
        article.add_tag(tag)

    return redirect(f'/articles/{article_id}')
Example #2
0
def add_event():
    """Adds new event and redirects to the previous events page."""

    # String unpacking to pass as arguments to datetime
    year, month, day = request.form.get('event-date').split('-')
    time = request.form.get('event-time')
    city = request.form.get('city')
    description = request.form.get('event-description')
    name = request.form.get('event-name')
    outfit_id = request.form.get('event-outfit')
    tag_ids = request.form.getlist('event-tags')
    new_tag_string = request.form.get('new-tags')

    if time:
        hour, minute = time.split(':')
        date_time = datetime(int(year), int(month), int(day), int(hour),
                             int(minute))
    else:
        date_time = datetime(int(year), int(month), int(day), int(10))

    # First create a new Event in the db
    event = WearEvent(user_id=session['user_id'],
                      outfit_id=outfit_id or None,
                      description=description or None,
                      name=name or f'{month}-{day}-{year}',
                      date=date_time)

    # If location is provided, get weather
    if city:
        event.set_weather(CITIES[city]['lat'], CITIES[city]['lng'])

    if outfit_id:
        outfit = Outfit.query.filter_by(outfit_id=outfit_id).one()
        outfit.incr_times_worn()

    all_tags = []
    for tag_id in tag_ids:
        all_tags.append(Tag.query.filter_by(tag_id=tag_id).one())

    # Any newly created tags should be added to this as well
    all_tags += Tag.parse_str_to_tag(new_tag_string, session['user_id'])

    # Then create all the tag relationships
    for tag in all_tags:
        event.add_tag(tag)

    db.session.add(event)
    db.session.commit()

    text = name if name else description

    flash(f"Created new event: {text}")

    return redirect('/events')
Example #3
0
def add_article():
    """Adds new clothing article and redirects to the previous category page."""

    category_id = request.form.get('category')
    description = request.form.get('article-description')
    file = request.files['article-image-upload']
    tag_ids = request.form.getlist('article-tags')
    new_tag_string = request.form.get('new-tags')
    purchase_price = request.form.get('purchase-price')

    category = Category.query.get(category_id)

    if not allowed_file(file.filename):
        flash(f'File extension .{file.filename.rsplit(".", 1)[1]} not allowed')
    if file and allowed_file(file.filename):

        # Sanitizes user input
        filename = secure_filename(file.filename)

        # Cloudinary upload function: 1) folders by user and category name,
        # 2) unique filename is true,
        # 3) use cloudinary's AI to remove background
        # ^ (commented out b/c paid service)
        upload_file = upload(
            file,
            folder=f"user/{session['user_email']}/{category.name}",
            unique_filename=1,
            # background_removal = "cloudinary_ai",
        )

        # For purchase_price, an empty string not ok, but okay to pass None
        new_article = Article(user_id=session['user_id'],
                              category_id=category_id,
                              image=upload_file['secure_url'],
                              description=description,
                              purchase_price=purchase_price or None)

        all_tags = []
        for tag_id in tag_ids:
            all_tags.append(Tag.query.filter_by(tag_id=tag_id).one())

        # Any newly created tags should be added to this as well
        all_tags += Tag.parse_str_to_tag(new_tag_string, session['user_id'])

        # Then create all the tag relationships
        for tag in all_tags:
            new_article.add_tag(tag)

        db.session.add(new_article)
        db.session.commit()
        flash(f"Created new item in {category.name}")

    return redirect(f'/categories/{category_id}')
Example #4
0
def update_outfit_details():
    """Updates an outfit's details."""

    outfit_id = request.form.get('outfit-to-edit')
    new_tag_string = request.form.get('new-tags')
    tag_ids = request.form.getlist('outfit-tags')
    outfit = Outfit.query.filter_by(outfit_id=outfit_id).one()

    all_tags = []
    for tag_id in tag_ids:
        all_tags.append(Tag.query.filter_by(tag_id=tag_id).one())

    # Any newly created tags should be added to this as well
    all_tags += Tag.parse_str_to_tag(new_tag_string, session['user_id'])

    # Then create all the tag relationships
    for tag in all_tags:
        outfit.add_tag(tag)

    return redirect(f'/outfits/{outfit_id}')
Example #5
0
def add_outfit():
    """Adds new outfit and redirects to the previous outfits page."""

    description = request.form.get('outfit-description')
    name = request.form.get('outfit-name')
    article_ids = request.form.getlist('articles-to-add')
    tag_ids = request.form.getlist('outfit-tags')
    new_tag_string = request.form.get('new-tags')

    # First create a new Outfit in the db
    outfit = Outfit(user_id=session['user_id'],
                    description=description,
                    name=name)
    db.session.add(outfit)

    # Then create all the article relationships
    for article_id in article_ids:
        article = Article.query.filter(Article.article_id == article_id).one()
        outfit.add_article(article)

    all_tags = []
    for tag_id in tag_ids:
        all_tags.append(Tag.query.filter_by(tag_id=tag_id).one())

    # Any newly created tags should be added to this as well
    all_tags += Tag.parse_str_to_tag(new_tag_string, session['user_id'])

    # Then create all the tag relationships
    for tag in all_tags:
        outfit.add_tag(tag)

    db.session.commit()

    text = name if name else description

    flash(f"Created new outfit: {text}")

    return redirect('/outfits')