Notice
Recent Posts
Recent Comments
Link
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

MyPrograming

인스타그램 클론코딩 ( 포스팅 업로드) 본문

Python/python 클론코딩

인스타그램 클론코딩 ( 포스팅 업로드)

SeongWon 2020. 4. 11. 13:12
반응형

1. 포스팅 업로드

<post_create.html>

<div class="main">
  <section class="card_section">
    <div class="card">
      <form action="{% url 'instagram:create' %}" method="POST" enctype="multipart/form-data" class="post_create_form">
      {% csrf_token %}
        <div class="form-group">
          {% comment %} <input type="textarea" class="form-control" id="content" name="content" placeholder="내용을 입력해주세요."> {% endcomment %}
          <textarea name="content" rows="10" cols="128">
          </textarea>
        </div>
        <!-- 해시태그 -->
        <div class="form-group">
          <textarea name="hashtag" rows="1" cols="133" placeholder="해시태그 입력..."></textarea>
        </div>

        <div class="input-group mb-3">
          <div class="input-group-prepend">
            <span class="input-group-text" id="inputGroupFileAddon01">Upload</span>
          </div>
          <div class="custom-file">
            <input type="file" class="custom-file-input" id="post_img" name="post_img" aria-describedby="inputGroupFileAddon01">
            <label class="custom-file-label" for="post_img">파일을 선택해주세요.</label>
          </div>
        </div>
        <button type="submit" class="btn btn-primary">글 올리기</button>
      </form>
    </div>
  </section>
</div>

 

 

<post_createView.py>

@login_required
def post_createView(request):
    if request.method == "GET":
        return render(request, 'post_create.html')

    if request.method =="POST":
        user = request.user
        cursor = connection.cursor()

        # POST 내용 INSERT
        content = request.POST.get('content')
        post_img = request.FILES.get('post_img')
        hashtag = request.POST.get('hashtag')
        post_img_url = fileUpload(user, post_img)

        split_hashtag = hashtag.split(' ')

        insert_sql = "INSERT INTO post(user_id, content, post_img_src)"
        insert_sql += " VALUES ((%s) ,(%s), (%s))"

        result = cursor.execute(insert_sql, (user.username, content, post_img_url,))
        post_id = cursor.lastrowid


        # 해시태그 INSERT
        for i in range(len(split_hashtag)):
            # 이미 등록된 해시태그인지 확인
            inserted_hashtag = "SELECT COUNT(*)"
            inserted_hashtag += " FROM hashtag"
            inserted_hashtag += " WHERE keyword = (%s)"

            inserted_hashtag_result = cursor.execute(inserted_hashtag, (split_hashtag[i],))
            inserted_hashtag_datas = cursor.fetchall()

            if inserted_hashtag_datas[0][0] == 0:
                if '#' in split_hashtag[i]:

                    hashtag_sql = "INSERT INTO hashtag(keyword)"
                    hashtag_sql += " VALUES (%s)"

                    hashtag_result = cursor.execute(hashtag_sql, (split_hashtag[i],))
                    hashtag_id = cursor.lastrowid


                    # POST 해시태그 테이블 INSERT
                    post_hashtag_sql = "INSERT INTO post_hashtag(post_id, hashtag_id)"
                    post_hashtag_sql += " VALUES ((%s), (%s))"

                    post_hashtag_result = cursor.execute(post_hashtag_sql, (post_id, hashtag_id,))

            else:
                if '#' in split_hashtag[i]:
                    # 이미 존재하는 해시태그 아이디 가져오기
                    present_hashtag_sql = "SELECT hashtag_id"
                    present_hashtag_sql += " FROM hashtag"
                    present_hashtag_sql += " WHERE keyword = (%s)"

                    present_hashtag_result = cursor.execute(present_hashtag_sql, (split_hashtag[i],))
                    present_hashtag_datas = cursor.fetchall()

                    # POST 해시태그 테이블 INSERT
                    post_hashtag_sql = "INSERT INTO post_hashtag(post_id, hashtag_id)"
                    post_hashtag_sql += " VALUES ((%s), (%s))"

                    post_hashtag_result = cursor.execute(post_hashtag_sql, (post_id, present_hashtag_datas[0][0],))

        connection.commit()
        connection.close()


        return redirect('instagram:list', user.username)

 

 

<포스팅 업로드 페이지>

 

 


 

2. 포스팅 리스트 페이지 (User 정보 페이지)

<업로드 후 포스팅 리스트 페이지 (유저 페이지)>

 

 

<post_listView.py>

