Exemplo n.º 1
0
def userdata_different_trading_partners_vendor(user_id, otherid):
    """
    # adds diff partners to user file
    # get vendor txt and write customer id
    :param user_id:
    :param otherid:
    :return:
    """
    # get the user
    user = db.session\
        .query(Auth_User)\
        .filter(user_id == Auth_User.id)\
        .first()
    getuserlocation = userimagelocation(user_id=user_id)
    # get stats if vendor
    itemsbought = db.session\
        .query(Profile_StatisticsVendor)\
        .filter(user_id == Profile_StatisticsVendor.vendorid)\
        .first()
    # find path of the user

    thepath = os.path.join(UPLOADED_FILES_DEST_USER, getuserlocation,
                           str(user_id))
    # make a directory if doesnt have it..should tho
    mkdir_p(path=thepath)
    # text file is user_id
    usertextfile = str(user.id) + ".txt"
    # text file location
    userfile = os.path.join(thepath, usertextfile)
    # get the other persons id
    othertraderid = str(otherid)
    text_file = open(userfile, "a")
    f = open(userfile, 'r')
    x = set(f.read().split(','))
    if str(otherid) in x:
        y = (len(x)) - 1
        itemsbought.diffpartners = y
    else:
        text_file.write(othertraderid + ',')
        text_file.close()
        y = (len(x))
        itemsbought.diffpartners = y

    db.session.add(itemsbought)
Exemplo n.º 2
0
def profilepicture():

    id_pic1 = id_generator_picture1()

    form = ProfilePicForm(CombinedMultiDict((request.files, request.form)), )
    user = User.query.filter_by(id=current_user.id).first()
    if request.method == 'POST':

        if form.delete.data and form.validate_on_submit():
            deleteuserprofileimage(user_id=user.id)
            return redirect((request.args.get('next', request.referrer)))

        if form.submit.data and form.validate_on_submit():

            getusernodelocation = userimagelocation(x=user.id)
            userlocation = os.path.join(UPLOADED_FILES_DEST, current_disk,
                                        'user', getusernodelocation,
                                        str(user.id))

            if form.imageprofile.data:
                try:
                    mkdir_p(path=userlocation)
                    deleteuserprofileimage(user_id=current_user.id)
                    filename = secure_filename(form.imageprofile.data.filename)
                    # makes directory (generic location + auction number id as folder)
                    # saves it to location
                    profileimagefilepath = os.path.join(userlocation, filename)
                    form.imageprofile.data.save(profileimagefilepath)
                    # RENAMING FILE
                    # split file name and ending
                    filenamenew, file_extension = os.path.splitext(
                        profileimagefilepath)
                    # gets new 64 digit filename
                    newfilename = id_pic1 + file_extension
                    # puts new name with ending
                    filenamenewfull = filenamenew + file_extension
                    # gets aboslute path of new file
                    newfilenamedestination = os.path.join(
                        userlocation, newfilename)
                    # renames file
                    os.rename(filenamenewfull, newfilenamedestination)
                    convert_profile_image(imagelocation=userlocation,
                                          imagename=newfilename)
                except Exception as e:
                    user.profileimage = "0"
                    flash("Error", category="success")
                    return redirect((request.args.get('next',
                                                      request.referrer)))
                if not form.imageprofile.data.filename:

                    user.profileimage = ""

                db.session.add(user)
                db.session.commit()
                flash("Profile has been changed", category="success")
                return redirect((request.args.get('next', request.referrer)))
        else:
            flash("Error.  No data submitted", category="danger")
            return redirect((request.args.get('next', request.referrer)))

    return render_template(
        'users/profile/profile_forms/profile_picture.html',
        form=form,
        user=user,
    )
