상세 컨텐츠

본문 제목

TO DO LIST 완성

카테고리 없음

by bumychoi 2023. 5. 11. 20:15

본문

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>나의 TO-DO LIST</title>
    <script src="https://code.jquery.com/jquery-3.6.4.min.js" integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8=" crossorigin="anonymous"></script>
    <style>
        html, body, h1,h2,h3,div,span,a,button,input{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body{
            background-color: goldenrod;
        }
        .wrap{
            background-color: aquamarine;
            width:350px ;
            height: 700px;
            margin: auto;
        }
        .flex-row{
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: center;

        }
        .flex-col{
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;

        }
        .wrap-todo{
            padding: 16px;
        }
        h1{
            font-size: 18px;
            margin: 16px auto 20px 0px;

        }
        .todo{
            padding: 16px;
            background-color: aliceblue;
            border-radius: 8px;
            width:300px;
            height: 50px ;
            margin-bottom: 8px;
            cursor: pointer;
        }
        .todo-content{
            margin-right: auto;
            margin-left: 8px;
            /* display: none; */
           
        }
        .todo>input{
            zoom: 1.5;
            cursor: pointer;
            background-color: transparent;
        }
        .add-form{
            padding: 16px;
            background-color: #fff6f8;
            border:1px solid red;
            border-radius: 8px;
            width:300px;
            height: 50px ;
            display: none;
        }
        .add-form-btns{
            margin-left: auto;
        }
        .add-form-btns>button{
            margin: 0pc 4px;
            background-color: transparent;
            border: none;
            cursor: pointer;
            font-size: 12px;
            font-weight: 600;
        }
        .add-form>input{
            background-color: transparent;
            border: none;
            font-size: 16px;
            width: 200px;
            cursor: pointer;
        }
        .add-form>input:focus-visible{
            outline:none;
        }
        .add-btn{
            padding: 16px;
            background-color: #fff6f8;
            border:1px dashed gold;
            border-radius: 8px;
            width:300px;
            height: 50px ;
            cursor: pointer;
        }
        .add-btn>span{
            margin-bottom: 5px;
           
        }
        .add-btn>button{
            margin-left: 8px;
            margin-right: auto;
            background-color: transparent;
            border: none;
            cursor: pointer;
            font-size: 12px;
            font-weight: 600;
        }
        .todo>button{
            border: none;
            background: transparent;
            cursor: pointer;
            font-weight: 600;
            font-size: 11px;
            color: red;
        }
        .todo-content-modify>input{
            border: none;
            margin-left: 8px;
            margin-right: auto;
            font-size: 16px;
            color: rebeccapurple;
            width: 160px;
        }
        .todo-content-modify>input:focus-visible{
            outline: none;
        }
        .todo-content-modify>button{
            margin: 0px 8px;
            font-weight: 600;
            background-color: transparent;
            cursor: pointer;
            border: none;
            font-size: 11px;
        }
        .calender{
            background-color: white;

        }
        .calender>h2{
            margin: 16px 0px;
            cursor: pointer;
        }
        .days{
            display: grid;
            grid-template-columns: repeat(7,1fr);
            grid-gap: 18px;
            margin-bottom: 20px;
        }
        .days>div{
            width: 30px;
            height: 30px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
        }
        .red{
            color: red;
            font-weight: 600;
        }
        .blue{
            color: blue;
            font-weight: 500;
        }
        .dates{
            display: grid;
            grid-template-columns: repeat(7,1fr);
            grid-gap: 18px;
            margin-bottom: 20px;
        }
        .dates>div{
            width: 30px;
            height: 30px;
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: center;

            cursor: pointer;
        }
        .dates>div.clicked{
            background-color: palevioletred;
            border-radius: 50%;
            color: cyan;
        }
    </style>
    <script>
        let date_clicked;

        $(document).ready(()=>{
           
            loadCalender();
            todayClicked();
        })
        function loadCalender(){
            let today= new Date();
            let year = today.getFullYear();
            let month = today.getMonth()+1;
             
            $('#this-month').text(`${year}${month}월`);

            let first_day = (new Date(year,month-1,1)).getDay()
            let last_day = (new Date(year,month,0)).getDate()
           
            for(let i=0; i<first_day;i++){
                let temp_html =`<div></div>`
                $('#dates').append(temp_html)
            }
            for(let i=0; i<last_day;i++){
                let id = `${year}-${month}-${i+1}`

                let target_day = (new Date(year,month-1,i+1)).getDay()
                let color =''
                if (target_day==0){
                    color='red'
                } else if (target_day==6){
                    color='blue'
                }

                let temp_html =`<div class="${color}" onclick="addClicked('${id}')" id=${id}>${i+1}</div>`
                $('#dates').append(temp_html)
            }
        }
        function addClicked(id){
            $('#dates>div').removeClass('clicked')
            $(`#${id}`).addClass('clicked')
            $('#today-todo').text(`${id}==>오늘의 할일`)
            date_clicked=id;

            listContent();
        }
        function todayClicked(){
            let today =new Date();
            let year = today.getFullYear();
            let month = today.getMonth()+1;
            let date =today.getDate();

            let id =`${year}-${month}-${date}`
            $(`#${id}`).trigger('click')
        }

        function showAddForm(){

            $('#add-form').css('display','flex');
            $("#add-btn").hide();
        }
        function hideAddForm(){
            if (confirm("정말취소하겠습니까??")) {
                $('#add-form').hide();
                $("#add-btn").css('display','flex');
            }
        }
        function addContent(){
            let content=$('#content').val();

            let formData = new FormData()
            formData.append("content_give", content)
            formData.append("date_give", date_clicked)


            fetch('/post', {method: "POST",body: formData}).then(res => res.json()).then(data => {
                alert(data["msg"])
                window.location.reload()
            })

        }
        function listContent(){
            let date = date_clicked;
            let url = `/list?date=${date}`
           
            fetch(url).then(res => res.json()).then(data => {
                let rows = data['result'];
                $('#todos').empty();
                rows.forEach((row)=>{
                    let content =row['content']
                    let _id = row['_id']
                    let check = row['check']

                    let txt_checked =''
                    let txt_style =''
                    if (check =="true"){
                                txt_checked='checked'
                                txt_style ='style="text-decoration:line-through"'}

                    let temp_html=`<div id="${_id}" class="todo  flex-row">
                                        <input ${txt_checked} onchange="checkContent('${_id}')" type="checkbox">
                                        <span ${txt_style} onclick="showUpdateForm('${_id}')" class="todo-content">${content}</span>
                                        <button onclick="deleteContent('${_id}')">삭제</button>
                                        <div style="display:none;" class="todo-content-modify flex-row">
                                            <input value="${content}" type="text">
                                            <button onclick="modifyContent('${_id}')">수정</button>
                                            <button onclick="hideUpdateForm('${_id}')">취소</button>
                                        </div>
                                    </div>`
                    $('#todos').append(temp_html);                
                })
            })
        }
        function showUpdateForm(_id){
            $(`#${_id}>span`).hide();
            $(`#${_id}>button`).hide();
            $(`#${_id}>input`).hide();
            $(`#${_id}>div`).css('display','flex');
        }
        function hideUpdateForm(_id){
            $(`#${_id}>span`).show();
            $(`#${_id}>button`).show();
            $(`#${_id}>input`).show();
            $(`#${_id}>div`).css('display','none');
            $(`#${_id}>span`).show();
        }
        function modifyContent(_id){
            let new_content =$(`#${_id} > div >input`).val();
            let formData = new FormData()
            formData.append("id_give",_id)
            formData.append("content_give", new_content)

            fetch('/modify', {method: "POST",body: formData}).then(res => res.json()).then(data => {
                alert(data["msg"])
                window.location.reload()
            })
        }
        function deleteContent(_id){
           
            let formData = new FormData()
            formData.append("id_give",_id)

            fetch('/delete', {method: "POST",body: formData}).then(res => res.json()).then(data => {
                alert(data["msg"])
                window.location.reload()
            })
        }
        function checkContent(_id){
            let is_checked =$(`#${_id} > input`).is(":checked");
           
            let formData = new FormData()
            formData.append("id_give",_id)
            formData.append("check_give",is_checked)

            fetch('/check', {method: "POST",body: formData}).then(res => res.json()).then(data => {
                alert(data["msg"])
                window.location.reload()
            })
        }
    </script>
</head>
<body>
    <div class="wrap">
        <div class="calender flex-col">
            <h2 onclick="todayClicked()"  id="this-month">2023년 5월</h2>
            <div class="days">
                <div class="red"></div>
                <div></div>
                <div></div>
                <div></div>
                <div></div>
                <div></div>
                <div class="blue"></div>
            </div>
            <div id="dates" class="dates">
               
            </div>
        </div>
        <div class="wrap-todo flex-col" >
            <h1 id="today-todo">오늘의 할 일</h1>
            <div id="todos" class="todos flex-col">
               
            </div>
            <div id="add-form" class="add-form flex-row">
                <input id="content" type="text">
                <div class="add-form-btns flex-row">
                    <button onclick="addContent()">추가</button>
                    <button onclick="hideAddForm()">취소</button>
                </div>

            </div>

            <div id="add-btn" onclick="showAddForm()" class="add-btn flex-row">
                <span>+</span>
                <button>추가하기</button>
            </div>
        </div>

    </div>
</body>
</html>
from flask import Flask, render_template, request, jsonify

from bson.objectid import ObjectId

from pymongo import MongoClient
client = MongoClient("mongodb+srv://sparta:test@cluster0.wu64ps9.mongodb.net/?retryWrites=true&w=majority")
db = client.pirates_lv2

application = app = Flask(__name__)

@app.route('/')
def home():
    return render_template("index.html")

@app.route("/list",methods=["GET"])
def list_todo():
    date =request.args.get('date')

    contents = list(db.todos1.find({'date':date}))
    for content in contents:
        content['_id']=str(content['_id'])
    return jsonify({"result":contents})

@app.route('/post', methods=["POST"])
def post_todo():
    content_receive = request.form["content_give"]
    date_receive = request.form["date_give"]
    doc ={
        'content': content_receive,
        'check': 'false',
        'date':date_receive
    }
    db.todos1.insert_one(doc)
    return jsonify({"msg": "등록 완료!"})

@app.route('/modify', methods=["POST"])
def modify_todo():
    id_receive = request.form["id_give"]
    id_receive = ObjectId(id_receive)
    content_receive = request.form["content_give"]

    db.todos1.update_one({'_id':id_receive},{'$set':{'content':content_receive}})

    return jsonify({"msg": "수정 완료!"})

@app.route('/delete', methods=["POST"])
def delete_todo():
    id_receive = request.form["id_give"]
    id_receive = ObjectId(id_receive)

    db.todos1.delete_one({'_id':id_receive})
    return jsonify({"msg": "삭제 완료!"})

@app.route('/check', methods=["POST"])
def check_todo():
    id_receive = request.form["id_give"]
    id_receive = ObjectId(id_receive)
    check_receive = request.form["check_give"]

    db.todos1.update_one({'_id':id_receive},{'$set':{'check':check_receive}})

    return jsonify({"msg": "체크 완료!"})


if __name__ == "__main__":
    app.run()