def post_listView(request, list):
    user_id = request.user

    cursor = connection.cursor()

    strSql = "SELECT post_img_src, user_id, post_id"  #클릭된 유저의 포스팅
    strSql += " FROM post"
    strSql += " WHERE user_id = (%s)"
    strSql += " ORDER BY time DESC"

    result = cursor.execute(strSql,(list,))
    datas = cursor.fetchall()

    post_count = "SELECT COUNT(post_id)"   #클릭된 유저의 포스팅 개수
    post_count += " FROM post"
    post_count += " WHERE user_id = (%s)"

    post_count_result = cursor.execute(post_count, (list,))
    post_count_datas = cursor.fetchall()

    following_count = "SELECT COUNT(following_id)"  # 클릭된 유저의 팔로워 수
    following_count += " FROM following"
    following_count += " WHERE user_id = (%s)"

    following_count_result = cursor.execute(following_count, (list,))
    following_count_datas = cursor.fetchall()

    name_strSql = "SELECT username, profile_img_src, profile_msg"
    name_strSql += " FROM accounts_user"   #클릭된 유저의 프로필 정보
    name_strSql += " WHERE username = (%s)"

    name_result = cursor.execute(name_strSql,(list,))
    name_datas = cursor.fetchall()

    follow_button = "SELECT COUNT(*)" #팔로우 유뮤에 따른 버튼 보여주기
    follow_button += " FROM following"
    follow_button += " WHERE user_id = (%s) AND following_id = (%s)"

    follow_button_result = cursor.execute(follow_button, (user_id.username, list))
    follow_button_datas = cursor.fetchall()

    follower_count_sql = "SELECT COUNT(user_id)" #클릭된 유저의 팔로잉 수
    follower_count_sql += " FROM following"
    follower_count_sql += " WHERE following_id = (%s)"

    follower_count_result = cursor.execute(follower_count_sql, (list,))
    follower_count_datas = cursor.fetchall()

    following_list = "SELECT profile_msg, profile_img_src, following_id, user_id" #클릭된 유저의 팔로잉 유저 목록
    following_list += " FROM accounts_user"
    following_list += " LEFT OUTER JOIN following on accounts_user.username = following.following_id"
    following_list += " WHERE following.user_id = (%s)"

    following_list_result = cursor.execute(following_list, (list,))
    following_list_datas = cursor.fetchall()

    follower_list = "SELECT profile_msg, profile_img_src, user_id, following_id"  # 클릭된 유저의 팔로워 유저 목록
    follower_list += " FROM accounts_user"
    follower_list += " LEFT OUTER JOIN following on accounts_user.username = following.user_id"
    follower_list += " WHERE following.following_id = (%s)"

    follower_list_result = cursor.execute(follower_list, (list,))
    follower_list_datas = cursor.fetchall()

    following_list_button = "SELECT user_id, following_id"
    following_list_button += " FROM following"

    following_list_button_result = cursor.execute(following_list_button)
    following_list_button_datas = cursor.fetchall()

    user_name = {'user_id':name_datas[0][0],
                 'profile_img_src':name_datas[0][1],
                 'profile_msg':name_datas[0][2]}

    post_count_tuple = {'post_count':post_count_datas[0][0]}

    following_count_tuple = {'following_count': following_count_datas[0][0]}

    follow_button_tuple = {'follow_button': follow_button_datas[0][0]}

    follower_count_tuple = {'follower_count': follower_count_datas[0][0]}

    following_list_tuple = []
    for following_list_data in following_list_datas:
        follow_button = "SELECT COUNT(*)"
        follow_button += " FROM following"
        follow_button += " WHERE user_id = (%s) AND following_id = (%s)"

        follow_button_result = cursor.execute(follow_button, (user_id.username, following_list_data[2],))
        follow_button_datas = cursor.fetchall()

        if follow_button_datas[0][0] == 0:
            is_follow = 0
        else:
            is_follow = 1

        row = {'profile_msg' : following_list_data[0],
               'profile_img_src' : following_list_data[1],
               'following_id' : following_list_data[2],
               'user_id' : following_list_data[3],
               'is_follow' : is_follow
               }
        following_list_tuple.append(row)

    follower_list_tuple = []
    for follower_list_data in follower_list_datas:
        follow_button = "SELECT COUNT(*)"
        follow_button += " FROM following"
        follow_button += " WHERE user_id = (%s) AND following_id = (%s)"

        follow_button_result = cursor.execute(follow_button, (user_id.username, follower_list_data[2],))
        follow_button_datas = cursor.fetchall()

        if follow_button_datas[0][0] == 0:
            is_follow = 0
        else:
            is_follow = 1

        row = {'profile_msg' : follower_list_data[0],
               'profile_img_src' : follower_list_data[1],
               'user_id' : follower_list_data[2],
               'following_id' : follower_list_data[3],
               'is_follow' : is_follow}
        follower_list_tuple.append(row)

    list= []
    for data in datas:
        row = {'post_img_src': data[0],
               'user_id': data[1],
               'post_id': data[2]}
        list.append(row)


    # 콜렉션 리스트
    collection_list_sql = "SELECT post.post_id, post_img_src"
    collection_list_sql += " FROM post"
    collection_list_sql += " LEFT OUTER JOIN collection on collection.post_id = post.post_id"
    collection_list_sql += " WHERE collection.user_id = (%s)"
    collection_list_sql += " ORDER BY post.time DESC"

    collection_list_result = cursor.execute(collection_list_sql, (user_id.username,))
    collection_list_datas = cursor.fetchall()

    collection_list = []
    for data in collection_list_datas:
        row = {'post_id': data[0],
               'post_img_src': data[1]}
        collection_list.append(row)

    connection.commit()
    connection.close()

    return render(request, 'post_list.html',{'list':list, 'user_name': user_name, 'post_count_tuple': post_count_tuple,
                                             'following_count_tuple': following_count_tuple,
                                             'follow_button_tuple': follow_button_tuple, 'follower_count_tuple': follower_count_tuple,
                                             'following_list': following_list_tuple, 'follower_list': follower_list_tuple,
                                             'collection_list': collection_list})

 

