def create_group():
    """Handle submission of new group form"""

    user = User.query.get(session["user_id"])
    group_name = request.form.get("group_name")
    group_descrip = request.form.get("group_descrip")
    hashtag = request.form.get("hashtag")

    if hashtag != "":
        hashtag = '#makealong' + hashtag
    else:
        hashtag = None

    if request.form.get("group_image") == " ":
        filename = photos.save(request.files['photo'])
        group_image = str(photos.path(filename))

    else:
        group_image = request.form.get("group_image")

    #pattern poll was created#
    if request.form.get("vote_days"):
        vote_days = request.form.get("vote_days")
        vote_timestamp = datetime.now()

        group = Group(group_name=group_name,
                      group_descrip=group_descrip, 
                      group_image=group_image,  
                      admin_id=user.user_id,
                      vote_days=vote_days,
                      vote_timestamp=vote_timestamp,
                      hashtag=hashtag)

        db.session.add(group)
        db.session.commit()

        helper.create_patterns_for_poll(group.group_id)
        
    else:
    #no pattern poll#    
        group = Group(group_name=group_name,
                      group_descrip=group_descrip, 
                      group_image=group_image,  
                      admin_id=user.user_id,
                      hashtag=hashtag)

        db.session.add(group)
        db.session.commit()

        if request.form.get("pattern_name"):             
            helper.add_chosen_pattern("pattern_name", "pattern_link","pattern_pdf", group.group_id)
        
    user_group= UserGroup(group_id=group.group_id,
                          user_id=user.user_id)

    db.session.add(user_group)
    db.session.commit()
    
    return redirect("/group_home/%d" % (group.group_id))
def update_group_profile(group_id):
    """Update group profile using inputs from group update form"""

    group = Group.query.get(group_id)

    if group.is_user_in_group(session["user_id"])==False:
        return redirect("/user") 

    else: 
        chosen_pattern = Pattern.query.filter(Pattern.group_id == group_id, Pattern.chosen == True)

        update_group_name = request.form.get("group_name")
        
        update_group_descrip = request.form.get("group_descrip")
        update_group_hashtag = request.form.get("hashtag")
        
        update_group_pattern_name = request.form.get("update_pattern_name")
        update_group_pattern_link = request.form.get("update_pattern_link")
        
    #basic user description update
        if update_group_name != "":
            group.group_name = update_group_name
            db.session.commit()

        if update_group_descrip != "":
            group.group_descrip = update_group_descrip
            db.session.commit()

        if update_group_hashtag != "":
            group.hashtag = update_group_hashtag
            db.session.commit()

        if "group_img" in request.files and request.files['group_img'].filename:
            group_photo_filename = photos.save(request.files["group_img"])
            update_group_image = str(photos.path(group_photo_filename))
            group.group_image = update_group_image
            db.session.commit()

    #update if group had a pattern, and is just changing info about it.
        if update_group_pattern_name != "":
            chosen_pattern.pattern_name = update_group_pattern_name
            db.session.commit()

        if update_group_pattern_link != "":
            chosen_pattern.pattern_link = update_group_pattern_link
            db.session.commit()

        if "update_pattern_pdf" in request.files and request.files['update_pattern_pdf'].filename:
            pattern_pdf_filename = manuals.save(request.files['update_pattern_pdf'])
            new_group_pattern_pdf = str(manuals.path(pattern_pdf_filename))
            chosen_pattern.pattern_pdf = new_group_pattern_pdf
            db.session.commit()

    # add pattern if one is selected and a poll is not created.

        if (request.form.get("new_pattern_name") 
            or request.form.get("new_pattern_link") 
            or ("new_pattern_pdf" in request.files and request.files['new_pattern_pdf'].filename)):

            helper.add_chosen_pattern("new_pattern_name", "new_pattern_link","new_pattern_pdf", group.group_id)

    ##info if pattern poll was created##
        
        if request.form.get("vote_days"):
            vote_days = request.form.get("vote_days")
            vote_timestamp = datetime.now()
            group.vote_days = vote_days
            group.vote_timestamp = vote_timestamp
            db.session.commit()

            helper.create_patterns_for_poll(group.group_id)

    return redirect("/group_home/%d" % (group.group_id))