Exemplo n.º 3
0
def post_edit_text(postid):
    now = datetime.utcnow()
    editposttextform = EditPostTextForm()
    id_pic1 = id_generator_picture1()

    thepost = db.session \
        .query(CommonsPost) \
        .filter(CommonsPost.id == int(postid)) \
        .first()

    if request.method == 'POST':

        if editposttextform.validate_on_submit():
            if editposttextform.delete.data:
                delete_image_server_one(user_id=current_user.id, postid=postid)
                return redirect((request.args.get('next', request.referrer)))

            if editposttextform.submit.data:
                urlfound, urltitlefound, urldescriptionfound, urlimagefound = geturl(editposttextform.postmessage.data)
                transformed_text, notifyuser = transform_image_links_markdown(str(editposttextform.postmessage.data))

                getpostnodelocation = postnodelocation(x=thepost.id)
                postlocation = os.path.join(UPLOADED_FILES_DEST,
                                            current_disk,
                                            "post",
                                            getpostnodelocation,
                                            str(thepost.id))
                thepost.post_text = transformed_text

                if urlfound:
                    delete_image_url_server(user_id=current_user.id, postid=postid)
                    thepost.url_image = urlimagefound
                    thepost.url_title = urltitlefound
                    thepost.url_of_post = urlfound
                    thepost.url_description = urldescriptionfound
                    post_get_image(url=thepost.url_image, imagelocation=postlocation, thepost=thepost)

                if editposttextform.image_one.data:
                    mkdir_p(path=postlocation)
                    filename = secure_filename(editposttextform.image_one.data.filename)
                    postimagefilepath = os.path.join(postlocation, filename)
                    editposttextform.image_one.data.save(postimagefilepath)
                    filenamenew, file_extension = os.path.splitext(postimagefilepath)
                    newfilename = id_pic1 + file_extension
                    filenamenewfull = filenamenew + file_extension
                    newfilenamedestination = os.path.join(postlocation, newfilename)
                    os.rename(filenamenewfull, newfilenamedestination)
                    post_convert_image(imagelocation=postlocation, imagename=newfilename, thepost=thepost)

                if notifyuser is not None:
                    # add info
                    add_new_notification(user_id=notifyuser.id,
                                         subid=thepost.subcommon_id,
                                         subname=thepost.subcommon_name,
                                         postid=thepost.id,
                                         commentid=0,
                                         msg=32
                                         )
            thepost.edited = now
            db.session.commit()

            flash("Post edited", category="success")
            return redirect(url_for('subforum.viewpost', subname=thepost.subcommon_name, postid=thepost.id))
        else:
            flash("form error")
            for errors in editposttextform.postmessage.errors:
                flash(errors, category="danger")
            return redirect((request.args.get('next', request.referrer)))
