๐Ÿ‘ฉ‍๐Ÿ’ป Learn programming

[๋‚˜๋งŒ์˜ ๋‹จ์–ด์žฅ] 7. ์ƒ์„ธํŽ˜์ด์ง€ - ์ €์žฅ & ์‚ญ์ œ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

๋ฐ๊ตฅ์ž‰ 2022. 5. 4. 14:20
๋ฐ˜์‘ํ˜•

1. ์ƒˆ ๋‹จ์–ด / ๊ธฐ์กด ๋‹จ์–ด ๊ตฌ๋ถ„

 

โœ… url ์˜ˆ์‹œ

localhost:5000/detail/hello?status_give=new ⇒ ๐Ÿ’พ ๋ฒ„ํŠผ

localhost:5000/detail/hello?status_give=old ⇒ ๐Ÿ—‘๏ธ๋ฒ„ํŠผ

status_receive = request.args.get("status_give", "new") 
# status_give ๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ status_receive ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ฐ›์Œ
# status_give ๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฃผ์ง€ ์•Š์„ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด, ๊ธฐ๋ณธ๊ฐ’์„ "new"๋กœ ์ฃผ๋„๋ก ํ•จ.
return render_template("detail.html", word=keyword, result=result, status=status_receive)
# detail.html์— status ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณด๋ƒ„
@app.route('/detail/')
def detail(keyword):
    # API์—์„œ ๋‹จ์–ด ๋œป ์ฐพ์•„์„œ ๊ฒฐ๊ณผ ๋ณด๋‚ด๊ธฐ
    status_receive = request.args.get("status_give", "new")
    r = requests.get(f"<https://owlbot.info/api/v4/dictionary/{keyword}>",
                     headers={"Authorization": "Token [๋‚ด ํ† ํฐ]"})
    result = r.json()
    print(result)
    return render_template("detail.html", word=keyword, result=result, status=status_receive)
{% if status=="new" %} <!--status๊ฐ€ "new"์ด๋ฉด ์ €์žฅ ๋ฒ„ํŠผ, ํด๋ฆญํ•˜๋ฉด save_word() ํ•จ์ˆ˜ ํ˜ธ์ถœ-->
    <button id="btn-save" class="btn btn-outline-sparta btn-lg" onclick="save_word()">
        <i class="fa fa-floppy-o"></i>
    </button>
{% else %} <!--์•„๋‹ˆ๋ฉด ์‚ญ์ œ ๋ฒ„ํŠผ, ํด๋ฆญํ•˜๋ฉด delete_word() ํ•จ์ˆ˜ ํ˜ธ์ถœ-->
    <button id="btn-delete" class="btn btn-sparta btn-lg" onclick="delete_word()">
        <i class="fa fa-trash-o"></i>
    </button>
{% endif %}

 

2. ์ €์žฅ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

 

โœ… ์„œ๋ฒ„ - ๋‹จ์–ด ์ €์žฅ API

๋ชฉ๋ก ํŽ˜์ด์ง€์—์„œ๋Š” ๋‹จ์–ด ๋‹น ๋œป์„ ํ•˜๋‚˜๋งŒ ๋ณด์—ฌ์ฃผ๊ธฐ ๋•Œ๋ฌธ์—

๋‹จ์–ด์™€ ์ฒซ ๋ฒˆ์งธ ์ •์˜๋งŒ POST ์š”์ฒญ์œผ๋กœ ๋ณด๋‚ด๊ณ ,

์„œ๋ฒ„์—์„œ ๋‹จ์–ด์™€ ๋œป์„ ๋ฐ›์•„ words ์ปฌ๋ ‰์…˜์— ์ €์žฅ.

@app.route('/api/save_word', methods=['POST'])
def save_word(): # ๋‹จ์–ด ์ €์žฅ ํ•จ์ˆ˜
    word_receive = request.form['word_give'] 
    # ๋ฐ›์„ ๋‹จ์–ด๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ word_receive์— request์˜ form์˜ word_give๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณด๋ƒ„ 
    # ์ฆ‰, word_give๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์ค€ ๋ฐ์ดํ„ฐ๋ฅผ word_receive๋กœ ๋ฐ›์Œ
    definition_receive = request.form['definition_give']
    # ๋ฐ›์„ ๋œป์„ ์ €์žฅํ•  ๋ณ€์ˆ˜ definition_receive์— request์˜ form์˜ definition_give๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณด๋ƒ„ 
    # ์ฆ‰, definition_give๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์ค€ ๋ฐ์ดํ„ฐ๋ฅผ definition_receive๋กœ ๋ฐ›์Œ

    doc = {"word": word_receive, "definition": definition_receive}

    db.words.insert_one(doc)
    # db.์ปฌ๋ Œ์…˜ ์ด๋ฆ„.doc์ด๋ผ๋Š” ๋„ํ๋จผํŠธ ํ•˜๋‚˜๋ฅผ ๋ณด๋ƒ„

    return jsonify({'result': 'success', 'msg': f'word "{word_receive}" saved'})

 