def update_group_profile(group_id):
    """Update group profile using inputs from group update form"""

    # Get group from database, use instance method on Group class to check if
    # user in the session is in the group, if not redirect to user homepage.

    group = Group.query.get(group_id)

    if group.is_user_in_group(session["user_id"])==False:
        return redirect("/user") 

    # Get form inputs
    else:
        if group.admin_id == session["user_id"]: 
            chosen_pattern = Pattern.query.filter(Pattern.group_id == group_id, Pattern.chosen == True)

            update_group_name = request.form.get("group_name")
            
            update_group_descrip = request.form.get("group_descrip")
            update_group_hashtag = request.form.get("hashtag")
            
            update_group_pattern_name = request.form.get("update_pattern_name")
            update_group_pattern_link = request.form.get("update_pattern_link")
            
        #Handle basic user name, description, hashtag and image updates from inputs
            if update_group_name != "":
                group.group_name = update_group_name
                # db.session.commit()

            if update_group_descrip != "":
                group.group_descrip = update_group_descrip
                # db.session.commit()

            if update_group_hashtag != "":
                group.hashtag = '#makealong' + update_group_hashtag
                # db.session.commit()

            if "group_img" in request.files and request.files['group_img'].filename:
                group_photo_filename = photos.save(request.files["group_img"])
                update_group_image = str(photos.path(group_photo_filename))
                group.group_image = update_group_image
                # db.session.commit()

        #Update if group had a pattern, and is just changing info about it.
            if update_group_pattern_name != "":
                chosen_pattern.pattern_name = update_group_pattern_name
                # db.session.commit()

            if update_group_pattern_link != "":
                chosen_pattern.pattern_link = update_group_pattern_link
                # db.session.commit()

            if "update_pattern_pdf" in request.files and request.files['update_pattern_pdf'].filename:
                pattern_pdf_filename = manuals.save(request.files['update_pattern_pdf'])
                new_group_pattern_pdf = str(manuals.path(pattern_pdf_filename))
                chosen_pattern.pattern_pdf = new_group_pattern_pdf
                # db.session.commit()

        # Add a pattern to database using helper function if a pattern was inputed
        # on the form and a poll was not created.

            if (request.form.get("new_pattern_name") 
                or request.form.get("new_pattern_link") 
                or ("new_pattern_pdf" in request.files and request.files['new_pattern_pdf'].filename)):

                helper.add_chosen_pattern("new_pattern_name", "new_pattern_link","new_pattern_pdf", group.group_id)

        # If vote days was entered in table, a pattern poll was created. Update fields
        # in group table for poll and use pattern poll helper function to instantiate
        # pattern objects in database.
            
            if request.form.get("vote_days"):
                vote_days = request.form.get("vote_days")
                vote_timestamp = datetime.now()
                group.vote_days = vote_days
                group.vote_timestamp = vote_timestamp
                db.session.commit()

                helper.create_patterns_for_poll(group.group_id)
                

    return redirect("/group_home/%d" % (group.group_id))
def create_group():
    """Handle submission of new group form"""

    # form inputs
    group_name = request.form.get("group_name")
    group_descrip = request.form.get("group_descrip")
    hashtag = request.form.get("hashtag")

    # If a hashtag was entered append makealong tag to beginning of value
    if hashtag != "":
        hashtag = '#makealong' + hashtag
    else:
        hashtag = None

    # If upload your own photo radio button was selected save the photo from 
    # request files to the photo folder and save the photo path in database. 
    # If one of the default photos was selected, set group_image to the photo 
    # path for the default photo selected.

    if request.form.get("group_image") == " ":
        filename = photos.save(request.files['photo'])
        group_image = str(photos.path(filename))

    else:
        group_image = request.form.get("group_image")

    # New group object if pattern poll was created, use vote days field as poll 
    # indicator. Using pattern helper function that instantiates poll patterns in 
    # the database.

    if request.form.get("vote_days"):
        vote_days = request.form.get("vote_days")
        vote_timestamp = datetime.now()

        group = Group(group_name=group_name,
                      group_descrip=group_descrip, 
                      group_image=group_image,  
                      admin_id=session["user_id"],
                      vote_days=vote_days,
                      vote_timestamp=vote_timestamp,
                      hashtag=hashtag)
        # disabled for demo
        # db.session.add(group)
        # db.session.commit()

        helper.create_patterns_for_poll(group.group_id)
    
    # New group without pattern poll.      
    else:
        group = Group(group_name=group_name,
                      group_descrip=group_descrip, 
                      group_image=group_image,  
                      admin_id=session["user_id"],
                      hashtag=hashtag)
        # disabled for demo
        # db.session.add(group)
        # db.session.commit()

        # If pattern name was entered in form, call helper function to instantiate
        # pattern in database.

        if request.form.get("pattern_name"):             
            helper.add_chosen_pattern("pattern_name", "pattern_link","pattern_pdf", group.group_id)
    
    # Instantiate a new UserGroup object and save it to usergroups which is an 
    # association table for groups and users.

    user_group= UserGroup(group_id=group.group_id,
                          user_id=session["user_id"])
    # disabled for demo
    # db.session.add(user_group)
    # db.session.commit()
    
    return redirect("/group_home/%d" % (group.group_id))