Exemplo n.º 4
0
def register():
    form = RegistrationForm()

    if request.method == 'POST':
        if form.validate_on_submit():

            try:
                theanonid = random_user_name_anon()
                now = datetime.utcnow()
                cryptedpwd = User.cryptpassword(password=form.password.data)

                # add user to db
                newuser = User(user_name=form.user_name.data,
                               email=form.email.data,
                               password_hash=cryptedpwd,
                               wallet_pin='0',
                               profileimage='',
                               bannerimage='',
                               member_since=now,
                               admin=0,
                               admin_role=0,
                               bio='',
                               last_seen=now,
                               locked=0,
                               fails=0,
                               confirmed=0,
                               anon_id=theanonid,
                               anon_mode=0,
                               over_age=0,
                               agree_to_tos=True,
                               banned=0,
                               color_theme=3,
                               post_style=1)
                db.session.add(newuser)
                db.session.commit()

                # profile info
                userbio = UserPublicInfo(user_id=newuser.id,
                                         bio='',
                                         short_bio='')

                stats_for_bch = UserStatsBCH(
                    user_name=newuser.user_name,
                    user_id=newuser.id,
                    # given to posters/commenters
                    total_donated_to_postcomments_bch=0,
                    total_donated_to_postcomments_usd=0,
                    # recieved from posting
                    total_recievedfromposts_bch=0,
                    total_recievedfromposts_usd=0,
                    # recieved from comments
                    total_recievedfromcomments_bch=0,
                    total_recievedfromcomments_usd=0,
                    # given to charities
                    total_donated_to_cause_bch=0,
                    total_donated_to_cause_usd=0,
                )

                stats_for_user = UserStats(user_name=newuser.user_name,
                                           user_id=newuser.id,
                                           post_upvotes=0,
                                           post_downvotes=0,
                                           comment_upvotes=0,
                                           comment_downvotes=0,
                                           total_posts=0,
                                           total_comments=0,
                                           user_level=1,
                                           user_exp=0,
                                           user_width_next_level='0')

                users_timers = UserTimers(user_name=newuser.user_name,
                                          user_id=newuser.id,
                                          account_created=now,
                                          last_post=now,
                                          last_common_creation=now,
                                          last_comment=now,
                                          last_report=now)

                # add to db
                db.session.add(userbio)
                db.session.add(users_timers)
                db.session.add(stats_for_user)
                db.session.add(stats_for_bch)

                # commit
                db.session.commit()
                # make a user a directory
                getusernodelocation = userimagelocation(x=newuser.id)
                userfolderlocation = os.path.join(UPLOADED_FILES_DEST,
                                                  current_disk, 'user',
                                                  getusernodelocation,
                                                  str(newuser.id))
                mkdir_p(path=userfolderlocation)
                # login new user

                try:
                    # bitcoin cash
                    bch_create_wallet(user_id=newuser.id)
                except:
                    pass

                login_user(newuser)
                current_user.is_authenticated()
                current_user.is_active()

                flash(
                    "Successfully Registered."
                    "  If you want to access your wallet,"
                    " you will need to confirm your email.  If you used an invalid email,"
                    " you can change this in account settings.",
                    category="success")
                return redirect(url_for('welcome'))
            except Exception as e:

                return redirect((request.args.get('next', request.referrer)))

        else:
            for errors in form.user_name.errors:
                flash(errors, category="danger")
            for errors in form.password.errors:
                flash(errors, category="danger")
            for errors in form.passwordtwo.errors:
                flash(errors, category="danger")
            for errors in form.passwordtwo.errors:
                flash(errors, category="danger")

            return redirect((request.args.get('next', request.referrer)))
    return render_template('users/register.html', form=form)
def post_get_image(url, imagelocation, thepost):
    """
    gets the image when a url is given
    """
    try:
        theurl = str(url)
        filename = url.split('/')[-1]
        r = requests.get(theurl, allow_redirects=False)
        mkdir_p(imagelocation + '/')
        thelocationandimage = (os.path.join(imagelocation + '/' + filename))
        thebasename = str(random_image_name())
        open(thelocationandimage, 'wb').write(r.content)
        extension = os.path.splitext(thelocationandimage)[1]
        newname_600 = thebasename + "_600"

        if extension == '.gif':
            non_renamed_file = (os.path.join(thebasename + extension))
            location_of_large_file = os.path.join(imagelocation,
                                                  non_renamed_file)
            location_of_large_file = location_of_large_file[5:]
            thepost.url_image_server = location_of_large_file

        else:
            img = Image.open(thelocationandimage)

            if extension not in ext:
                img = img.convert('RGB')
                newextension = '.jpg'
                non_renamed_file = (os.path.join(thebasename + newextension))
                renamed_file_large = (os.path.join(newname_600 + newextension))
            else:
                non_renamed_file = (os.path.join(thebasename + extension))
                renamed_file_large = (os.path.join(newname_600 + extension))

            # save file
            imagesave = os.path.join(imagelocation, non_renamed_file)
            img.save(imagesave, subsampling=0, quality=95, optimize=True)
            os.chmod(imagesave, 0o775)

            imagewidth = int(img.size[0])
            if int(imagewidth) > int(551):
                # CONVERT
                # LARGE IMAGE

                # convert and save large image
                wpercent = (basewidth_600 / float(img.size[0]))
                hsize = int((float(img.size[1]) * float(wpercent)))
                img = img.resize((basewidth_600, hsize), Image.ANTIALIAS)
                imagesave = os.path.join(imagelocation, renamed_file_large)
                img.save(imagesave, subsampling=0, quality=95, optimize=True)
                os.chmod(imagesave, 0o775)

                # large file - get name and set for database
                location_of_large_file = os.path.join(imagelocation,
                                                      non_renamed_file)
                location_of_large_file = location_of_large_file[5:]

                if location_of_large_file is None:
                    thepost.url_image_server = ''
                else:
                    thepost.url_image_server = location_of_large_file

                if os.path.isfile(thelocationandimage):
                    os.remove(thelocationandimage)
            else:
                # SMALL FILE
                # DONT CONVERT
                # large file - get name and set for database
                location_of_large_file = os.path.join(imagelocation,
                                                      non_renamed_file)
                location_of_large_file = location_of_large_file[5:]

                if location_of_large_file is None:
                    thepost.url_image_server = ''
                else:
                    thepost.url_image_server = location_of_large_file
    except:
        thepost.url_image_server = ''

    db.session.add(thepost)