3. 포스팅 디테일 페이지

<게시물을 클릭했을때 Detail 페이지로 rendering>

 

 

<post_detailView.py>

@login_required
def post_detailView(request, detail):
    user_id = request.user

    cursor = connection.cursor()

    detail_strSql = "SELECT user_id, post_img_src, content, time, post_id, profile_img_src"
    detail_strSql += " FROM post"
    detail_strSql += " LEFT OUTER JOIN accounts_user on accounts_user.username=post.user_id WHERE post_id = (%s)"

    restult = cursor.execute(detail_strSql, (detail,))
    datas = cursor.fetchall()

    profile_strSql = "SELECT instagram_2nd.accounts_user.profile_img_src"
    profile_strSql += " FROM instagram_2nd.accounts_user"
    profile_strSql += " WHERE instagram_2nd.accounts_user.username = (%s)"

    profile_result = cursor.execute(profile_strSql, (user_id,))
    profile_datas = cursor.fetchall()

    # 해당 포스트가 좋아요 되있는지
    is_like_sql = "SELECT COUNT(*)"
    is_like_sql += " FROM like_post"
    is_like_sql += " WHERE user_id = (%s) AND post_id = (%s)"

    is_like_result = cursor.execute(is_like_sql, (user_id.username, detail,))
    is_like_datas = cursor.fetchall()
    is_like = is_like_datas[0][0]

    # 해당 포스트가 콜렉션 되있는지
    is_collection_sql = "SELECT COUNT(*)"
    is_collection_sql += " FROM collection"
    is_collection_sql += " WHERE user_id = (%s) AND post_id = (%s)"

    is_collection_result = cursor.execute(is_collection_sql, (user_id.username, detail,))
    is_collection_datas = cursor.fetchall()
    is_collection = is_collection_datas[0][0]

    like_count = "SELECT COUNT(user_id)"
    like_count += " FROM like_post"
    like_count += " WHERE post_id = (%s)"

    like_count_result = cursor.execute(like_count, (detail,))
    like_count_datas = cursor.fetchall()

    # 해시태그 가져오기
    post_hashtag_sql = "SELECT keyword"
    post_hashtag_sql += " FROM hashtag"
    post_hashtag_sql += " LEFT OUTER JOIN post_hashtag on hashtag.hashtag_id = post_hashtag.hashtag_id"
    post_hashtag_sql += " WHERE post_id = (%s)"

    post_hashtag_sql_result = cursor.execute(post_hashtag_sql, (detail,))
    post_hashtag_sql_datas = cursor.fetchall()

    hashtag = []
    for i in range(len(post_hashtag_sql_datas)):
        hashtag.append(post_hashtag_sql_datas[i][0])

    connection.commit()
    connection.close()

    detail = {'user_id': datas[0][0],
              'post_img_src': datas[0][1],
              'content': datas[0][2],
              'time': datas[0][3],
              'post_id': datas[0][4],
              'profile_img_src': datas[0][5],
              'like_count': like_count_datas[0][0],
              'hashtag': hashtag}

    profile = {'profile_img_src': profile_datas[0][0]}


    return render(request, 'post_detail.html', {'detail':detail, 'profile':profile,
                                                'is_like': is_like, 'is_collection': is_collection, 'hashtag': hashtag})

 

 

반응형