Beispiel #5
0
def update_group_profile(group_id):
    """Update group profile using inputs from group update form"""

    # Get group from database, use instance method on Group class to check if
    # user in the session is in the group, if not redirect to user homepage.

    group = Group.query.get(group_id)

    if group.is_user_in_group(session["user_id"]) == False:
        return redirect("/user")

    # Get form inputs
    else:
        if group.admin_id == session["user_id"]:
            chosen_pattern = Pattern.query.filter(Pattern.group_id == group_id,
                                                  Pattern.chosen == True)

            update_group_name = request.form.get("group_name")

            update_group_descrip = request.form.get("group_descrip")
            update_group_hashtag = request.form.get("hashtag")

            update_group_pattern_name = request.form.get("update_pattern_name")
            update_group_pattern_link = request.form.get("update_pattern_link")

            #Handle basic user name, description, hashtag and image updates from inputs
            if update_group_name != "":
                group.group_name = update_group_name
                # db.session.commit()

            if update_group_descrip != "":
                group.group_descrip = update_group_descrip
                # db.session.commit()

            if update_group_hashtag != "":
                group.hashtag = '#makealong' + update_group_hashtag
                # db.session.commit()

            if "group_img" in request.files and request.files[
                    'group_img'].filename:
                group_photo_filename = photos.save(request.files["group_img"])
                update_group_image = str(photos.path(group_photo_filename))
                group.group_image = update_group_image
                # db.session.commit()

        #Update if group had a pattern, and is just changing info about it.
            if update_group_pattern_name != "":
                chosen_pattern.pattern_name = update_group_pattern_name
                # db.session.commit()

            if update_group_pattern_link != "":
                chosen_pattern.pattern_link = update_group_pattern_link
                # db.session.commit()

            if "update_pattern_pdf" in request.files and request.files[
                    'update_pattern_pdf'].filename:
                pattern_pdf_filename = manuals.save(
                    request.files['update_pattern_pdf'])
                new_group_pattern_pdf = str(manuals.path(pattern_pdf_filename))
                chosen_pattern.pattern_pdf = new_group_pattern_pdf
                # db.session.commit()

        # Add a pattern to database using helper function if a pattern was inputed
        # on the form and a poll was not created.

            if (request.form.get("new_pattern_name")
                    or request.form.get("new_pattern_link")
                    or ("new_pattern_pdf" in request.files
                        and request.files['new_pattern_pdf'].filename)):

                helper.add_chosen_pattern("new_pattern_name",
                                          "new_pattern_link",
                                          "new_pattern_pdf", group.group_id)

        # If vote days was entered in table, a pattern poll was created. Update fields
        # in group table for poll and use pattern poll helper function to instantiate
        # pattern objects in database.

            if request.form.get("vote_days"):
                vote_days = request.form.get("vote_days")
                vote_timestamp = datetime.now()
                group.vote_days = vote_days
                group.vote_timestamp = vote_timestamp
                db.session.commit()

                helper.create_patterns_for_poll(group.group_id)

    return redirect("/group_home/%d" % (group.group_id))
Beispiel #6
0
def create_group():
    """Handle submission of new group form"""

    # form inputs
    group_name = request.form.get("group_name")
    group_descrip = request.form.get("group_descrip")
    hashtag = request.form.get("hashtag")

    # If a hashtag was entered append makealong tag to beginning of value
    if hashtag != "":
        hashtag = '#makealong' + hashtag
    else:
        hashtag = None

    # If upload your own photo radio button was selected save the photo from
    # request files to the photo folder and save the photo path in database.
    # If one of the default photos was selected, set group_image to the photo
    # path for the default photo selected.

    if request.form.get("group_image") == " ":
        filename = photos.save(request.files['photo'])
        group_image = str(photos.path(filename))

    else:
        group_image = request.form.get("group_image")

    # New group object if pattern poll was created, use vote days field as poll
    # indicator. Using pattern helper function that instantiates poll patterns in
    # the database.

    if request.form.get("vote_days"):
        vote_days = request.form.get("vote_days")
        vote_timestamp = datetime.now()

        group = Group(group_name=group_name,
                      group_descrip=group_descrip,
                      group_image=group_image,
                      admin_id=session["user_id"],
                      vote_days=vote_days,
                      vote_timestamp=vote_timestamp,
                      hashtag=hashtag)
        # disabled for demo
        # db.session.add(group)
        # db.session.commit()

        helper.create_patterns_for_poll(group.group_id)

    # New group without pattern poll.
    else:
        group = Group(group_name=group_name,
                      group_descrip=group_descrip,
                      group_image=group_image,
                      admin_id=session["user_id"],
                      hashtag=hashtag)
        # disabled for demo
        # db.session.add(group)
        # db.session.commit()

        # If pattern name was entered in form, call helper function to instantiate
        # pattern in database.

        if request.form.get("pattern_name"):
            helper.add_chosen_pattern("pattern_name", "pattern_link",
                                      "pattern_pdf", group.group_id)

    # Instantiate a new UserGroup object and save it to usergroups which is an
    # association table for groups and users.

    user_group = UserGroup(group_id=group.group_id, user_id=session["user_id"])
    # disabled for demo
    # db.session.add(user_group)
    # db.session.commit()

    return redirect("/group_home/%d" % (group.group_id))