Exemplo n.º 6
0
def create_post_room_all(userid):

    now = datetime.utcnow()
    id_pic1 = id_generator_picture1()
    wall_post_form = MainPostForm()
    uniqueid = id_generator(size=15)

    if request.method == 'POST':

        usersubforums = db.session \
            .query(Subscribed) \
            .join(SubForums, (Subscribed.subcommon_id == SubForums.id)) \
            .filter(current_user.id == Subscribed.user_id) \
            .all()

        wall_post_form.roomname.choices = [(str(row.subscriber.id),
                                            str(row.subscriber.subcommon_name))
                                           for row in usersubforums]

        theuser = User.query \
            .filter(User.id == userid) \
            .first()
        getuser_timers = db.session \
            .query(UserTimers) \
            .filter_by(user_id=current_user.id) \
            .first()
        user_stats = db.session \
            .query(UserStats) \
            .filter(UserStats.user_id == current_user.id) \
            .first()

        if wall_post_form.validate_on_submit():

            # see if user posted comment
            if wall_post_form.post_message.data == '':
                score_1 = 0
            else:
                score_1 = 1
            if wall_post_form.image_one.data:
                score_2 = 1
            else:
                score_2 = 0
            total_score = score_1 + score_2

            if total_score == 0:
                flash("You need to post some content", category="success")
                return redirect((request.args.get('next', request.referrer)))

            getmatchingsubcommon = db.session \
                .query(SubForums) \
                .filter(SubForums.id == int(wall_post_form.roomname.data)) \
                .first()

            chosen_subcommon_id = getmatchingsubcommon.id
            chosen_subcommon_name = getmatchingsubcommon.subcommon_name

            newpostnumber = user_stats.total_posts + 1

            urlfound, urltitlefound, urldescriptionfound, urlimagefound = \
                geturl(wall_post_form.post_message.data)
            transformed_text, notifyuser = \
                transform_image_links_markdown(str(wall_post_form.post_message.data))
            # add post to db
            newpost = CommonsPost(
                title=wall_post_form.post_title.data,
                user_id=theuser.id,
                user_name=theuser.user_name,

                # location
                realid=uniqueid,
                subcommon_id=chosen_subcommon_id,
                subcommon_name=chosen_subcommon_name,
                type_of_post=0,

                # text
                post_text=transformed_text,

                # url
                url_of_post=urlfound,
                url_description=urldescriptionfound,
                url_image=urlimagefound,
                url_title=urltitlefound,
                url_image_server='',

                # images
                image_server_1='',

                # stats
                total_exp_commons=0,
                highest_exp_reached=0,
                upvotes_on_post=0,
                comment_count=0,
                vote_timestamp=now,
                last_active=now,
                hotness_rating_now=0,
                page_views=0,

                # admin
                sticky=0,
                active=1,
                locked=0,
                hidden=0,
                muted=0,
                created=now,
                edited=now,
            )

            getuser_timers.last_post = now
            user_stats.total_posts = newpostnumber

            db.session.add(user_stats)
            db.session.add(getuser_timers)
            db.session.add(newpost)
            db.session.commit()

            getusernodelocation = postnodelocation(x=newpost.id)
            postlocation = os.path.join(UPLOADED_FILES_DEST, current_disk,
                                        "post", getusernodelocation,
                                        str(newpost.id))

            # image from url
            if urlfound:
                post_get_image(url=newpost.url_image,
                               imagelocation=postlocation,
                               thepost=newpost)

            # image From User
            if wall_post_form.image_one.data:
                mkdir_p(path=postlocation)
                filename = secure_filename(
                    wall_post_form.image_one.data.filename)
                postimagefilepath = os.path.join(postlocation, filename)
                wall_post_form.image_one.data.save(postimagefilepath)
                # split file name and ending
                filenamenew, file_extension = os.path.splitext(
                    postimagefilepath)
                # gets new 64 digit filename
                newfilename = id_pic1 + file_extension
                # puts new name with ending
                filenamenewfull = filenamenew + file_extension
                # gets aboslute path of new file
                newfilenamedestination = os.path.join(postlocation,
                                                      newfilename)
                # renames file
                os.rename(filenamenewfull, newfilenamedestination)
                post_convert_image(imagelocation=postlocation,
                                   imagename=newfilename,
                                   thepost=newpost)

            if wall_post_form.image_one.data \
                    or (urlimagefound is not None and urlfound is not None):
                db.session.commit()

            if notifyuser is not None:
                # add info
                add_new_notification(user_id=notifyuser.id,
                                     subid=newpost.subcommon_id,
                                     subname=newpost.subcommon_name,
                                     postid=newpost.id,
                                     commentid=0,
                                     msg=32)

            # add exp points
            exppoint(user_id=current_user.id, category=1)

            return redirect((url_for('subforum.viewpost',
                                     subname=newpost.subcommon_name,
                                     postid=newpost.id)))
        else:

            flash("Post Creation Failure.", category="danger")
            for errors in wall_post_form.roomname.errors:
                flash(errors, category="danger")
            for errors in wall_post_form.post_message.errors:
                flash(
                    "Message Failure...a message is required and 3- 5000 characters",
                    category="danger")
                flash(errors, category="danger")
            for errors in wall_post_form.image_one.errors:
                flash(errors, category="danger")
            return redirect((request.args.get('next', request.referrer)))