โœ… ํด๋ผ์ด์–ธํŠธ - ๋‹จ์–ด ์ €์žฅ ์š”์ฒญ์„ ๋ณด๋‚ผ ํ•จ์ˆ˜

ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” save_word API๋กœ POST์š”์ฒญ์„ ๋ณด๋‚ด๋Š”๋ฐ,

ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ(word_give, definition_give)๋“ค์„ ๋ณด๋‚ด์„œ

๋‹จ์–ด ์ €์žฅ์—ฃ ์„ฑ๊ณต์„ ํ•˜๋ฉด ๋ฉ”์‹œ์ง€๋ฅผ alert๋กœ ๋„์šฐ๊ณ ,

delete ๋ฒ„ํŠผ์ด ์žˆ๋Š” status_give๊ฐ€ old์ธ ํŽ˜์ด์ง€๋กœ ๋ณ€๊ฒฝ.

function save_word() {
    $.ajax({
        type: "POST",
        url: `/api/save_word`, 
        data: {
            word_give: "{{ word }}",
            definition_give: "{{ result.definitions[0].definition }}"
        },
        success: function (response) {
          alert(response["msg"])
          window.location.href = "/detail/{{ word }}?status_give=old"
        }
    });
}

 

3. ์‚ญ์ œ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

 

โœ… ์„œ๋ฒ„ - ๋‹จ์–ด ์‚ญ์ œ API

๋‹จ์–ด๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋Š” ๋‹จ์–ด๋งŒ ์žˆ์œผ๋ฉด ๋˜๋ฏ€๋กœ POST ์š”์ฒญ์œผ๋กœ ๋‹จ์–ด๋ฅผ ๋ณด๋‚ด์ฃผ๊ณ ,

์„œ๋ฒ„์—์„œ๋Š” ํ•ด๋‹น ๋‹จ์–ด๋ฅผ ์ฐพ์•„ ์‚ญ์ œํ•ด์คŒ.

@app.route('/api/delete_word', methods=['POST'])
def delete_word(): # ๋‹จ์–ด ์‚ญ์ œ ํ•จ์ˆ˜
    word_receive = request.form['word_give']
		# word_receive๋กœ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ด์ค€ ๋ฐ์ดํ„ฐ๋ฅผ word_give๋กœ ๋ฐ›์Œ
    db.words.delete_one({"word": word_receive})
		#db์˜ words์—์„œ word_receive์— ํ•ด๋‹นํ•˜๋Š” word ํ•˜๋‚˜๋ฅผ ์ง€์›€
    db.examples.delete_many({"word": word_receive})
    return jsonify({'result': 'success', 'msg': f'word "{word_receive}" deleted'})

 

โœ… ํด๋ผ์ด์–ธํŠธ - ๋‹จ์–ด ์‚ญ์ œ ์š”์ฒญ์„ ๋ณด๋‚ผ ํ•จ์ˆ˜

delete_word API๋กœ POST์š”์ฒญ์„ ๋ณด๋‚ด๋Š”๋ฐ, ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ(word)๋“ค์„ ๋ณด๋ƒ„.

๋‹จ์–ด ์‚ญ์ œ์— ์„ฑ๊ณตํ•˜๋ฉด ๋”์ด์ƒ ๋ณด์—ฌ์ค„ ์ •๋ณด๊ฐ€ ์—†์œผ๋ฏ€๋กœ

๋ฉ”์‹œ์ง€๋ฅผ alert๋กœ ๋„์šฐ๊ณ , ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™

function delete_word() {
    $.ajax({
        type: "POST",
        url: `/api/delete_word`,
        data: {
            word_give: "{{ word }}
        },
        success: function (response) {
            alert(response["msg"])
            window.location.href = "/"
        }
    });
}

 

 

 

๋ฐ˜์‘ํ˜•