Exemplo n.º 7
0
def createcomment(subname, postid):
    """
    View / Reply to a post
    """
    comment_form = CreateCommentForm()
    uniqueid = id_generator(size=15)
    id_pic1 = id_generator_picture1()
    now = datetime.utcnow()
    currentbchprice = BchPrices.query.get(1)

    the_post = db.session \
        .query(CommonsPost) \
        .filter(CommonsPost.id == postid) \
        .first()
    getmatchingsubcommon = db.session \
        .query(SubForums) \
        .filter(SubForums.subcommon_name == subname) \
        .first()
    chosen_subcommon_id = getmatchingsubcommon.id
    chosen_subcommon_name = getmatchingsubcommon.subcommon_name

    if request.method == 'GET':
        return render_template(
            'comments/_comment.html',
            comment_form=comment_form,
            currentbchprice=currentbchprice,
        )

    if request.method == 'POST':

        # see if user posted comment
        if comment_form.post_message.data == '':
            score_1 = 0
        else:
            score_1 = 1
        if comment_form.image_one.data:
            score_2 = 1
        else:
            score_2 = 0
        total_score = score_1 + score_2

        if total_score == 0:
            flash("You need to post some content", category="success")
            return redirect((request.args.get('next', request.referrer)))

        urlfound, urltitlefound, urldescriptionfound, urlimagefound = \
            geturl(comment_form.post_message.data)
        transformed_text, notifyuser = \
            transform_image_links_markdown(str(comment_form.post_message.data))

        new_reply_to_question = Comments(
            realid=uniqueid,
            # the id's
            commons_post_id=chosen_subcommon_id,
            subcommon_id=chosen_subcommon_name,
            created=now,
            # if answer is a right one
            accepted_answer=0,
            # user_name
            user_name=current_user.user_name,
            user_id=current_user.id,
            # stats
            total_exp_commons=0,
            upvotes_on_comment=0,
            downvotes_on_comment=0,
            # reported
            active=1,
            hidden=0,
            deleted=0,
            # donations
            total_recieved_bch=0,
            total_recieved_bch_usd=0,
            # the content
            url_image=urlimagefound,
            url_title=urltitlefound,
            url_of_post=urlfound,
            url_description=urldescriptionfound,
            url_image_server='',
            image_server_1='',
            body=transformed_text,
        )

        db.session.add(new_reply_to_question)

        db.session.commit()

        getusernodelocation = postnodelocation(x=the_post.id)
        comment_image_location = os.path.join(UPLOADED_FILES_DEST,
                                              current_disk, "post",
                                              getusernodelocation,
                                              str(the_post.id))

        # image from url
        if urlfound:
            comment_get_image(url=new_reply_to_question.url_image,
                              imagelocation=comment_image_location,
                              the_comment=new_reply_to_question)

        # image From User
        if comment_form.image_one.data:
            mkdir_p(path=comment_image_location)
            filename = secure_filename(comment_form.image_one.data.filename)
            postimagefilepath = os.path.join(comment_image_location, filename)
            comment_form.image_one.data.save(postimagefilepath)
            # split file name and ending
            filenamenew, file_extension = os.path.splitext(postimagefilepath)
            # gets new 64 digit filename
            newfilename = id_pic1 + file_extension
            # puts new name with ending
            filenamenewfull = filenamenew + file_extension
            # gets aboslute path of new file
            newfilenamedestination = os.path.join(comment_image_location,
                                                  newfilename)
            # renames file
            os.rename(filenamenewfull, newfilenamedestination)
            # convert image to right size and ending
            comment_convert_image(imagelocation=comment_image_location,
                                  imagename=newfilename,
                                  the_comment=new_reply_to_question)

        if comment_form.image_one.data \
                or (urlimagefound is not None and urlfound is not None):
            db.session.commit()
Exemplo n.º 8
0
def bannerpicture(business_id):

    id_pic1 = id_generator_picture1()

    form = BannerPicForm(
        CombinedMultiDict((request.files, request.form)),
    )
    thebiz = db.session.query(Business).filter(Business.id == business_id).first()
    if current_user.id != thebiz.user_id:
        return redirect(url_for('index'))
    if request.method == 'POST':
        if current_user.id != thebiz.user_id:
            return redirect(url_for('index'))
        if form.delete.data and form.validate_on_submit():
            deletebannerimage(business_id=thebiz.id)
            return redirect((request.args.get('next', request.referrer)))

        if form.submit.data and form.validate_on_submit():
            getusernodelocation = userimagelocation(x=thebiz.id)
            userlocation = os.path.join(UPLOADED_FILES_DEST, current_disk, 'business', getusernodelocation, str(thebiz.id))

            if form.imageprofile.data:
                try:
                    # make a user a directory
                    mkdir_p(path=userlocation)
                    deletebannerimage(business_id=thebiz.id)
                    filename = secure_filename(form.imageprofile.data.filename)
                    # makes directory (generic location + auction number id as folder)
                    profileimagefilepath = os.path.join(userlocation, filename)
                    # saves it to location
                    form.imageprofile.data.save(profileimagefilepath)
                    # RENAMING FILE
                    # split file name and ending
                    filenamenew, file_extension = os.path.splitext(profileimagefilepath)
                    # gets new 64 digit filename
                    newfilename = id_pic1 + file_extension
                    # puts new name with ending
                    filenamenewfull = filenamenew + file_extension
                    # gets aboslute path of new file
                    newfilenamedestination = os.path.join(userlocation, newfilename)
                    # renames file
                    os.rename(filenamenewfull, newfilenamedestination)
                    convert_banner_image(imagelocation=userlocation, imagename=newfilename, business_id=thebiz.id)

                except Exception as e:
                    thebiz.bannerimage = ""
                    flash("Error", category="success")
                    return redirect((request.args.get('next', request.referrer)))

                if not form.imageprofile.data.filename:
                    thebiz.bannerimage = ''

                db.session.add(thebiz)
                db.session.commit()
                flash("Profile banner has been changed", category="success")
                return redirect((request.args.get('next', request.referrer)))
        else:
            flash("Error. No data submitted", category="danger")
            return redirect((request.args.get('next', request.referrer)))

    return render_template('business/edit/images/profile_banner.html',
                           form=form,
                           thebiz=thebiz,
                           )