code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -1,10 +1,178 @@ import React from 'react'; +import Nav from '../../../components/Nav/Nav'; +import Feed from '../../../components/Feed/Feed'; +import './Main.scss'; + class Main extends React.Component { + constructor() { + super(); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feedData.json', { + method: 'GET', + }) + .then(res => res.json()) + .then(data => { + this.setState({ + feeds: data, + }); + }); + } + render() { + const { feeds } = this.state; + + const STORIES = [ + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder1', + time: 43, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder2', + time: 3, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder3', + time: 23, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder4', + time: 27, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder5', + time: 13, + }, + ]; + + const Stories = STORIES.map(store => { + return ( + <div className="story"> + <img alt={store.alt} src={store.img} /> + <div> + <div className="boldFont">{store.name}</div> + <div className="lightFont">{store.time}๋ถ„ ์ „</div> + </div> + </div> + ); + }); return ( <div> - <h3>๋ฉ”์ธ</h3> + <Nav /> + <main> + <div className="content"> + <div> + {feeds.map(feed => { + return <Feed key={`feed${feed.id}`} data={feed} />; + })} + </div> + <div className="mainRight"> + <div className="rightTop"> + <img + className="wecodeLogo" + alt="์œ„์ฝ”๋“œ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">wecode_bootcamp</div> + <div className="lightFont">WeCode | ์œ„์ฝ”๋“œ</div> + </div> + </div> + <div className="rightMiddle"> + <div className="seeAllStory"> + <div className="lightFont">์Šคํ† ๋ฆฌ</div> + <div className="boldFont">๋ชจ๋‘ ๋ณด๊ธฐ</div> + </div> + <div className="seeStory">{Stories}</div> + </div> + <div className="rightMiddle"> + <div className="friendsRecommendContainer"> + <div className="lightFont">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</div> + <div className="boldFont">๋ชจ๋‘ ๋ณด๊ธฐ</div> + </div> + <div className="friendsRecommendList"> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">ํ™๊ธธ๋™</div> + <div className="lightFont">๋ธ”๋ผ๋ธ”๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์•„๋ฌด๊ฐœ</div> + <div className="lightFont">์‹œ๋Œ์‹œ๋Œ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์–ด์ฉŒ๊ตฌ</div> + <div className="lightFont">์–„๋ผ์–„๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์ €์ฉŒ๊ตฌ</div> + <div className="lightFont">์šœ๋กœ์šœ๋กœ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์ผ๋ผ์ผ๋ผ</div> + <div className="lightFont">๋ธ”๋ผ๋ธ”๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + </div> + </div> + <footer> + <div className="lightFont"> + Westagram ์ •๋ณด . ์ง€์› . ํ™๋ณด ์„ผํ„ฐ . API . ์ฑ„์šฉ์ •๋ณด . + ๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ . ์•ฝ๊ด€ . ๋””๋ ‰ํ„ฐ๋ฆฌ . ํ”„๋กœํ•„ . ํ•ด์‹œํƒœ๊ทธ . ์–ธ์–ด + </div> + <div className="lightFont boldFont">ยฉ 2021 WESTAGRAM</div> + </footer> + </div> + </div> + </main> </div> ); }
JavaScript
story ๋ถ€๋ถ„์ด ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณต๋˜๊ณ  ์žˆ๋„ค์š”! ์ƒ์ˆ˜ ๋ฐ์ดํ„ฐ์™€ map์„ ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณต๋œ ๋ถ€๋ถ„์„ ์ค„์—ฌ์ฃผ์„ธ์š”! ``` const STORIES = [ {img: "//", name: "wecorder1"}, {img: "//", name: "wecorder2"}, {img: "//", name: "wecorder3"} ]; STORIES.map(); ```
@@ -0,0 +1,285 @@ +@import 'https://fonts.googleapis.com/css2?family=Lobster&display=swap'; +@media screen and (max-width: 1200px) { + .mainRight { + display: none; + } + + .article { + width: 100%; + } +} + +body { + margin: 0; + padding: 0; + border-collapse: collapse; +} + +article { + width: 800px; + background-color: white; +} + +nav { + display: flex; + justify-content: space-around; + align-items: center; + margin: 0px 170px; + height: 60px; +} + +.logo { + width: 30px; + height: 30px; + padding-right: 15px; + border-right: 1px solid black; +} + +.rightIcon { + width: 30px; + height: 30px; + margin-right: 30px; +} + +.instagram { + margin-left: 15px; + font-family: 'Lobster', cursive; + font-size: 33px; + font-weight: 1; +} + +.search { + outline: none; +} + +.writeComment { + width: 100%; + height: 40px; + padding-left: 10px; + border: 1px solid #efefef; + outline: none; +} + +.search-box { + > input { + width: 200px; + height: 20px; + border: 1px solid #dbdbdb; + text-align: center; + } + + position: relative; + > img { + position: absolute; + width: 11px; + height: 11px; + left: 75px; + top: 7px; + } +} + +main { + padding: 60px 0px; + background-color: #fafafa; +} + +.content { + display: flex; + justify-content: space-around; + flex-direction: row; + margin: 0px 350px; +} + +.mainRight { + width: 330px; + margin-left: 10px; +} + +.feedTop { + display: flex; + justify-content: space-between; + align-items: center; + width: 780px; + padding: 10px 10px; + border: 1px solid #e6e6e6; +} + +.feedUser { + display: flex; + align-items: center; +} + +.feedPicture { + height: 800px; + border: 1px solid #e6e6e6; +} + +.feedId { + margin-left: 10px; +} + +.feedMore { + width: 15px; + height: 15px; +} + +.smallUserPicture { + width: 40px; + height: 40px; + border: 1px solid #e6e6e6; + border-radius: 50px; +} + +.feedIcons { + > img { + width: 27px; + height: 27px; + } + + > div { + > img { + width: 27px; + height: 27px; + } + } + display: flex; + justify-content: space-between; + flex-direction: row; + margin: 0px; +} + +.feedBottom { + padding: 10px 10px; + border-left: 1px solid #efefef; + border-right: 1px solid #efefef; + border-bottom: 1px solid #efefef; +} + +.FeedBottomLeftIcon { + > img { + padding-right: 10px; + } +} + +.like { + > img { + width: 15px; + height: 15px; + margin-right: 5px; + border: 1px solid #e6e6e6; + border-radius: 7px; + } + display: flex; + align-items: center; +} + +.comment { + > div { + > div { + color: #a4a4a4; + } + } +} + +.commentIcons { + float: right; + display: inline; + width: 15px; + height: 15px; + margin-left: 5px; +} + +.boldFont { + font-weight: bolder; +} + +.addCommentFrom { + position: relative; + width: 786px; +} + +.postingButton { + position: absolute; + color: #cde6fc; + background-color: white; + top: 14px; + right: 10px; + border: 0; + outline: 0; +} + +.rightTop { + display: flex; + align-items: center; + width: 300px; +} + +.wecodeLogo { + width: 60px; + height: 60px; + margin-right: 20px; + border-radius: 50px; +} + +.lightFont { + color: #9e9e9e; +} + +.followFont { + color: #3797f0; +} + +.rightMiddle { + height: 200px; + margin: 20px 0px; + padding: 12px; + background: white; + border: 3px solid #e6e6e6; + border-radius: 7px; + overflow: scroll; +} + +.seeAllStory { + display: flex; + align-items: center; + justify-content: space-between; +} + +.friendsRecommendContainer { + display: flex; + align-items: center; + justify-content: space-between; +} + +.story { + display: flex; + align-items: center; + margin: 10px; + + > img { + width: 40px; + height: 40px; + margin-right: 10px; + border-radius: 50px; + border: 2px solid #d93081; + } +} + +.friendsRecommend { + display: flex; + align-items: center; + margin: 10px; + > img { + width: 40px; + height: 40px; + border-radius: 50px; + margin-right: 10px; + } +} + +footer { + margin-top: 20px; + > .boldFont { + margin-top: 10px; + } +}
Unknown
๋ฐ˜์‘ํ˜• ์•„์ฃผ ์ข‹๋„ค์š”!! ๋ธŒ๋ ˆ์ดํฌ ํฌ์ธํŠธ๋Š” ๊ธฐํš๋งˆ๋‹ค ๋‹ฌ๋ผ์ง€์ง€๋งŒ ๋ณดํ†ต ํƒœ๋ธ”๋ฆฟ๊ณผ ๋ฐ์Šคํฌํƒ‘์˜ ๊ตฌ๋ถ„์ ์€ 1078px์ด๋ผ๋Š” ์  ์•Œ์•„๋‘์„ธ์š”! ๋ชจ๋ฐ”์ผ๊ณผ ํƒœ๋ธ”๋ฆฟ์€ 768px์ž…๋‹ˆ๋‹ค!
@@ -0,0 +1,285 @@ +@import 'https://fonts.googleapis.com/css2?family=Lobster&display=swap'; +@media screen and (max-width: 1200px) { + .mainRight { + display: none; + } + + .article { + width: 100%; + } +} + +body { + margin: 0; + padding: 0; + border-collapse: collapse; +} + +article { + width: 800px; + background-color: white; +} + +nav { + display: flex; + justify-content: space-around; + align-items: center; + margin: 0px 170px; + height: 60px; +} + +.logo { + width: 30px; + height: 30px; + padding-right: 15px; + border-right: 1px solid black; +} + +.rightIcon { + width: 30px; + height: 30px; + margin-right: 30px; +} + +.instagram { + margin-left: 15px; + font-family: 'Lobster', cursive; + font-size: 33px; + font-weight: 1; +} + +.search { + outline: none; +} + +.writeComment { + width: 100%; + height: 40px; + padding-left: 10px; + border: 1px solid #efefef; + outline: none; +} + +.search-box { + > input { + width: 200px; + height: 20px; + border: 1px solid #dbdbdb; + text-align: center; + } + + position: relative; + > img { + position: absolute; + width: 11px; + height: 11px; + left: 75px; + top: 7px; + } +} + +main { + padding: 60px 0px; + background-color: #fafafa; +} + +.content { + display: flex; + justify-content: space-around; + flex-direction: row; + margin: 0px 350px; +} + +.mainRight { + width: 330px; + margin-left: 10px; +} + +.feedTop { + display: flex; + justify-content: space-between; + align-items: center; + width: 780px; + padding: 10px 10px; + border: 1px solid #e6e6e6; +} + +.feedUser { + display: flex; + align-items: center; +} + +.feedPicture { + height: 800px; + border: 1px solid #e6e6e6; +} + +.feedId { + margin-left: 10px; +} + +.feedMore { + width: 15px; + height: 15px; +} + +.smallUserPicture { + width: 40px; + height: 40px; + border: 1px solid #e6e6e6; + border-radius: 50px; +} + +.feedIcons { + > img { + width: 27px; + height: 27px; + } + + > div { + > img { + width: 27px; + height: 27px; + } + } + display: flex; + justify-content: space-between; + flex-direction: row; + margin: 0px; +} + +.feedBottom { + padding: 10px 10px; + border-left: 1px solid #efefef; + border-right: 1px solid #efefef; + border-bottom: 1px solid #efefef; +} + +.FeedBottomLeftIcon { + > img { + padding-right: 10px; + } +} + +.like { + > img { + width: 15px; + height: 15px; + margin-right: 5px; + border: 1px solid #e6e6e6; + border-radius: 7px; + } + display: flex; + align-items: center; +} + +.comment { + > div { + > div { + color: #a4a4a4; + } + } +} + +.commentIcons { + float: right; + display: inline; + width: 15px; + height: 15px; + margin-left: 5px; +} + +.boldFont { + font-weight: bolder; +} + +.addCommentFrom { + position: relative; + width: 786px; +} + +.postingButton { + position: absolute; + color: #cde6fc; + background-color: white; + top: 14px; + right: 10px; + border: 0; + outline: 0; +} + +.rightTop { + display: flex; + align-items: center; + width: 300px; +} + +.wecodeLogo { + width: 60px; + height: 60px; + margin-right: 20px; + border-radius: 50px; +} + +.lightFont { + color: #9e9e9e; +} + +.followFont { + color: #3797f0; +} + +.rightMiddle { + height: 200px; + margin: 20px 0px; + padding: 12px; + background: white; + border: 3px solid #e6e6e6; + border-radius: 7px; + overflow: scroll; +} + +.seeAllStory { + display: flex; + align-items: center; + justify-content: space-between; +} + +.friendsRecommendContainer { + display: flex; + align-items: center; + justify-content: space-between; +} + +.story { + display: flex; + align-items: center; + margin: 10px; + + > img { + width: 40px; + height: 40px; + margin-right: 10px; + border-radius: 50px; + border: 2px solid #d93081; + } +} + +.friendsRecommend { + display: flex; + align-items: center; + margin: 10px; + > img { + width: 40px; + height: 40px; + border-radius: 50px; + margin-right: 10px; + } +} + +footer { + margin-top: 20px; + > .boldFont { + margin-top: 10px; + } +}
Unknown
๊ฐ™์€ ์Šคํƒ€์ผ์˜ ๊ฒฝ์šฐ์—๋Š” ์ค‘๋ณตํ•˜์—ฌ ์ฃผ์‹œ์ง€ ๋ง๊ณ  ๊ฐ™์€ ํด๋ž˜์Šค๋ช…์„ ์‚ฌ์šฉํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,125 @@ +import React from 'react'; +import Comment from '../Comment/Comment'; +import './Feed.scss'; + +class Feed extends React.Component { + constructor(props) { + super(props); + const { comment } = this.props.data; + this.state = { + comment: comment, + commentInputValue: '', + }; + } + + addComment = () => { + const { comment, commentInputValue } = this.state; + if (commentInputValue.length >= 1) { + const newComment = { + id: comment.length + 1, + content: commentInputValue, + isLiked: false, + }; + this.setState({ + comment: [...comment, newComment], + commentInputValue: '', + }); + } + }; + + deleteComment = e => { + const { comment } = this.state; + const deleteCommentId = Number(e.target.id); + const result = comment.filter(comment => comment.id !== deleteCommentId); + this.setState({ + comment: result, + }); + }; + + inputEnterPress = e => { + if (e.key === 'Enter') { + e.preventDefault(); + this.addComment(); + } + }; + + onChange = e => { + const { value } = e.target; + this.setState({ + commentInputValue: value, + }); + }; + + render() { + const { inputEnterPress, onChange, addComment, deleteComment } = this; + const { commentInputValue, comment } = this.state; + const { alt, img, userName } = this.props.data; + + return ( + <div className="feed"> + <article> + <div className="feedTop"> + <div className="feedUser"> + <img className="smallUserPicture" alt={alt} src={img} /> + <div className="feedId boldFont">{userName}</div> + </div> + <img + className="feedMore" + alt="๋”๋ณด๊ธฐ" + src="/images/youngho/more.png" + /> + </div> + <img className="feedPicture" alt="ํ”ผ๋“œ์‚ฌ์ง„" src={img} /> + <div className="feedBottom"> + <div className="feedIcons"> + <div className="FeedBottomLeftIcon"> + <img alt="๋นจ๊ฐ„์ƒ‰ํ•˜ํŠธ" src="/images/youngho/redHeart.png" /> + <img alt="๋ฉ”์„ธ์ง€" src="/images/youngho/speech-bubble.png" /> + <img alt="์—…๋กœ๋“œ" src="/images/youngho/upload.png" /> + </div> + <img alt="์ €์žฅ" src="/images/youngho/ribbon.png" /> + </div> + <div className="like"> + <img alt="์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅธ ์‚ฌ๋žŒ ์‚ฌ์ง„" src={img} /> + <span className="boldFont">manja</span>๋‹˜ + <span className="boldFont">์™ธ 7๋ช…</span>์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค + </div> + {comment.map(comment => { + return ( + <Comment + key={comment.id} + id={comment.id} + userName={comment.userName} + comment={comment.content} + isLiked={comment.isLiked} + deleteComment={deleteComment} + /> + ); + })} + </div> + <form className="addCommentFrom"> + <input + className="writeComment" + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onKeyPress={inputEnterPress} + onChange={onChange} + value={commentInputValue} + /> + <input + className={`postingButton ${ + commentInputValue.length ? 'activeButton' : '' + }`} + type="button" + defaultValue="๊ฒŒ์‹œ" + onClick={addComment} + disabled={commentInputValue.length ? false : true} + /> + </form> + </article> + </div> + ); + } +} + +export default Feed;
JavaScript
import ๊ตฌ๋ถ„์€ ๋„ˆ๋ฌด ์ข‹์ง€๋งŒ ์ค„๋ฐ”๊ฟˆ์€ ์•ˆํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค!!!
@@ -0,0 +1,125 @@ +import React from 'react'; +import Comment from '../Comment/Comment'; +import './Feed.scss'; + +class Feed extends React.Component { + constructor(props) { + super(props); + const { comment } = this.props.data; + this.state = { + comment: comment, + commentInputValue: '', + }; + } + + addComment = () => { + const { comment, commentInputValue } = this.state; + if (commentInputValue.length >= 1) { + const newComment = { + id: comment.length + 1, + content: commentInputValue, + isLiked: false, + }; + this.setState({ + comment: [...comment, newComment], + commentInputValue: '', + }); + } + }; + + deleteComment = e => { + const { comment } = this.state; + const deleteCommentId = Number(e.target.id); + const result = comment.filter(comment => comment.id !== deleteCommentId); + this.setState({ + comment: result, + }); + }; + + inputEnterPress = e => { + if (e.key === 'Enter') { + e.preventDefault(); + this.addComment(); + } + }; + + onChange = e => { + const { value } = e.target; + this.setState({ + commentInputValue: value, + }); + }; + + render() { + const { inputEnterPress, onChange, addComment, deleteComment } = this; + const { commentInputValue, comment } = this.state; + const { alt, img, userName } = this.props.data; + + return ( + <div className="feed"> + <article> + <div className="feedTop"> + <div className="feedUser"> + <img className="smallUserPicture" alt={alt} src={img} /> + <div className="feedId boldFont">{userName}</div> + </div> + <img + className="feedMore" + alt="๋”๋ณด๊ธฐ" + src="/images/youngho/more.png" + /> + </div> + <img className="feedPicture" alt="ํ”ผ๋“œ์‚ฌ์ง„" src={img} /> + <div className="feedBottom"> + <div className="feedIcons"> + <div className="FeedBottomLeftIcon"> + <img alt="๋นจ๊ฐ„์ƒ‰ํ•˜ํŠธ" src="/images/youngho/redHeart.png" /> + <img alt="๋ฉ”์„ธ์ง€" src="/images/youngho/speech-bubble.png" /> + <img alt="์—…๋กœ๋“œ" src="/images/youngho/upload.png" /> + </div> + <img alt="์ €์žฅ" src="/images/youngho/ribbon.png" /> + </div> + <div className="like"> + <img alt="์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅธ ์‚ฌ๋žŒ ์‚ฌ์ง„" src={img} /> + <span className="boldFont">manja</span>๋‹˜ + <span className="boldFont">์™ธ 7๋ช…</span>์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค + </div> + {comment.map(comment => { + return ( + <Comment + key={comment.id} + id={comment.id} + userName={comment.userName} + comment={comment.content} + isLiked={comment.isLiked} + deleteComment={deleteComment} + /> + ); + })} + </div> + <form className="addCommentFrom"> + <input + className="writeComment" + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onKeyPress={inputEnterPress} + onChange={onChange} + value={commentInputValue} + /> + <input + className={`postingButton ${ + commentInputValue.length ? 'activeButton' : '' + }`} + type="button" + defaultValue="๊ฒŒ์‹œ" + onClick={addComment} + disabled={commentInputValue.length ? false : true} + /> + </form> + </article> + </div> + ); + } +} + +export default Feed;
JavaScript
์ด๋ ‡๊ฒŒ ๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น ๋„ˆ๋ฌด ์ข‹๋„ค์š”!!
@@ -0,0 +1,74 @@ +[ + { + "id": 1, + "userName": "bbangho", + "alt": "wecode", + "img": "/images/youngho/hanRiver.jpeg", + "comment": [ + { + "id": 1, + "userName": "bbangho", + "content": "ํ•œ๊ฐ• ๋‹ค๋…€๊ฐ~ โœŒ๏ธ", + "isLiked": true + }, + { + "id": 2, + "userName": "manja", + "content": "ํ•œ๊ฐ• ๊ฐฑ~ ๐Ÿ”ซ ๐Ÿ”ซ", + "isLiked": true + } + ] + }, + { + "id": 2, + "userName": "joonsikyang", + "alt": "wecode", + "img": "/images/youngho/we.jpeg", + "comment": [ + { + "id": 1, + "userName": "joonsikyang", + "content": "Work hard ๐Ÿคฏ", + "isLiked": true + }, + { + "id": 2, + "userName": "bbangho", + "content": "Go for it ๐Ÿคฃ", + "isLiked": false + }, + { + "id": 3, + "userName": "manja", + "content": "๐Ÿ”ฅ๐Ÿ”ฅ", + "isLiked": false + } + ] + }, + { + "id": 3, + "userName": "jayPark", + "alt": "wecode", + "img": "/images/youngho/we.jpeg", + "comment": [ + { + "id": 1, + "userName": "jayPark", + "content": "Let's get it", + "isLiked": true + }, + { + "id": 2, + "userName": "bbanho", + "content": "Skrt~ ๐Ÿ˜Ž", + "isLiked": false + }, + { + "id": 3, + "userName": "gildong", + "content": "Yo~ ๐ŸŒž", + "isLiked": true + } + ] + } +]
Unknown
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -1,10 +1,94 @@ import React from 'react'; +import { Link } from 'react-router-dom'; + +import './Login.scss'; class Login extends React.Component { + constructor() { + super(); + this.state = { + loginId: '', + loginPassword: '', + }; + } + + handleLoginInput = e => { + const { value, name } = e.target; + this.setState({ + [name]: value, + }); + }; + + handleLogin = () => { + fetch('http://10.58.0.84:8000/users/login', { + method: 'POST', + body: JSON.stringify({ + email: this.state.loginId, + password: this.state.loginPassword, + }), + }) + .then(response => response.json()) + .then(result => console.log('๊ฒฐ๊ณผ: ', result)); + }; + + // handleLogin = () => { + // fetch('http://10.58.0.84:8000/users/signup', { + // method: 'POST', + // body: JSON.stringify({ + // name: '๊น€์˜ํ˜ธ', + // email: this.state.loginId, + // password: this.state.loginPassword, + // phone_number: '01012345678', + // date_of_birth: '1995-07-07', + // gender: 'M', + // address: 'Seoul', + // }), + // }) + // .then(response => response.json()) + // .then(result => console.log('๊ฒฐ๊ณผ: ', result)); + // }; + render() { + const { checkIdPassword, handleLoginInput } = this; + const { loginId, loginPassword } = this.state; + + const condition = + loginId.length >= 5 && loginPassword.length >= 5 && loginId.includes('@'); + return ( <div> - <h3>๋กœ๊ทธ์ธ</h3> + <div className="container"> + <header className="header">Westagram</header> + <form className="loginForm" onKeyUp={checkIdPassword}> + <input + id="id" + name="loginId" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={handleLoginInput} + /> + <input + id="password" + name="loginPassword" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={handleLoginInput} + /> + <Link to="/main"> + <button + onClick={this.handleLogin} + className={condition ? 'buttonActivate' : 'buttonDisabled'} + id="loginButton" + disabled={condition ? false : true} + > + ๋กœ๊ทธ์ธ + </button> + </Link> + </form> + <a href="#!" className="findPassword"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </a> + </div> </div> ); }
JavaScript
๋ฐ˜์˜ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,78 @@ +@import 'https://fonts.googleapis.com/css2?family=Lobster&display=swap'; +body { + background-color: #fafafa; +} + +.container { + text-align: center; + width: 600px; + height: 600px; + margin: 50px auto 0 auto; + border: 1px solid #e6e6e6; + background-color: white; +} + +.header { + padding: 30px; + font-family: 'Lobster', cursive; + font-size: 70px; + font-weight: 1px; +} + +.loginForm { + width: 425px; + margin: 0 auto; +} + +#id { + display: block; + width: 100%; + margin: 20px 0px auto; + padding: 15px; + border: 1px solid #efefef; + background-color: #fafafa; + border-radius: 5px; + font-size: 20px; +} + +#password { + display: block; + width: 100%; + margin: 20px 0px auto; + padding: 15px; + border: 1px solid #efefef; + background-color: #fafafa; + border-radius: 5px; + font-size: 20px; +} + +#loginButton { + display: block; + width: 459px; + margin: 20px 0px auto; + padding: 15px; + border: 0px; + border-radius: 5px; + color: white; + font-size: 20px; +} + +.findPassword { + display: block; + height: 50px; + margin-top: 180px; + font-size: 18px; + text-decoration: none; +} + +.buttonDisabled { + background-color: #c5e1fc; +} + +.buttonActivate { + background-color: #0095f6; +} + +a { + text-decoration: none; +}
Unknown
๋ฐ˜์˜ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,78 @@ +@import 'https://fonts.googleapis.com/css2?family=Lobster&display=swap'; +body { + background-color: #fafafa; +} + +.container { + text-align: center; + width: 600px; + height: 600px; + margin: 50px auto 0 auto; + border: 1px solid #e6e6e6; + background-color: white; +} + +.header { + padding: 30px; + font-family: 'Lobster', cursive; + font-size: 70px; + font-weight: 1px; +} + +.loginForm { + width: 425px; + margin: 0 auto; +} + +#id { + display: block; + width: 100%; + margin: 20px 0px auto; + padding: 15px; + border: 1px solid #efefef; + background-color: #fafafa; + border-radius: 5px; + font-size: 20px; +} + +#password { + display: block; + width: 100%; + margin: 20px 0px auto; + padding: 15px; + border: 1px solid #efefef; + background-color: #fafafa; + border-radius: 5px; + font-size: 20px; +} + +#loginButton { + display: block; + width: 459px; + margin: 20px 0px auto; + padding: 15px; + border: 0px; + border-radius: 5px; + color: white; + font-size: 20px; +} + +.findPassword { + display: block; + height: 50px; + margin-top: 180px; + font-size: 18px; + text-decoration: none; +} + +.buttonDisabled { + background-color: #c5e1fc; +} + +.buttonActivate { + background-color: #0095f6; +} + +a { + text-decoration: none; +}
Unknown
๋ฐ˜์˜ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -1,10 +1,178 @@ import React from 'react'; +import Nav from '../../../components/Nav/Nav'; +import Feed from '../../../components/Feed/Feed'; +import './Main.scss'; + class Main extends React.Component { + constructor() { + super(); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feedData.json', { + method: 'GET', + }) + .then(res => res.json()) + .then(data => { + this.setState({ + feeds: data, + }); + }); + } + render() { + const { feeds } = this.state; + + const STORIES = [ + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder1', + time: 43, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder2', + time: 3, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder3', + time: 23, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder4', + time: 27, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder5', + time: 13, + }, + ]; + + const Stories = STORIES.map(store => { + return ( + <div className="story"> + <img alt={store.alt} src={store.img} /> + <div> + <div className="boldFont">{store.name}</div> + <div className="lightFont">{store.time}๋ถ„ ์ „</div> + </div> + </div> + ); + }); return ( <div> - <h3>๋ฉ”์ธ</h3> + <Nav /> + <main> + <div className="content"> + <div> + {feeds.map(feed => { + return <Feed key={`feed${feed.id}`} data={feed} />; + })} + </div> + <div className="mainRight"> + <div className="rightTop"> + <img + className="wecodeLogo" + alt="์œ„์ฝ”๋“œ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">wecode_bootcamp</div> + <div className="lightFont">WeCode | ์œ„์ฝ”๋“œ</div> + </div> + </div> + <div className="rightMiddle"> + <div className="seeAllStory"> + <div className="lightFont">์Šคํ† ๋ฆฌ</div> + <div className="boldFont">๋ชจ๋‘ ๋ณด๊ธฐ</div> + </div> + <div className="seeStory">{Stories}</div> + </div> + <div className="rightMiddle"> + <div className="friendsRecommendContainer"> + <div className="lightFont">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</div> + <div className="boldFont">๋ชจ๋‘ ๋ณด๊ธฐ</div> + </div> + <div className="friendsRecommendList"> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">ํ™๊ธธ๋™</div> + <div className="lightFont">๋ธ”๋ผ๋ธ”๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์•„๋ฌด๊ฐœ</div> + <div className="lightFont">์‹œ๋Œ์‹œ๋Œ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์–ด์ฉŒ๊ตฌ</div> + <div className="lightFont">์–„๋ผ์–„๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์ €์ฉŒ๊ตฌ</div> + <div className="lightFont">์šœ๋กœ์šœ๋กœ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์ผ๋ผ์ผ๋ผ</div> + <div className="lightFont">๋ธ”๋ผ๋ธ”๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + </div> + </div> + <footer> + <div className="lightFont"> + Westagram ์ •๋ณด . ์ง€์› . ํ™๋ณด ์„ผํ„ฐ . API . ์ฑ„์šฉ์ •๋ณด . + ๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ . ์•ฝ๊ด€ . ๋””๋ ‰ํ„ฐ๋ฆฌ . ํ”„๋กœํ•„ . ํ•ด์‹œํƒœ๊ทธ . ์–ธ์–ด + </div> + <div className="lightFont boldFont">ยฉ 2021 WESTAGRAM</div> + </footer> + </div> + </div> + </main> </div> ); }
JavaScript
๋ฐ˜์˜ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -1,10 +1,178 @@ import React from 'react'; +import Nav from '../../../components/Nav/Nav'; +import Feed from '../../../components/Feed/Feed'; +import './Main.scss'; + class Main extends React.Component { + constructor() { + super(); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feedData.json', { + method: 'GET', + }) + .then(res => res.json()) + .then(data => { + this.setState({ + feeds: data, + }); + }); + } + render() { + const { feeds } = this.state; + + const STORIES = [ + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder1', + time: 43, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder2', + time: 3, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder3', + time: 23, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder4', + time: 27, + }, + { + alt: '์Šคํ† ๋ฆฌ ํ”„๋กœํ•„ ์‚ฌ์ง„', + img: '/images/youngho/wecode.png', + name: 'wecorder5', + time: 13, + }, + ]; + + const Stories = STORIES.map(store => { + return ( + <div className="story"> + <img alt={store.alt} src={store.img} /> + <div> + <div className="boldFont">{store.name}</div> + <div className="lightFont">{store.time}๋ถ„ ์ „</div> + </div> + </div> + ); + }); return ( <div> - <h3>๋ฉ”์ธ</h3> + <Nav /> + <main> + <div className="content"> + <div> + {feeds.map(feed => { + return <Feed key={`feed${feed.id}`} data={feed} />; + })} + </div> + <div className="mainRight"> + <div className="rightTop"> + <img + className="wecodeLogo" + alt="์œ„์ฝ”๋“œ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">wecode_bootcamp</div> + <div className="lightFont">WeCode | ์œ„์ฝ”๋“œ</div> + </div> + </div> + <div className="rightMiddle"> + <div className="seeAllStory"> + <div className="lightFont">์Šคํ† ๋ฆฌ</div> + <div className="boldFont">๋ชจ๋‘ ๋ณด๊ธฐ</div> + </div> + <div className="seeStory">{Stories}</div> + </div> + <div className="rightMiddle"> + <div className="friendsRecommendContainer"> + <div className="lightFont">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</div> + <div className="boldFont">๋ชจ๋‘ ๋ณด๊ธฐ</div> + </div> + <div className="friendsRecommendList"> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">ํ™๊ธธ๋™</div> + <div className="lightFont">๋ธ”๋ผ๋ธ”๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์•„๋ฌด๊ฐœ</div> + <div className="lightFont">์‹œ๋Œ์‹œ๋Œ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์–ด์ฉŒ๊ตฌ</div> + <div className="lightFont">์–„๋ผ์–„๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์ €์ฉŒ๊ตฌ</div> + <div className="lightFont">์šœ๋กœ์šœ๋กœ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + <div className="friendsRecommend"> + <img + alt="์นœ๊ตฌ์ถ”์ฒœ ํ”„๋กœํ•„ ์‚ฌ์ง„" + src="/images/youngho/wecode.png" + /> + <div> + <div className="boldFont">์ผ๋ผ์ผ๋ผ</div> + <div className="lightFont">๋ธ”๋ผ๋ธ”๋ผ๋‹˜ ์™ธ 2๋ช…์ด ...</div> + </div> + <div className="followFont boldFont">ํŒ”๋กœ์šฐ</div> + </div> + </div> + </div> + <footer> + <div className="lightFont"> + Westagram ์ •๋ณด . ์ง€์› . ํ™๋ณด ์„ผํ„ฐ . API . ์ฑ„์šฉ์ •๋ณด . + ๊ฐœ์ธ์ •๋ณด์ฒ˜๋ฆฌ๋ฐฉ์นจ . ์•ฝ๊ด€ . ๋””๋ ‰ํ„ฐ๋ฆฌ . ํ”„๋กœํ•„ . ํ•ด์‹œํƒœ๊ทธ . ์–ธ์–ด + </div> + <div className="lightFont boldFont">ยฉ 2021 WESTAGRAM</div> + </footer> + </div> + </div> + </main> </div> ); }
JavaScript
๋ฐ˜์˜ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,285 @@ +@import 'https://fonts.googleapis.com/css2?family=Lobster&display=swap'; +@media screen and (max-width: 1200px) { + .mainRight { + display: none; + } + + .article { + width: 100%; + } +} + +body { + margin: 0; + padding: 0; + border-collapse: collapse; +} + +article { + width: 800px; + background-color: white; +} + +nav { + display: flex; + justify-content: space-around; + align-items: center; + margin: 0px 170px; + height: 60px; +} + +.logo { + width: 30px; + height: 30px; + padding-right: 15px; + border-right: 1px solid black; +} + +.rightIcon { + width: 30px; + height: 30px; + margin-right: 30px; +} + +.instagram { + margin-left: 15px; + font-family: 'Lobster', cursive; + font-size: 33px; + font-weight: 1; +} + +.search { + outline: none; +} + +.writeComment { + width: 100%; + height: 40px; + padding-left: 10px; + border: 1px solid #efefef; + outline: none; +} + +.search-box { + > input { + width: 200px; + height: 20px; + border: 1px solid #dbdbdb; + text-align: center; + } + + position: relative; + > img { + position: absolute; + width: 11px; + height: 11px; + left: 75px; + top: 7px; + } +} + +main { + padding: 60px 0px; + background-color: #fafafa; +} + +.content { + display: flex; + justify-content: space-around; + flex-direction: row; + margin: 0px 350px; +} + +.mainRight { + width: 330px; + margin-left: 10px; +} + +.feedTop { + display: flex; + justify-content: space-between; + align-items: center; + width: 780px; + padding: 10px 10px; + border: 1px solid #e6e6e6; +} + +.feedUser { + display: flex; + align-items: center; +} + +.feedPicture { + height: 800px; + border: 1px solid #e6e6e6; +} + +.feedId { + margin-left: 10px; +} + +.feedMore { + width: 15px; + height: 15px; +} + +.smallUserPicture { + width: 40px; + height: 40px; + border: 1px solid #e6e6e6; + border-radius: 50px; +} + +.feedIcons { + > img { + width: 27px; + height: 27px; + } + + > div { + > img { + width: 27px; + height: 27px; + } + } + display: flex; + justify-content: space-between; + flex-direction: row; + margin: 0px; +} + +.feedBottom { + padding: 10px 10px; + border-left: 1px solid #efefef; + border-right: 1px solid #efefef; + border-bottom: 1px solid #efefef; +} + +.FeedBottomLeftIcon { + > img { + padding-right: 10px; + } +} + +.like { + > img { + width: 15px; + height: 15px; + margin-right: 5px; + border: 1px solid #e6e6e6; + border-radius: 7px; + } + display: flex; + align-items: center; +} + +.comment { + > div { + > div { + color: #a4a4a4; + } + } +} + +.commentIcons { + float: right; + display: inline; + width: 15px; + height: 15px; + margin-left: 5px; +} + +.boldFont { + font-weight: bolder; +} + +.addCommentFrom { + position: relative; + width: 786px; +} + +.postingButton { + position: absolute; + color: #cde6fc; + background-color: white; + top: 14px; + right: 10px; + border: 0; + outline: 0; +} + +.rightTop { + display: flex; + align-items: center; + width: 300px; +} + +.wecodeLogo { + width: 60px; + height: 60px; + margin-right: 20px; + border-radius: 50px; +} + +.lightFont { + color: #9e9e9e; +} + +.followFont { + color: #3797f0; +} + +.rightMiddle { + height: 200px; + margin: 20px 0px; + padding: 12px; + background: white; + border: 3px solid #e6e6e6; + border-radius: 7px; + overflow: scroll; +} + +.seeAllStory { + display: flex; + align-items: center; + justify-content: space-between; +} + +.friendsRecommendContainer { + display: flex; + align-items: center; + justify-content: space-between; +} + +.story { + display: flex; + align-items: center; + margin: 10px; + + > img { + width: 40px; + height: 40px; + margin-right: 10px; + border-radius: 50px; + border: 2px solid #d93081; + } +} + +.friendsRecommend { + display: flex; + align-items: center; + margin: 10px; + > img { + width: 40px; + height: 40px; + border-radius: 50px; + margin-right: 10px; + } +} + +footer { + margin-top: 20px; + > .boldFont { + margin-top: 10px; + } +}
Unknown
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,285 @@ +@import 'https://fonts.googleapis.com/css2?family=Lobster&display=swap'; +@media screen and (max-width: 1200px) { + .mainRight { + display: none; + } + + .article { + width: 100%; + } +} + +body { + margin: 0; + padding: 0; + border-collapse: collapse; +} + +article { + width: 800px; + background-color: white; +} + +nav { + display: flex; + justify-content: space-around; + align-items: center; + margin: 0px 170px; + height: 60px; +} + +.logo { + width: 30px; + height: 30px; + padding-right: 15px; + border-right: 1px solid black; +} + +.rightIcon { + width: 30px; + height: 30px; + margin-right: 30px; +} + +.instagram { + margin-left: 15px; + font-family: 'Lobster', cursive; + font-size: 33px; + font-weight: 1; +} + +.search { + outline: none; +} + +.writeComment { + width: 100%; + height: 40px; + padding-left: 10px; + border: 1px solid #efefef; + outline: none; +} + +.search-box { + > input { + width: 200px; + height: 20px; + border: 1px solid #dbdbdb; + text-align: center; + } + + position: relative; + > img { + position: absolute; + width: 11px; + height: 11px; + left: 75px; + top: 7px; + } +} + +main { + padding: 60px 0px; + background-color: #fafafa; +} + +.content { + display: flex; + justify-content: space-around; + flex-direction: row; + margin: 0px 350px; +} + +.mainRight { + width: 330px; + margin-left: 10px; +} + +.feedTop { + display: flex; + justify-content: space-between; + align-items: center; + width: 780px; + padding: 10px 10px; + border: 1px solid #e6e6e6; +} + +.feedUser { + display: flex; + align-items: center; +} + +.feedPicture { + height: 800px; + border: 1px solid #e6e6e6; +} + +.feedId { + margin-left: 10px; +} + +.feedMore { + width: 15px; + height: 15px; +} + +.smallUserPicture { + width: 40px; + height: 40px; + border: 1px solid #e6e6e6; + border-radius: 50px; +} + +.feedIcons { + > img { + width: 27px; + height: 27px; + } + + > div { + > img { + width: 27px; + height: 27px; + } + } + display: flex; + justify-content: space-between; + flex-direction: row; + margin: 0px; +} + +.feedBottom { + padding: 10px 10px; + border-left: 1px solid #efefef; + border-right: 1px solid #efefef; + border-bottom: 1px solid #efefef; +} + +.FeedBottomLeftIcon { + > img { + padding-right: 10px; + } +} + +.like { + > img { + width: 15px; + height: 15px; + margin-right: 5px; + border: 1px solid #e6e6e6; + border-radius: 7px; + } + display: flex; + align-items: center; +} + +.comment { + > div { + > div { + color: #a4a4a4; + } + } +} + +.commentIcons { + float: right; + display: inline; + width: 15px; + height: 15px; + margin-left: 5px; +} + +.boldFont { + font-weight: bolder; +} + +.addCommentFrom { + position: relative; + width: 786px; +} + +.postingButton { + position: absolute; + color: #cde6fc; + background-color: white; + top: 14px; + right: 10px; + border: 0; + outline: 0; +} + +.rightTop { + display: flex; + align-items: center; + width: 300px; +} + +.wecodeLogo { + width: 60px; + height: 60px; + margin-right: 20px; + border-radius: 50px; +} + +.lightFont { + color: #9e9e9e; +} + +.followFont { + color: #3797f0; +} + +.rightMiddle { + height: 200px; + margin: 20px 0px; + padding: 12px; + background: white; + border: 3px solid #e6e6e6; + border-radius: 7px; + overflow: scroll; +} + +.seeAllStory { + display: flex; + align-items: center; + justify-content: space-between; +} + +.friendsRecommendContainer { + display: flex; + align-items: center; + justify-content: space-between; +} + +.story { + display: flex; + align-items: center; + margin: 10px; + + > img { + width: 40px; + height: 40px; + margin-right: 10px; + border-radius: 50px; + border: 2px solid #d93081; + } +} + +.friendsRecommend { + display: flex; + align-items: center; + margin: 10px; + > img { + width: 40px; + height: 40px; + border-radius: 50px; + margin-right: 10px; + } +} + +footer { + margin-top: 20px; + > .boldFont { + margin-top: 10px; + } +}
Unknown
๋ฐ˜์˜ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,125 @@ +import React from 'react'; +import Comment from '../Comment/Comment'; +import './Feed.scss'; + +class Feed extends React.Component { + constructor(props) { + super(props); + const { comment } = this.props.data; + this.state = { + comment: comment, + commentInputValue: '', + }; + } + + addComment = () => { + const { comment, commentInputValue } = this.state; + if (commentInputValue.length >= 1) { + const newComment = { + id: comment.length + 1, + content: commentInputValue, + isLiked: false, + }; + this.setState({ + comment: [...comment, newComment], + commentInputValue: '', + }); + } + }; + + deleteComment = e => { + const { comment } = this.state; + const deleteCommentId = Number(e.target.id); + const result = comment.filter(comment => comment.id !== deleteCommentId); + this.setState({ + comment: result, + }); + }; + + inputEnterPress = e => { + if (e.key === 'Enter') { + e.preventDefault(); + this.addComment(); + } + }; + + onChange = e => { + const { value } = e.target; + this.setState({ + commentInputValue: value, + }); + }; + + render() { + const { inputEnterPress, onChange, addComment, deleteComment } = this; + const { commentInputValue, comment } = this.state; + const { alt, img, userName } = this.props.data; + + return ( + <div className="feed"> + <article> + <div className="feedTop"> + <div className="feedUser"> + <img className="smallUserPicture" alt={alt} src={img} /> + <div className="feedId boldFont">{userName}</div> + </div> + <img + className="feedMore" + alt="๋”๋ณด๊ธฐ" + src="/images/youngho/more.png" + /> + </div> + <img className="feedPicture" alt="ํ”ผ๋“œ์‚ฌ์ง„" src={img} /> + <div className="feedBottom"> + <div className="feedIcons"> + <div className="FeedBottomLeftIcon"> + <img alt="๋นจ๊ฐ„์ƒ‰ํ•˜ํŠธ" src="/images/youngho/redHeart.png" /> + <img alt="๋ฉ”์„ธ์ง€" src="/images/youngho/speech-bubble.png" /> + <img alt="์—…๋กœ๋“œ" src="/images/youngho/upload.png" /> + </div> + <img alt="์ €์žฅ" src="/images/youngho/ribbon.png" /> + </div> + <div className="like"> + <img alt="์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅธ ์‚ฌ๋žŒ ์‚ฌ์ง„" src={img} /> + <span className="boldFont">manja</span>๋‹˜ + <span className="boldFont">์™ธ 7๋ช…</span>์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค + </div> + {comment.map(comment => { + return ( + <Comment + key={comment.id} + id={comment.id} + userName={comment.userName} + comment={comment.content} + isLiked={comment.isLiked} + deleteComment={deleteComment} + /> + ); + })} + </div> + <form className="addCommentFrom"> + <input + className="writeComment" + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onKeyPress={inputEnterPress} + onChange={onChange} + value={commentInputValue} + /> + <input + className={`postingButton ${ + commentInputValue.length ? 'activeButton' : '' + }`} + type="button" + defaultValue="๊ฒŒ์‹œ" + onClick={addComment} + disabled={commentInputValue.length ? false : true} + /> + </form> + </article> + </div> + ); + } +} + +export default Feed;
JavaScript
๋ฐ˜์˜ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,125 @@ +import React from 'react'; +import Comment from '../Comment/Comment'; +import './Feed.scss'; + +class Feed extends React.Component { + constructor(props) { + super(props); + const { comment } = this.props.data; + this.state = { + comment: comment, + commentInputValue: '', + }; + } + + addComment = () => { + const { comment, commentInputValue } = this.state; + if (commentInputValue.length >= 1) { + const newComment = { + id: comment.length + 1, + content: commentInputValue, + isLiked: false, + }; + this.setState({ + comment: [...comment, newComment], + commentInputValue: '', + }); + } + }; + + deleteComment = e => { + const { comment } = this.state; + const deleteCommentId = Number(e.target.id); + const result = comment.filter(comment => comment.id !== deleteCommentId); + this.setState({ + comment: result, + }); + }; + + inputEnterPress = e => { + if (e.key === 'Enter') { + e.preventDefault(); + this.addComment(); + } + }; + + onChange = e => { + const { value } = e.target; + this.setState({ + commentInputValue: value, + }); + }; + + render() { + const { inputEnterPress, onChange, addComment, deleteComment } = this; + const { commentInputValue, comment } = this.state; + const { alt, img, userName } = this.props.data; + + return ( + <div className="feed"> + <article> + <div className="feedTop"> + <div className="feedUser"> + <img className="smallUserPicture" alt={alt} src={img} /> + <div className="feedId boldFont">{userName}</div> + </div> + <img + className="feedMore" + alt="๋”๋ณด๊ธฐ" + src="/images/youngho/more.png" + /> + </div> + <img className="feedPicture" alt="ํ”ผ๋“œ์‚ฌ์ง„" src={img} /> + <div className="feedBottom"> + <div className="feedIcons"> + <div className="FeedBottomLeftIcon"> + <img alt="๋นจ๊ฐ„์ƒ‰ํ•˜ํŠธ" src="/images/youngho/redHeart.png" /> + <img alt="๋ฉ”์„ธ์ง€" src="/images/youngho/speech-bubble.png" /> + <img alt="์—…๋กœ๋“œ" src="/images/youngho/upload.png" /> + </div> + <img alt="์ €์žฅ" src="/images/youngho/ribbon.png" /> + </div> + <div className="like"> + <img alt="์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅธ ์‚ฌ๋žŒ ์‚ฌ์ง„" src={img} /> + <span className="boldFont">manja</span>๋‹˜ + <span className="boldFont">์™ธ 7๋ช…</span>์ด ์ข‹์•„ํ•ฉ๋‹ˆ๋‹ค + </div> + {comment.map(comment => { + return ( + <Comment + key={comment.id} + id={comment.id} + userName={comment.userName} + comment={comment.content} + isLiked={comment.isLiked} + deleteComment={deleteComment} + /> + ); + })} + </div> + <form className="addCommentFrom"> + <input + className="writeComment" + type="text" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onKeyPress={inputEnterPress} + onChange={onChange} + value={commentInputValue} + /> + <input + className={`postingButton ${ + commentInputValue.length ? 'activeButton' : '' + }`} + type="button" + defaultValue="๊ฒŒ์‹œ" + onClick={addComment} + disabled={commentInputValue.length ? false : true} + /> + </form> + </article> + </div> + ); + } +} + +export default Feed;
JavaScript
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐ŸŒ
@@ -0,0 +1,17 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + password = models.CharField(max_length=100) + relation = models.ManyToManyField('self', through='Follow', symmetrical=False) + + class Meta: + db_table = "users" + + +class Follow(models.Model): + following = models.ForeignKey(User, on_delete = models.SET_NULL, null=True, related_name = "following") + follower = models.ForeignKey(User, on_delete = models.SET_NULL, null=True, related_name = "follower") + + class Meta: + db_table = "follows" \ No newline at end of file
Python
๋‹ค๋ฏผ๋‹˜! ์ž˜ ์ž‘์„ฑํ•ด์ฃผ์…จ๋Š”๋ฐ `email` ์€ ๊ฒน์น  ์ˆ˜ ์—†๋Š” ์ •๋ณด์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹นํ•˜๋Š” ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค! ์•Œ์•„๋ณด์‹œ๊ณ  ์•Œ๋งž์€ ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”~
@@ -0,0 +1,10 @@ +from django.urls import path +from .views import SignupView, LoginView, FollowView + +urlpatterns = [ + path('/user', SignupView.as_view()), + path('/login', LoginView.as_view()), + path('/follow', FollowView.as_view()), + +] +'''Error๊ฐ€ ์•„๋‹ˆ๋ผ django์˜ ๊ถŒ์žฅ์‚ฌํ•ญ. ํ•˜์ง€๋งŒ ํŒจํ„ด ์ปจ๋ฒค์…˜์—๋Š” ์œ„ ์‚ฌํ•ญ์ด ๋งž๋‹ค.'''
Python
import ํ•˜์‹ค ๋•Œ `*` ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์‹œ๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์€ class ๋ฅผ ์ ์–ด์ฃผ์„ธ์š”!
@@ -0,0 +1,10 @@ +from django.urls import path +from .views import SignupView, LoginView, FollowView + +urlpatterns = [ + path('/user', SignupView.as_view()), + path('/login', LoginView.as_view()), + path('/follow', FollowView.as_view()), + +] +'''Error๊ฐ€ ์•„๋‹ˆ๋ผ django์˜ ๊ถŒ์žฅ์‚ฌํ•ญ. ํ•˜์ง€๋งŒ ํŒจํ„ด ์ปจ๋ฒค์…˜์—๋Š” ์œ„ ์‚ฌํ•ญ์ด ๋งž๋‹ค.'''
Python
์ด ๋ถ€๋ถ„ ์„ธ์…˜ ์ค‘ ํ•œ๋ฒˆ ์„ค๋ช… ๋“œ๋ ธ๋Š”๋ฐ, ์™œ `/` ๊ด€๋ จ warning ์ด ๋œฌ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”!?
@@ -0,0 +1,21 @@ +"""westagram URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.urls import path, include + +urlpatterns = [ + path('account', include('account.urls')), + path('posting', include('posting.urls')), +]
Python
์ด ๋ถ€๋ถ„์€ ์œ„์ฝ”๋“œ url ํŒจํ„ด ์ปจ๋ฒค์…˜์— ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์œ„์— ๋ฆฌ๋ทฐ๋“œ๋ฆฐ ๋‚ด์šฉ ์ค‘ `/` ๊ด€๋ จ ์งˆ๋ฌธ ๋‚จ๊ฒจ๋“œ๋ ธ์œผ๋‹ˆ ํ™•์ธํ•ด์ฃผ์„ธ์š”.
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
๊น”๋”ํ•˜๋„ค์š”~! ๐Ÿ’ฏ
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
`json.loads()` ๋„ ํ•ธ๋“คํ•ด์ฃผ์…”์•ผํ•˜๋Š” exception ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™•์ธํ•ด๋ณด์‹œ๊ณ  ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
๋‹ค๋ฏผ๋‹˜์ด ์ž‘์„ฑํ•˜์‹  ์ฝ”๋“œ ๋กœ์ง์„ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. `client` ์—์„œ ๋“ค์–ด์˜จ `email`, `password` ๊ฐ’์„ ์ด์šฉํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” user ๋ฅผ filter ํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค user ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ค€๋‹ค. 2. `email` ๋กœ filter ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ์กด์žฌํ•˜๋Š” user ์ธ์ง€ ํ™•์ธํ•œ๋‹ค. 3. `password` ๋กœ filter ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ์กด์žฌํ•˜๋Š” user ์ธ์ง€ ํ™•์ธํ•œ๋‹ค. 4. client ์—์„œ ๋“ค์–ด์˜จ `email`, `password` ๊ฐ€ ์œ ํšจํ•œ ํฌ๋งท์ธ์ง€ ๊ฒ€์‚ฌํ•˜์—ฌ ์œ ํšจํ•˜๋‹ค๋ฉด CREATE ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ง๋กœ ์ •๋ฆฌํ•ด๋ณด๋‹ˆ ํ๋ฆ„์ด ์ข€ ์–ด์ƒ‰ํ•˜์ง€ ์•Š๋‚˜์š”? ์ด ๋ถ€๋ถ„์„ ์–ด๋–ค ์ˆœ์„œ๋Œ€๋กœ ์ž‘์„ฑํ•˜๋ฉด ์ข‹์„์ง€ ์ œ๊ฐ€ ํ•œ๊ฒƒ์ฒ˜๋Ÿผ ๊ธ€๋กœ ์ •๋ฆฌํ•ด์„œ comment ๋‚จ๊ฒจ์ฃผ์„ธ์š”.
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
์—ฌ๊ธฐ์„œ ์ž ๊น ์งˆ๋ฌธ! `KeyError` ์–ด๋–ค ์—๋Ÿฌ์ด๋ฉฐ, ์–ธ์ œ ๋ฐœ์ƒํ• ๊นŒ์š”?
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
SignupView ์™€ ๋น„์Šทํ•œ ๋กœ์ง ํ๋ฆ„์ด๋„ค์š”. ์œ„์ชฝ ๋‚จ๊ฒจ๋“œ๋ฆฐ ์งˆ๋ฌธ ๋จผ์ € ํ™•์ธํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,17 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + password = models.CharField(max_length=100) + relation = models.ManyToManyField('self', through='Follow', symmetrical=False) + + class Meta: + db_table = "users" + + +class Follow(models.Model): + following = models.ForeignKey(User, on_delete = models.SET_NULL, null=True, related_name = "following") + follower = models.ForeignKey(User, on_delete = models.SET_NULL, null=True, related_name = "follower") + + class Meta: + db_table = "follows" \ No newline at end of file
Python
`from django.db import models class User(models.Model): email = models.EmailField(max_length=50, unique=True)` ๋กœ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,10 @@ +from django.urls import path +from .views import SignupView, LoginView, FollowView + +urlpatterns = [ + path('/user', SignupView.as_view()), + path('/login', LoginView.as_view()), + path('/follow', FollowView.as_view()), + +] +'''Error๊ฐ€ ์•„๋‹ˆ๋ผ django์˜ ๊ถŒ์žฅ์‚ฌํ•ญ. ํ•˜์ง€๋งŒ ํŒจํ„ด ์ปจ๋ฒค์…˜์—๋Š” ์œ„ ์‚ฌํ•ญ์ด ๋งž๋‹ค.'''
Python
`from django.urls import path from .views import SignupView, LoginView` ๋กœ ํ•„์š”ํ•œ ๋ทฐ ํด๋ž˜์Šค ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์œผ๋กœ ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,10 @@ +from django.urls import path +from .views import SignupView, LoginView, FollowView + +urlpatterns = [ + path('/user', SignupView.as_view()), + path('/login', LoginView.as_view()), + path('/follow', FollowView.as_view()), + +] +'''Error๊ฐ€ ์•„๋‹ˆ๋ผ django์˜ ๊ถŒ์žฅ์‚ฌํ•ญ. ํ•˜์ง€๋งŒ ํŒจํ„ด ์ปจ๋ฒค์…˜์—๋Š” ์œ„ ์‚ฌํ•ญ์ด ๋งž๋‹ค.'''
Python
Error๊ฐ€ ์•„๋‹ˆ๋ผ django์˜ ๊ถŒ์žฅ์‚ฌํ•ญ์ด์–ด์„œ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํŒจํ„ด ์ปจ๋ฒค์…˜์—๋Š” ๊ณ ์ณ์„œ ์“ฐ๋Š” ๊ฒƒ์ด ๋งž๊ธฐ ๋•Œ๋ฌธ์— ๊ณ ์ณค์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,21 @@ +"""westagram URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/3.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.urls import path, include + +urlpatterns = [ + path('account', include('account.urls')), + path('posting', include('posting.urls')), +]
Python
์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
`class SignupView(View): def post(self, request): try: data = json.loads(request.body) user = User.objects.filter(email=data['email'],password=data['password'])` ์•„๋ž˜ ๋กœ๊ทธ์ธ๋ทฐ๋„ ๋˜‘๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค. ์ด์œ  : ์‚ฌ์šฉ์ž์—๊ฒŒ ํ˜•์‹์„ ๋ฐ›์„ ๋•Œ ๊ผญ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ json์ด ์˜ค์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ธฐ์— Key-error ์ƒํ™ฉ์„ ์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ.
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
์ด๊ฑฐ ํƒํ–ฅ๋‹˜ํ•˜๊ณ  ์ด์•ผ๊ธฐํ•˜๋‹ค๊ฐ€ ์•ˆ ๊ฒƒ์ธ๋ฐ, ํ‚ค ์—๋Ÿฌ๋ž€ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋ฉฐ ๋”•์…”๋„ˆ๋ฆฌ ๋‚ด๋ถ€์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ๋‚ด๋ ค๊ณ  ํ•  ๋•Œ ๋‚˜๋Š” ์—๋Ÿฌ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ์˜ˆ์‹œ๋กœ๋Š” ๋งŒ์ผ ๋ฐ์ดํ„ฐ๊ฐ€ email=~~~ password=~~~ ๋กœ ์˜ค๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ emailadkfj=~~~ apsdkfjsa=~~~์ฒ˜๋Ÿผ ํ‚ค ๋‚ด์šฉ์ด ์ž˜๋ชป ์™€๋ฒ„๋ฆฌ๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋กœ ํ‚ค ์—๋Ÿฌ๋ฅผ ๋‚ด๊ธฐ ์œ„ํ•ด ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์˜๋„ํ•˜๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
๋จผ์ € ๋‘ ํด๋ž˜์Šค ๋ชจ๋‘ ๊ณตํ†ต์ ์œผ๋กœ user๋ผ๋Š” ๋ณ€์ˆ˜๊ฐ€ ๋ถˆํ•„์š”ํ•ด์„œ ์ง€์› ์Šต๋‹ˆ๋‹ค. - ์ด์œ  : ์ด๋ฏธ User.~๋ฅผ ํ†ตํ•ด ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ๋ฐ์ดํ„ฐ์—์„œ ํ•„์š”ํ•œ ์ž๋ฃŒ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ๋ถˆํ•„์š”ํ•œ ๋ณ€์ˆ˜์˜€๋‹ค. - comment : 1. client์—์„œ ๋“ค์–ด์˜จ ๊ฐ’ ์ค‘ ์ด๋ฉ”์ผ ํ•„ํ„ฐ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ์กด์žฌํ•˜๋Š” ์ด๋ฉ”์ผ์ผ ๊ฒฝ์šฐ์ธ์ง€ ํ™•์ธ. 2. (๋งˆ์ฐฌ๊ฐ€์ง€๋กœ) ๋น„๋ฐ€๋ฒˆํ˜ธ ํ•„ํ„ฐ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ์กด์žฌํ•˜๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ์ธ์ง€ ํ™•์ธ. 3. ์ด๋ฉ”์ผ๊ณผ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์œ ํšจํ•œ ํฌ๋งท์ธ์ง€ ๊ฒ€์‚ฌํ•˜์—ฌ ์œ ํšจํ•˜๋ฉด ๊ฐ’ ๋งŒ๋“ค๊ธฐ ์ง„ํ–‰. - ์•„์ง ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆฐ๋‹ค๋Š” ๊ฐœ๋…์ด ํ™•์‹คํžˆ ์„œ์ง€ ์•Š์•„ ํ‘œํ˜„์ด ๋งž๋Š”๊ฑด์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ฌธ๋ฒ•์ ์ธ ์ ์€ ๊ฒ€ํ† ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค..!
@@ -0,0 +1,17 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + password = models.CharField(max_length=100) + relation = models.ManyToManyField('self', through='Follow', symmetrical=False) + + class Meta: + db_table = "users" + + +class Follow(models.Model): + following = models.ForeignKey(User, on_delete = models.SET_NULL, null=True, related_name = "following") + follower = models.ForeignKey(User, on_delete = models.SET_NULL, null=True, related_name = "follower") + + class Meta: + db_table = "follows" \ No newline at end of file
Python
`unique=True` ๊ตณ์ž…๋‹ˆ๋‹ค!!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
@damin0320 ์ •ํ™•ํ•ฉ๋‹ˆ๋‹ค ๋‹ค๋ฏผ๋‹˜!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ๋กœ์ง๋ณ„๋กœ ๋ถ„๋ฆฌํ•ด์„œ ๋„์–ด์“ฐ๊ธฐ๋กœ ๊ตฌ๋ถ„ํ•ด์ฃผ์„ธ์š”! ์ง€๊ธˆ์€ ๋„ˆ๋ฌด ๋‹ค ๋ถ™์–ด์žˆ์–ด์„œ ์ฝ๊ธฐ๊ฐ€ ์ข‹์€ ์ฝ”๋“œ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค ๋‹ค๋ฏผ๋‹˜!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
exists() ํ™œ์šฉ gooood ๐Ÿ‘
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
์ด ๋ถ€๋ถ„์€ ๋กœ์ง์ด ์กฐ๊ธˆ ์ด์ƒํ•œ๋ฐ์š”. password ๋Š” ๊ณ ์œ ๊ฐ’์ด ์•„๋‹ˆ๊ณ , ๊ฐ™์€ password ๋ฅผ ๊ฐ€์ง„ ์œ ์ €์ •๋ณด๋“ค์ด ์ถฉ๋ถ„ํžˆ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ password ๋กœ ํ•„ํ„ฐ๋ฅผ ๊ฑธ์–ด์ค„ ํ•„์š”๋Š” ์—†๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค~
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
์•”ํ˜ธํ™” ๊ณผ์ • ์ ์šฉ ์ •๋ง ์ž˜ํ•˜์…จ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
create() ๋’ค์— save() ๋Š” ๋ถˆํ•„์š”ํ•ฉ๋‹ˆ๋‹ค ๋‹ค๋ฏผ๋‹˜. ์—†์• ์ฃผ์„ธ์š”!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
get() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๊ผญ ์žก์•„์ฃผ์…”์•ผํ•˜๋Š” exception ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์•Œ์•„๋ณด์‹œ๊ณ  exception handling ๋กœ์ง์— ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
์—ฌ๊ธฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ๋กœ์ง์„ ๋ธ”๋ก์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ•œ์ค„์”ฉ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
์ด์•ผ~ jwt ์ƒ์„ฑ ๋กœ์ง๊นŒ์ง€ ๋„ˆ๋ฌด ์ž˜ ์ ์šฉํ•˜์…จ๋„ค์š”! ํ•˜์ง€๋งŒ ์ƒ์„ฑ์„ ์™„๋ฃŒํ–ˆ๋Š”๋ฐ ์ƒ์„ฑ๋งŒํ•˜๊ณ  ์‚ฌ์šฉ์„ ์•ˆํ•˜๊ณ  ๊ณ„์‹œ๋„ค์š”. ๋‹ค์Œ ์Šคํ…์€ ๋ฌด์—‡์ผ๊นŒ์š”?
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
์—ฌ๊ธฐ์„œ ์ด๋ ‡๊ฒŒ else ๋กœ ์žก์•„์ฃผ์‹œ๊ธฐ ๋ณด๋‹ค๋Š” checkpw() ์˜ ๊ฐ’์ด Falsy ํ•˜๋‹ค๋ฉด 401 ์„ ๋ฆฌํ„ดํ•ด์ฃผ์‹œ๊ณ , ๋„˜์–ด์™€์„œ ์ง€๊ธˆ ์œ„์— ๋ณด์‹œ๋Š” If ๋ฌธ์˜ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•ด์ฃผ์‹ ๋‹ค๋ฉด ๋ถˆํ•„์š”ํ•œ else ๋ฌธ์ด ์—†์–ด์ง€๊ฒ ์ฃ ?
@@ -0,0 +1,103 @@ +import json +import bcrypt +import jwt + +from django.views import View +from django.http import JsonResponse + +from .models import User, Follow +from my_settings import SECRET_KEY + +class SignupView(View): + def post(self, request): + try: + data = json.loads(request.body) + if User.objects.filter(email=data['email']).exists(): + return JsonResponse({"message": "EMAIL_ERROR"}, status=400) + + if not '@' in data['email'] or not '.' in data['email']: + return JsonResponse({"message":"EMAIL_FAIL"}, status=400) + if len(data['password']) < 8: + return JsonResponse({"message":"PASSWORD_TOO_SHORT"}, status=400) + + byted_password = data['password'].encode('utf-8') + hash_password = bcrypt.hashpw(byted_password, bcrypt.gensalt()).decode() + password = hash_password + user = User.objects.create( + email = data['email'], + password = password + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + except KeyError: + return JsonResponse({"message": "KEY_ERROR"}, status=400) + + +class LoginView(View): + def post(self, request): + try: + data = json.loads(request.body) + try: + user = User.objects.get(email=data['email']) + # user๋Š” ๊ฐ์ฒด๋‹ค + user_id = user.id + # ๊ฐ์ฒด์— .~ํ•˜๋ฉด ๋ฐ”๋กœ ๋‚ด์šฉ์„ ๊บผ๋‚ผ ์ˆ˜ ์žˆ๋‹ค. + except User.DoesNotExist: + return JsonResponse({"message":"USER_DOES_NOT_EXIST"}, status=400) + + if bcrypt.checkpw(data['password'].encode('utf-8'), user.password.encode('utf-8')): + token = jwt.encode({'user_id' : user_id}, SECRET_KEY, algorithm="HS256") + # ์œ ์ € id๋ฅผ ํ† ํฐ ๋‚ด์šฉ๋ฌผ์„ ๋„ฃ๋Š”๋‹ค. ์ด๋–„ ์ด๋ฏธ ์ˆซ์ž๊ฐ€ ๋‚˜์™”์œผ๋ฏ€๋กœ data๋กœ ํ•  ํ•„์š” ์—†์Œ + return JsonResponse({'token' : token, "message":"SUCCESS"}, status=200) + + return JsonResponse({"message":"INVALID_USER"}, status=401) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + +class TokenCheckView(View): + def post(self,request): + data = json.loads(request.body) + + user_token_info = jwt.decode(data['token'], SECRET_KEY, algorithms='HS256') + + if User.objects.filter(email=user_token_info['email']).exists(): + return JsonResponse({"message": "SUCCESS"}, status=200) + return JsonResponse({"message":"INVALID_USER"}, status=401) + +class FollowView(View): + def post(self, request): + data = json.loads(request.body) + following = User.objects.get(email=data['following']) + follower = User.objects.get(email=data['follower']) + if following == follower: + return JsonResponse({"message":"SAME_PERSON!"}) + follow = Follow.objects.create( + following = following, + follower = follower + ) + return JsonResponse({"message": "SUCCESS"}, status=200) + +def TokenCheck(func): + def wrapper(self, request, *args, **kwargs): + # ๋ฆฌํ€˜์ŠคํŠธํ•ด์„œ ํ† ํฐ ๊นŒ๋Š” ์•  + # ํ† ํฐ์˜ ํ•ต์‹ฌ์€ ๊ณต๊ฐœ๊ฐ€ ๋˜์–ด๋„ ๋œ๋‹ค -> ๊ทธ๋ž˜์„œ ์˜ˆ์ œ๊ฐ€ user_id์˜€๋˜ ๊ฒƒ. + try: + token = request.headers.get('Authorization') + if token: + payload = jwt.decode(token, SECRET_KEY, algorithms="HS256") + user_id = payload['user_id'] + user = User.objects.get(id=user_id) + # user_id ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ user๋ผ๋Š” ๋ณ€์ˆ˜์— ๋‹ด๊ณ  + + request.user = user + # ๋ณ€์ˆ˜์— ๋‹ด์€ ๊ฒƒ์„ ๋’ค์—์„œ ๋ถ€๋ฅผ request.user์— ๋˜ ๋‹ด์•„์ค€๋‹ค. + return func(self, request, *args, **kwargs) + # ์–˜๋Š” returnํ•˜๋ฉด์„œ request๋ฅผ posting์—์„œ ์‚ฌ์šฉํ•  ์˜ˆ์ •(.user ๋งŒ ๋ถ™์ด๋ฉด ์ด์ œ ์ž๋™ ์™„์„ฑ) + return JsonResponse({"message":"GIVE_ME_TOKEN"}, status=400) + except jwt.InvalidTokenError: + return JsonResponse({"message":"YOUR_TOKEN_ERROR"}, status=400) + + # ์ง€๊ธˆ์€ ์ˆซ์ž๋งŒ ํ™•์ธ ์ƒํ™ฉ(id๊ฐ’๋งŒ ๋ฐ›์•„์„œ ํ™•์ธํ•œ ์ƒํ™ฉ) + # ์ถ”๊ฐ€ : ํ† ํฐ์„ ๋‹ค์‹œ ์•ˆ ๊ฐ–๋‹ค์ค€ ์ƒํ™ฉ(๋ฆฌํ€˜์ŠคํŠธ์— ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.) ; ๋ฆฌํ€˜์ŠคํŠธ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค.(์•„์ด๋””๊ฐ’์„ ๋„ฃ์–ด์ค€๋‹ค.->์ด๊ฑธ ๊บผ๋‚ด ์“ด๋‹ค.) + + return wrapper \ No newline at end of file
Python
ํ† ํฐ์—์„œ ์œ ์ €์ •๋ณด๋ฅผ ์ฐพ์•„์™€์„œ ํ•„ํ„ฐํ•˜๋Š” ๊ธฐ๋Šฅ๊นŒ์ง€! ๐Ÿ‘ ๐Ÿ‘ ๐Ÿ‘ ๋‹ค๋ฏผ๋‹˜ ์ฒ˜์Œ์—๋Š” ์ดํ•ดํ•˜๋Š”๋ฐ ์–ด๋ ค์›Œํ•˜์‹œ๋Š” ๊ฒƒ ๊ฐ™์•„ ๊ฑฑ์ •์„ ์กฐ๊ธˆ ํ–ˆ์—ˆ๋Š”๋ฐ, ์ง€๊ธˆ๋ณด๋ฉด ๋ฌธ์ œ ํ•ด๊ฒฐ๋Šฅ๋ ฅ์ด ์ •๋ง ์ข‹์œผ์‹  ๊ฒƒ ๊ฐ™์•„์š”! ๋„ˆ๋ฌด ์ž˜ํ•˜์…จ์Šต๋‹ˆ๋‹คใ…Žใ…Ž
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + + email = models.EmailField(max_length=50, unique=True) + name = models.CharField(max_length=50, unique=True) + phone = models.CharField(max_length=50, unique=True) + password= models.CharField(max_length=500) + + class Meta: + db_table='users' +
Python
- email ์€ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด, ๊ทธ๋ฆฌ๊ณ  ์ถ”ํ›„ ์žฅ๊ณ  Form ์ด ์ œ๊ณตํ•˜๋Š” validator ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์„ ๊ฐ์•ˆํ•ด์„œ EmailField ๋กœ ์„ ์–ธํ•ด์ฃผ์„ธ์š”! - ๊ทธ๋ฆฌ๊ณ  email ์€ ๊ณ ์œ ๊ฐ’์ด์ฃ ? ์ด๋Ÿด๋•Œ ์ถ”๊ฐ€ํ•ด์ค„ ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค! ์ฐพ์•„์„œ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”~
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + + email = models.EmailField(max_length=50, unique=True) + name = models.CharField(max_length=50, unique=True) + phone = models.CharField(max_length=50, unique=True) + password= models.CharField(max_length=500) + + class Meta: + db_table='users' +
Python
๋ณดํ†ต ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปฌ๋Ÿผ๋ช…์€ ์ค„์—ฌ์“ฐ์ง€์•Š๊ณ  ์ตœ๋Œ€ํ•œ ๋ช…ํ™•ํ•˜๊ฒŒ ํ’€์–ด์„œ ์„ ์–ธํ•ด์ค๋‹ˆ๋‹ค. ์—ฌ๊ธฐ๋Š” password ๋กœ ํ’€์–ด์ฃผ์„ธ์š”~ ๊ทธ๋ฆฌ๊ณ  ์ด์ œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜๋Š”๋งŒํผ ์ตœ๋Œ€ ๊ธ€์ž์ˆ˜๋ฅผ ์กฐ๊ธˆ ๋” ๋„‰๋„‰ํ•˜๊ฒŒ ์žก์•„์ฃผ์‹œ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
- exists ๋ฉ”์†Œ๋“œ ๋„ˆ๋ฌด ์ž˜ ํ™œ์šฉํ•˜์…จ๋„ค์š”!! ๐Ÿ‘ - ์—ฌ๊ธฐ์„œ exists ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋”ฐ๋กœ ๋ณ€์ˆ˜์— ์ €์žฅํ•ด์ค„๊ฒƒ ์—†์ด ์•„๋ž˜ if ๋ฌธ์— ๋ฐ”๋กœ ์ ์šฉํ•ด์ฃผ์‹ ๋‹ค๋ฉด ๋ถˆํ•„์š”ํ•œ ๋ณ€์ˆ˜ ์„ ์–ธ์„ ์—†์•จ ์ˆ˜ ์žˆ๊ฒ ์ฃ ?!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
or, not, in ํ™œ์šฉ ๋„ˆ๋ฌด ์ข‹์Šต๋‹ˆ๋‹ค ํ˜ธ์—ด๋‹˜!!! ๊ทธ๋Ÿฐ๋ฐ email ์„ list ๋กœ ํ˜•๋ณ€ํ™˜ํ•ด์ฃผ์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
์œ„์— ๋ฆฌ๋ทฐ ๋‚ด์šฉ ๋ฐ˜์˜ํ•ด์ฃผ์‹œ๋ฉด ์ด ๋ถ€๋ถ„์— ์ˆ˜์ •์‚ฌํ•ญ์ด ์ƒ๊ธฐ๊ฒ ์ฃ !?
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
`or` ๊ณผ `is` ์ค‘์— ์–ด๋–ค๊ฒŒ ์šฐ์„ ์ˆœ์œ„์ผ๊นŒ์š”? ์กฐ๊ฑด๋ฌธ์—์„œ operator precedence ์— ๋”ฐ๋ผ ์กฐ๊ฑด์ด ์™„์ „ํžˆ ๋ฐ”๋€” ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ž˜ ํŒŒ์•…ํ•˜๊ณ  ์‚ฌ์šฉํ•˜์‹œ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
์ด ๋ถ€๋ถ„๋“ค์—์„œ๋Š” ํŒŒ์ด์ฌ์˜ exception ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Exception handling ์— ๋Œ€ํ•ด์„œ repl.it ์—์„œ ๋ณด์‹ ์ ์ด ์žˆ์œผ์‹ ๋ฐ์š”! ์‹ค์ œ๋กœ view ์—์„œ ์ •๋ง์ •๋ง ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•˜๋Š” ๋งŒํผ ๊ผญ ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•˜๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ๋“ค์—์„œ ์–ด๋–ค exception ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์„์ง€ ํ™•์ธํ•ด๋ณด์‹œ๊ณ  ํ•ธ๋“ค๋ง ๋กœ์ง ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
์ด์ œ bcrypt ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™” ๋กœ์ง ์ถ”๊ฐ€ํ•ด์„œ ์ €์žฅํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
CREATED ๋ฅผ ์˜๋ฏธํ•˜๋Š” 201 ์‚ฌ์šฉ ๋„ˆ๋ฌด ์ž˜ํ•˜์…จ์Šต๋‹ˆ๋‹ค! ๐Ÿ˜‰
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + + email = models.EmailField(max_length=50, unique=True) + name = models.CharField(max_length=50, unique=True) + phone = models.CharField(max_length=50, unique=True) + password= models.CharField(max_length=500) + + class Meta: + db_table='users' +
Python
ํœด๋Œ€ํฐ ๋ฒˆํ˜ธ๋„ ๊ณ ์œ ๊ฐ’์ด๋‹ˆ ์œ„์ฒ˜๋Ÿผ ๊ณ ์œ ๊ฐ’์„ ๋œปํ•˜๋Š” ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
string / list ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ–ˆ์Šต๋‹ˆ๋‹ค. - [x] Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one: - [x] if '.' not in email or '@' not in email:
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
๋ถˆํ•„์š”ํ•œ ์ฃผ์„ ์ œ์™ธํ•˜๊ณ  ์˜ฌ๋ ค์ฃผ์„ธ์š”.
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
else ๋ถˆํ•„์š”
@@ -0,0 +1,76 @@ +import json,bcrypt,jwt +from json.decoder import JSONDecodeError + +from django.db.models import Q +from django.views import View +from django.http import JsonResponse + +from .models import User +from my_settings import SECRET_KEY, ALGORITHM + + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + name = data['name'] + email = data['email'] + phone = data['phone'] + password= data['password'] + + if '.' not in email or '@' not in email: + return JsonResponse({'message':'error_email_form'}, status=401) + + if len(password)<8: + return JsonResponse({'message':'error_password_form'}, status=401) + + if User.objects.filter(Q(name=name) | Q(email=email) | Q(phone=phone)).exists: + return JsonResponse({'message':'id_exist'}, status = 401) + + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + decoded_password= hashed_password.decode('utf-8') + + User.objects.create(email=email, name=name, phone=phone, password=decoded_password) + return JsonResponse({'message':'success_signup'}, status = 201) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + id = data['id'] + password= data['password'] + user = User.objects.get(Q(name=id) | Q(email=id) | Q(phone=id)) + + if user is not id: + return JsonResponse({'message':'error_id_matching'}, status=401) + + if user is id: + decoded_password = user.password + encoded_password = password.encode('utf-8') + bcrypt.checkpw(encoded_password, decoded_password.encode('utf-8')) + token = jwt.encode({'user':user.id}, SECRET_KEY, ALGORITHM) + return JsonResponse({'message':'success_signin', 'access_token':token}, status=201) + + return JsonResponse({'message':'error_password_matching'}, status = 401) + + except User.MultipleObjectsReturned: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except User.DoesNotExist: + return JsonResponse({'message': 'KEY_ERROR'}, status=401) + + except KeyError: + return JsonResponse({'message': 'KEY_ERROR'}, status=400) + + except JSONDecodeError: + return JsonResponse({'message': 'JSONDecodeError'}, status=400) + + +
Python
์„ฑ๊ณต ์ผ€์ด์Šค์—์„œ ์“ธ ๋กœ์ง์ด๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜๋กœ ์œ„์น˜์‹œ์ผœ์ฃผ์„ธ์š”.
@@ -0,0 +1,145 @@ +""" +Django settings for westagram project. + +Generated by 'django-admin startproject' using Django 3.1.7. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" + +from pathlib import Path +from my_settings import SECRET_KEY, DATABASES + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = SECRET_KEY + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] + + +# Application definition + +INSTALLED_APPS = [ +# 'django.contrib.admin', +# 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'corsheaders', + 'user', + 'posting' +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', +# 'django.middleware.csrf.CsrfViewMiddleware', +# 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'corsheaders.middleware.CorsMiddleware', +] + +ROOT_URLCONF = 'westagram.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'westagram.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.1/ref/settings/#databases + +DATABASES = DATABASES + + +# Password validation +# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = '/static/' + +##CORS +CORS_ORIGIN_ALLOW_ALL=True +CORS_ALLOW_CREDENTIALS = True + +CORS_ALLOW_METHODS = ( + 'DELETE', + 'GET', + 'OPTIONS', + 'PATCH', + 'POST', + 'PUT', +) + +CORS_ALLOW_HEADERS = ( + 'accept', + 'accept-encoding', + 'authorization', + 'content-type', + 'dnt', + 'origin', + 'user-agent', + 'x-csrftoken', + 'x-requested-with', +)
Python
์™„๋ฒฝํ•ฉ๋‹ˆ๋‹ค ๊ตญํ˜„๋‹˜! ๐Ÿ’ฏ
@@ -0,0 +1,145 @@ +""" +Django settings for westagram project. + +Generated by 'django-admin startproject' using Django 3.1.7. + +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" + +from pathlib import Path +from my_settings import SECRET_KEY, DATABASES + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = SECRET_KEY + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['*'] + + +# Application definition + +INSTALLED_APPS = [ +# 'django.contrib.admin', +# 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'corsheaders', + 'user', + 'posting' +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', +# 'django.middleware.csrf.CsrfViewMiddleware', +# 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + 'corsheaders.middleware.CorsMiddleware', +] + +ROOT_URLCONF = 'westagram.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'westagram.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/3.1/ref/settings/#databases + +DATABASES = DATABASES + + +# Password validation +# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/3.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = '/static/' + +##CORS +CORS_ORIGIN_ALLOW_ALL=True +CORS_ALLOW_CREDENTIALS = True + +CORS_ALLOW_METHODS = ( + 'DELETE', + 'GET', + 'OPTIONS', + 'PATCH', + 'POST', + 'PUT', +) + +CORS_ALLOW_HEADERS = ( + 'accept', + 'accept-encoding', + 'authorization', + 'content-type', + 'dnt', + 'origin', + 'user-agent', + 'x-csrftoken', + 'x-requested-with', +)
Python
๋„ต๋„ต ํ™•์ธ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค ์Šนํ˜„๋‹˜ ใ…Žใ…Ž ๐Ÿ˜Š๐Ÿ˜Š ๋ฏธ์…˜2 ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค~~~!!๐Ÿ‘จโ€๐Ÿ’ป
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + phone = models.CharField(max_length=11, null=True, unique=True) + full_name = models.CharField(max_length=40, null=True) + user_name = models.CharField(max_length=20, null=True, unique=True) + password = models.CharField(max_length=70) + date_of_birth = models.DateField(null=True) + + class Meta: + db_table = 'users'
Python
๋ถˆํ•„์š”ํ•œ ์ฃผ์„ ์—†์• ์ฃผ์„ธ์š”!
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + phone = models.CharField(max_length=11, null=True, unique=True) + full_name = models.CharField(max_length=40, null=True) + user_name = models.CharField(max_length=20, null=True, unique=True) + password = models.CharField(max_length=70) + date_of_birth = models.DateField(null=True) + + class Meta: + db_table = 'users'
Python
์ž‘์„ฑํ•˜์‹  ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ•„๋“œ๋“ค ์ค‘ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ํ•„๋“œ๋“ค์ด ๋ณด์ด๋Š”๋ฐ ๊ทธ๋Ÿด๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค! ์ฐพ์•„์„œ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š” ๐Ÿ˜‰
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๊น”๋”ํ•ฉ๋‹ˆ๋‹ค!! ๐Ÿ’ฏ ๐Ÿ’ฏ ๐Ÿ’ฏ
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
ํ•„์ˆ˜ํ•„๋“œ, `NULL` ํ—ˆ์šฉํ•„๋“œ์— ๋”ฐ๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹ ๋‹ค๋ฅด๊ฒŒ ์ ์šฉ ์ž˜ํ•˜์…จ์Šต๋‹ˆ๋‹ค!! ๐Ÿ‘๐Ÿ‘๐Ÿ‘ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์กฐ๊ธˆ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒ ๋„ค์š”! ์•„๋ž˜์™€ ๊ฐ™์ด์š”ใ…Žใ…Ž ```suggestion email = data['email'] password = data['password'] phone = data.get('phone', None) full_name = data.get('full_name', None) user_name = data.get('user_name', None) date_of_birth = data.get('date_of_birth', None) ```
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ž…๋ ฅ๋ฐ›์€ ํฐ๋ฒˆํ˜ธ์— `-` ๊ฐ€ ํฌํ•จ๋˜์–ด ๋“ค์–ด์˜ฌ ์‹œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋„ฃ๊ณ ์‹ถ์€ ํ˜•์‹์— ๋งž์ถฐ์ฃผ์‹œ๋Š” ๋กœ์ง ์ข‹์Šต๋‹ˆ๋‹ค! ์•ž์œผ๋กœ ํ”„๋ก ํŠธ์—”๋“œ๋ถ„๋“ค๊ณผ ํ˜‘์—…์„ ํ•˜๋ฉด์„œ ๊ฒฝํ—˜ํ•˜์‹œ๊ฒ ์ง€๋งŒ, ์ด๋Ÿฐ ๋ถ€๋ถ„์€ ๊ผญ ํ”„๋ก ํŠธ/๋ฐฑ ๊ฐ„์˜ ์ƒ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์  ์ธ์ง€ํ•ด์ฃผ์‹œ๊ณ  ๋‹ค์Œ ์ฃผ ํ”„/๋ฐฑ ํ†ต์‹  ๋“ค์–ด๊ฐ€์‹œ๋ฉด ๋„์›€์ด ๋˜์‹ค๊ฑฐ์—์š”! ใ…Žใ…Ž
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ email ๊ณผ password validate ํ•ด์ฃผ์‹œ๋Š” ๋กœ์ง์ธ๋ฐ ์กฐ๊ธˆ ์ž์ž˜ํ•˜๊ฒŒ ๋‚˜๋ˆ„์–ด์„œ ๋น„๊ตํ•˜์‹œ๋ ค๋‹ค๋ณด๋‹ˆ ์กฐ๊ธˆ ๋ณต์žกํ•ด๋ณด์ด๋„ค์š”! ์ฐจ๋ผ๋ฆฌ ์ด๋ฉ”์ผ ํ˜•์‹์— ๋งž๋Š” ์ •๊ทœํ‘œํ˜„์‹์„ ํ†ต์งธ๋กœ ๊ฐ€์ ธ์™€ ์ €์žฅํ•ด๋‘๊ณ  `re.match()` ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•œ๋ฒˆ์— ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. `password` ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๊ตฌ์š”! ๊ทธ๋ ‡๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ์‹œ๋ฉด ์กฐ๊ธˆ ๋” ๊น”๋”ํ•œ ์กฐ๊ฑด๋ฌธ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค ๐Ÿ˜‰
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ •๋ณด์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋กœ์ง์„ ์ž‘์„ฑํ•ด์ฃผ์…จ๋Š”๋ฐ ์ž‘์„ฑํ•ด์ฃผ์‹  ๋กœ์ง์„ ํ๋ฆ„๋Œ€๋กœ ๊ธ€๋กœ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 1. ์ž…๋ ฅ๋ฐ›์€ `email` ๊ฐ’์œผ๋กœ ์œ ์ € ํ…Œ์ด๋ธ”์— filter ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ๋ฐ˜ํ™˜๋ฐ›์€ QuerySet ๋ฆฌ์ŠคํŠธ์— ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด `phone_number` ๊ณผ `user_name` ์œผ๋กœ filter ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค ์ถ”๊ฐ€ ํ™•์ธ์„ ํ•œ๋‹ค. 2. ์กด์žฌํ•œ๋‹ค๋ฉด `400` ์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์žฌ `SignUp` View ์—์„œ๋Š” `email` ์„ ํ•„์ˆ˜๊ฐ’์œผ๋กœ ๋ฐ›์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์‹ค์ƒ `email` ์กด์žฌ ์œ ๋ฌด๋งŒ ํ™•์ธํ•ด๋„ ๋“ฑ๋ก๋œ ์œ ์ €์ธ์ง€ ์•„๋‹Œ์ง€๋Š” ์•Œ ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. `phone_number` ์™€ `user_name` ์€ ๋ถ€๊ฐ€์ •๋ณด๋“ค์ด๋‹ˆ๊นŒ์š”! ๊ทธ๋Ÿฐ๋ฐ `email` ๋กœ ์กด์žฌ์œ ๋ฌด๋Š” ํ™•์ธํ•˜๋”๋ผ๋„ `phone_number` ์™€ `user_name` ์˜ ์ค‘๋ณต ๊ฐ€๋Šฅ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ์ถ”๊ฐ€ ๋กœ์ง์€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ œ๊ฐ€ ๋ชจ๋ธ์ชฝ์— ๋‚จ๊ฒจ๋“œ๋ฆฐ ๋ฆฌ๋ทฐ๋ฅผ ํ™•์ธํ•ด๋ณด์‹  ํ›„ ์ƒ๊ฐํ•˜์‹  ๊ตฌ์กฐ์— ๋”ฐ๋ผ ์ˆ˜์ •ํ•ด์ฃผ์‹  ๋‹ค์Œ ๋‹ค์‹œํ•œ๋ฒˆ ์ด์ชฝ ๋ถ€๋ถ„์„ ํ™•์ธํ•ด๋ณด์„ธ์š”!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๊น”๋”ํ•ฉ๋‹ˆ๋‹ค~ ๐Ÿ’ฏ
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๊ตฌ๋ถ„์„ ์œ„ํ•ด ์œ„์•„๋ž˜๋กœ ํ•œ์ค„์”ฉ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹์•„์ง€๊ฒ ๋„ค์š”! ๐Ÿ˜Ž
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ด๋Ÿฐ์‹์œผ๋กœ exception ์„ ์ฒ˜๋ฆฌํ•˜์‹œ๊ฒŒ ๋˜๋ฉด ์–ด๋–ค exception ์ด ๋ฌด์—‡๋•Œ๋ฌธ์— `raise` ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ exception ํ•ธ๋“ค๋Ÿฌ ์ถ”๊ฐ€ํ•ด์ฃผ์‹ค๋•Œ๋Š” ์œ„์—์„œ `KeyError` ์ฒ˜๋ฆฌํ•ด์ฃผ์‹ ๊ฒƒ๊ณผ ๊ฐ™์ด ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์ •ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•ด์ฃผ์…”์•ผํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ง€๊ธˆ ๋‹จ๊ณ„์—์„œ ์–ด๋–ค exception ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ๋ชจ๋ฅด๋Š”๊ฒŒ ๋‹น์—ฐํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿด๋•Œ๋Š” ๊ทธ๋ƒฅ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ˜ธ์ถœํ•ด๋ณด๋ฉด์„œ ๊ฒฝํ—˜ํ•˜์—ฌ ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋‹ค๋ณด๋ฉด ๋‚˜์ค‘์—๋Š” ๋ฉ”์†Œ๋“œ ํ•˜๋‚˜๋ฅผ ์ถ”๊ฐ€ํ• ๋•Œ ๊ด€๋ จํ•œ exception ์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๋กœ์ง์„ ๋ฐ”๋กœ ํ•จ๊ป˜ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์‹ค๊ฑฐ์—์š”! ๐Ÿ˜
@@ -0,0 +1,12 @@ +from django.db import models + +class User(models.Model): + email = models.EmailField(max_length=50, unique=True) + phone = models.CharField(max_length=11, null=True, unique=True) + full_name = models.CharField(max_length=40, null=True) + user_name = models.CharField(max_length=20, null=True, unique=True) + password = models.CharField(max_length=70) + date_of_birth = models.DateField(null=True) + + class Meta: + db_table = 'users'
Python
๋„ค๋„ต!!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์•„ํ•˜ ๋„ค๋„ต!! ๊ถ๊ธˆํ•œ ์  ํ•ด๊ฒฐ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž!!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์Šนํ˜„๋‹˜, ๋ง์”€ํ•˜์‹ ๋Œ€๋กœ, ์ด๋ฉ”์ผ ์ฃผ์†Œ์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ •๊ทœ์‹ ๊ฒ€์‚ฌ์— ํ•„์š”ํ•œ ๊ฐ’์„ REGEX_EMAIL, REGEX_PASSWORD์—๋‹ค๊ฐ€ ์ €์žฅํ•˜์—ฌ ํ™œ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค..! ๋ณด๋‹ค ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•ด์ ธ์„œ ์•„์ฃผ ์ข‹๋„ค์š” ใ…Žใ…Žใ…Ž
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์Šนํ˜„๋‹˜, ์•„๊นŒ ํ•ด์ฃผ์‹  ๋ง์”€ ๋“ฃ๊ณ  User ํด๋ž˜์Šค์— ํŠน์ • ์ปฌ๋Ÿผ์€ ๊ณ ์œ ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๋„๋ก unique ์˜ต์…˜์„ ์„ค์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค..! ๊ทธ๋ฆฌ๊ณ  ๋ง์”€ํ•ด์ฃผ์‹  q ํด๋ž˜์Šค๋„ ๊ณต๋ถ€ํ•˜๊ณ  ๋‹ค์Œ ์†Œ์Šค์—๋„ ์ ์šฉํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
Truthy, Falsy values ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์‹œ๊ณ  ํ•ด๋‹น ์กฐ๊ฑด๋ฌธ๋“ค์„ ์–ด๋–ป๊ฒŒ ๋” ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์„์ง€ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๊ณ  ์ ์šฉํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์ด์ œ bcrypt ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”ํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ๋กœ์ง์„ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์—ฌ๊ธฐ์„œ๋„ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” exception ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค! exception handling ๋กœ์ง์— ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š”!!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
`=` ๊ธฐ์ค€ ๊ฐ„๊ฒฉ ์ •๋ ฌํ•ด์ฃผ์„ธ์š” ๊ตญํ˜„๋‹˜!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
- `|` ์‚ฌ์ด ๊ฐ„๊ฒฉ๋„ ํ†ต์ผํ•ด์ฃผ์„ธ์š”~ - ์ด ๋ถ€๋ถ„ ๋กœ์ง์ด ์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๋˜๋Š”๋ฐ ์ „๋ถ€ user_id ๋ฅผ ๋Œ€์ž…ํ•˜์—ฌ ํ™•์ธํ•˜๊ณ  ์‹ถ์€๊ฒŒ ๋งž์œผ์‹ ๊ฐ€์š”?!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๋„ต๋„ต!! ์ˆ˜์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค~~
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
๋„ต ์Šนํ˜„๋‹˜! ์ €๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธํ•  ๋•Œ ์ด๋ฉ”์ผ, ํœด๋Œ€ํฐ, ์œ ์ €๋„ค์ž„ ์ด ์„ธ๊ฐœ์ค‘์— ํ•˜๋‚˜๋กœ ๋กœ๊ทธ์ธํ• ๊บผ๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•ด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์„ ์•„์˜ˆ user_id๋กœ ๊ฐ’์„ ๋ฐ›์œผ๋ ค ํ–ˆ์Šต๋‹ˆ๋‹ค..! ๊ทธ๋ž˜์„œ ์ด user_id๊ฐ’์„ ๊ฐ€์ง€๊ณ  ํ•˜๋‚˜๋ผ๋„ ์ด๋ฉ”์ผ, ํœด๋Œ€ํฐ, ์œ ์ €๋„ค์ž„๊ณผ ๊ฐ™์€๊ฒŒ ์žˆ์œผ๋ฉด ์œ ํšจํ•œ ์•„์ด๋””๊ฐ’์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์˜€์Šต๋‹ˆ๋‹ค..!
@@ -0,0 +1,87 @@ +import re, json, bcrypt + +from django.views import View +from django.http import JsonResponse, request +from django.db.models.query_utils import Q +from json.decoder import JSONDecodeError + +from user.utils import LoginCheck +from user.models import User + +class SignUpView(View): + def post(self, request): + try: + data = json.loads(request.body) + + email = data['email'] + password = data['password'] + phone = data.get('phone', None) + full_name = data.get('full_name', None) + user_name = data.get('user_name', None) + date_of_birth = data.get('date_of_birth', None) + + if phone: + phone = phone.replace('-','') + + REGEX_EMAIL = '^[a-zA-Z0-9+-_.]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$' + REGEX_PASSWORD = '\S{8,20}' + + if not re.match(REGEX_EMAIL,email): + return JsonResponse({'message':'EMAIL VALIDATION ERROR'}, status=400) + if not re.match(REGEX_PASSWORD,password): + return JsonResponse({'message':'PASSWORD VALIDATION ERROR'}, status=400) + + if not User.objects.filter(email=email): + if phone and User.objects.filter(phone=phone): + return JsonResponse({'message':'PHONE ALREADY EXISTS'}, status=400) + elif user_name and User.objects.filter(user_name=user_name): + return JsonResponse({'message':'USER_NAME ALREADY EXISTS'}, status=400) + else: + return JsonResponse({'message':'EMAIL ALREADY EXISTS'}, status=400) + + hashed_password = bcrypt.hashpw(password.encode('UTF-8'), bcrypt.gensalt()).decode() + + User.objects.create( + email = email, + phone = phone, + full_name = full_name, + user_name = user_name, + password = hashed_password, + date_of_birth = date_of_birth, + ) + + return JsonResponse({'message':'SUCCESS'}, status=200) + + except KeyError: + return JsonResponse({'message':'KEY ERROR'}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) + + +class SignInView(View): + def post(self, request): + try: + data = json.loads(request.body) + + user_id = data['user_id'] + password = data['password'] + + user= User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)) + + if user: + stored_password = User.objects.get(Q(user_name=user_id)|Q(email=user_id)|Q(phone=user_id)).password + if not bcrypt.checkpw(password.encode('UTF-8'), stored_password.encode('UTF-8')): + return JsonResponse({"message":"INVALID_PASSWORD"}, status=401) + else: + return JsonResponse({"message":"INVALID_USER"}, status=401) + + return JsonResponse({"message":"SUCCESS", "Authorization":LoginCheck(user.id)}, status=200) + + except KeyError: + return JsonResponse({"message":"KEY_ERROR"}, status=400) + except JSONDecodeError: + return JsonResponse({'message':'JSON DECODE ERROR'}, status=400) + except Exception as e: + print(e) \ No newline at end of file
Python
์Šนํ˜„๋‹˜ ํ˜น์‹œ json ๊ด€๋ จ exception ์ด JSONDecodeError ์ธ๊ฒƒ์ผ๊นŒ์š”..?
@@ -37,6 +37,8 @@ public PageReviewRes<ReviewRes> getPagedReviewsByTemplestayId(Long templestayId, throw new TemplestayCoreException(ErrorCode.REVIEW_NOT_FOUND); } + Long count = reviewRepository.countByTempleName(templeName); + List<ReviewRes> reviewDtos = reviews.getContent().stream() .map(review -> new ReviewRes( review.getId(), @@ -51,6 +53,7 @@ public PageReviewRes<ReviewRes> getPagedReviewsByTemplestayId(Long templestayId, return new PageReviewRes<>( templestayId, + count, page, size, reviews.getTotalPages(),
Java
๋ฆฌ๋ทฐ ์ „์ฒด ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ํ”„๋ก ํŠธ์—์„œ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ์— ๋„์›€์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ตฟ
@@ -0,0 +1,37 @@ +[ + { + "id": 1, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 2, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 3, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 4, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 5, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 6, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + }, + { + "id": 7, + "img": "images/yoonhee/imgg.jpg", + "text": "ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ" + } +]
Unknown
์• ๊ตญ์ž ์œคํฌ๋‹˜์˜ ๋œป ์ž˜ ์•Œ๊ฒ ์Šต๋‹ˆ๋‹ค.
@@ -1,9 +1,23 @@ import React from 'react'; +import './Login.scss'; +import LoginForm from './LoginForm'; -class Login extends React.Component { +class LoginYoonHee extends React.Component { render() { - return null; + return ( + <article className="login-art"> + <div className="log-in__main"> + <h1 className="main-name">westagram</h1> + <div className="log-in"> + <LoginForm /> + </div> + <a className="find-ps" href="#!"> + ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”? + </a> + </div> + </article> + ); } } -export default Login; +export default LoginYoonHee;
JavaScript
props์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด ์ž˜ ์ดํ•ดํ•˜์…จ๋„ค์š”! ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ํ•  ํ•„์š” ์—†์ด, `<LoginForm>` ์ปดํฌ๋„ŒํŠธ์—์„œ withRouter importํ•ด์„œ ๊ฐ์‹ธ์ฃผ์‹œ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜์‹œ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™๋„ค์š”!
@@ -0,0 +1,75 @@ +.login-art { + display: flex; + justify-content: center; + align-items: center; + height: 100vh; + background-color: var(--color-boxgray); + + .log-in__main { + display: flex; + flex-direction: column; + justify-content: space-evenly; + align-items: center; + width: 300px; + height: 350px; + background-color: white; + border: 1px solid var(--color-boxborder); + } + + .main-name { + font-size: 40px; + font-weight: lighter; + font-family: 'Lobster', cursive; + } + + .log-in { + display: flex; + flex-direction: column; + align-items: center; + } + + .log-in__id, + .log-in__ps { + width: 230px; + height: 35px; + margin: 3px 0; + padding: 3px 10px; + border-radius: 5px; + border: 1px solid var(--color-boxborder); + color: var(--color-textgray); + background-color: var(--color-boxgray); + } + + .find-ps { + text-decoration: none; + font-size: 13px; + } + + .log-in__btn { + width: 230px; + height: 30px; + margin-top: 10px; + margin-bottom: 80px; + border-radius: 5px; + background-color: var(--color--btn-text); + border: none; + color: white; + font-weight: bold; + } + .disabled { + width: 230px; + height: 30px; + margin-top: 10px; + margin-bottom: 80px; + border-radius: 5px; + background-color: var(--color--btn-text-yet); + border: none; + color: white; + font-weight: bold; + } + + .log-in > form { + display: flex; + flex-direction: column; + } +}
Unknown
- ํ•˜๋‚˜์˜ ์š”์†Œ์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์†์„ฑ์„ ๋ถ€์—ฌํ•˜๋Š” ๊ฒฝ์šฐ ์ค‘์š”๋„, ๊ด€๋ จ๋„์— ๋”ฐ๋ผ์„œ ๋‚˜๋ฆ„์˜ convention์„ ์ง€์ผœ์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. - ์ผ๋ฐ˜์ ์ธ convention ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆœ์„œ ์ ์šฉํ•ด์ฃผ์„ธ์š”. [CSS property ์ˆœ์„œ] - Layout Properties (position, float, clear, display) - Box Model Properties (width, height, margin, padding) - Visual Properties (color, background, border, box-shadow) - Typography Properties (font-size, font-family, text-align, text-transform) - Misc Properties (cursor, overflow, z-index) - ๋ฆฌ๋ทฐํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„๋„ ์ฐธ๊ณ ํ•ด์„œ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”-!
@@ -0,0 +1,51 @@ +import React from 'react'; +import { withRouter } from 'react-router-dom'; + +class LoginForm extends React.Component { + constructor() { + super(); + this.state = { id: '', ps: '' }; + } + + goToMain = e => { + this.props.history.push('/main-yoonhee'); + }; + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + render() { + const { id, ps } = this.state; + const isAble = id.includes('@') && ps.length >= 5; + return ( + <form> + <input + name="id" + className="log-in__id" + type="text" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + onChange={this.handleInput} + /> + + <input + name="ps" + className="log-in__ps" + type="password" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + onChange={this.handleInput} + /> + <button + type="button" + className={`log-in__btn ${isAble ? '' : 'disabled'}`} + onClick={this.goToMain} + disabled={!isAble} + > + ๋กœ๊ทธ์ธ + </button> + </form> + ); + } +} + +export default withRouter(LoginForm);
JavaScript
๊ณ„์‚ฐ๋œ ์†์„ฑ๋ช… ์ž˜ ํ™œ์šฉํ•ด์ฃผ์…จ๋„ค์š”! :)
@@ -0,0 +1,10 @@ +import React from 'react'; + +class Comment extends React.Component { + render() { + const { innerText } = this.props; + return <li>{innerText}</li>; + } +} + +export default Comment;
JavaScript
map ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๋ถ€๋ถ„์—์„œ key prop ๋ถ€์—ฌํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
isKeyEnter๋ผ๋Š” ํ•จ์ˆ˜๋ช…์€ key๊ฐ€ enter์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๋Š” boolean ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ ํ•ฉํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”. ํ•จ์ˆ˜์˜ ๋™์ž‘์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ์ง๊ด€์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋™์‚ฌํ˜•์œผ๋กœ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”! ex) addCommentByEnter
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
๋ฆฌ๋ทฐํ•˜์ง€ ์•Š์€ ๋ถ€๋ถ„๋„ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
- `newfeedComment`๋ผ๊ณ  ์ƒˆ๋กœ ์„ ์–ธํ•˜๊ณ  ํ• ๋‹นํ•  ํ•„์š” ์—†์ด, concat ๋ฉ”์„œ๋“œ์— feedComment๋ผ๋Š” state๋ฅผ ์ง์ ‘ ๋„ฃ์–ด์ฃผ์…”๋„ ๋  ๊ฒƒ ๊ฐ™๋„ค์š”. - concat ์ž˜ ์‚ฌ์šฉํ•ด์ฃผ์…จ๋Š”๋ฐ, concat ๋Œ€์‹ ์— spread operator๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋™์ผํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฐ์Šตํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๊ณ  ์ฐพ์•„์„œ ๊ตฌํ˜„ํ•ด๋ณด์„ธ์š”! ```suggestion const { feedComment, commentList } = this.state; this.setState({ commentList: commentList.concat(feedComment), feedComment: '', }); ```
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
์ด ์ปดํฌ๋„ŒํŠธ์˜ tag๋“ค์—์„œ id์™€ className์„ ๊ฐ™์ด ๋ถ€์—ฌํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”??
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
์œ„์— ๋ฆฌ๋ทฐ๋“œ๋ฆฐ ๋‚ด์šฉ์ด๋„ค์š”! index๋ฅผ props๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ, ์ด ๋ถ€๋ถ„์—์„œ key={index}๋กœ ๋ถ€์—ฌํ•ด์ฃผ์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,59 @@ +import React from 'react'; +import Comment from './Comment'; + +class CommentBox extends React.Component { + constructor(props) { + super(props); + this.state = { feedComment: '', commentList: [] }; + } + + handleInput = e => { + this.setState({ [e.target.name]: e.target.value }); + }; + + addCommentByEnter = e => { + if (e.key === 'Enter') { + this.addComment(); + } + }; + + addComment = () => { + const { feedComment, commentList } = this.state; + this.setState({ + commentList: [...commentList, feedComment], + feedComment: '', + }); + }; + + render() { + return ( + <> + <div className="feed__comment-box"> + <input + name="feedComment" + className="comment__input" + placeholder="๋Œ“๊ธ€ ๋‹ฌ๊ธฐ..." + onChange={this.handleInput} + onKeyDown={this.addCommentByEnter} + value={this.state.feedComment} + /> + <button + className="btn btn--hover comment__btn" + onClick={this.addComment} + > + ๊ฒŒ์‹œ + </button> + </div> + <div className="feed__comment"> + <ul className="feed__comment-list"> + {this.state.commentList.map((comment, index) => { + return <Comment key={index} innerText={comment} />; + })} + </ul> + </div> + </> + ); + } +} + +export default CommentBox;
JavaScript
๊ทธ๋ฆฌ๊ณ  cur์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์€ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๋ช…ํ™•ํ•˜์ง€ ์•Š์€๋ฐ, ํ•ด๋‹น ๋ณ€์ˆ˜๊ฐ€ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ๋‚ด์šฉ์ด ์ข€ ๋” ์ง๊ด€์ ์œผ๋กœ ๋“œ๋Ÿฌ๋‚  ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”! ex) comment
@@ -0,0 +1,32 @@ +import React from 'react'; +import Feed from './Feed'; + +class Feeds extends React.Component { + constructor(props) { + super(props); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feed.json') + .then(res => res.json()) + .then(data => { + this.setState({ feeds: data }); + }); + } + + render() { + const { feeds } = this.state; + return ( + <div className="feeds"> + {feeds.map(feed => ( + <Feed key={feed.id} img={feed.img} text={feed.text} /> + ))} + </div> + ); + } +} + +export default Feeds;
JavaScript
- method ์ž˜ ์ƒ๋žตํ•ด์ฃผ์…จ๋„ค์š”! ๐Ÿ‘ - ์ถ”๊ฐ€์ ์œผ๋กœ, `http://localhost:3000` ๋ถ€๋ถ„๋„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํฌํŠธ ๋ฒˆํ˜ธ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ๊ทธ๋•Œ๊ทธ๋•Œ ์ˆ˜์ •ํ•ด์ค˜์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ๋žตํ•ด์„œ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”! ```suggestion fetch('/data/feed.json') ```
@@ -0,0 +1,32 @@ +import React from 'react'; +import Feed from './Feed'; + +class Feeds extends React.Component { + constructor(props) { + super(props); + this.state = { + feeds: [], + }; + } + + componentDidMount() { + fetch('/data/feed.json') + .then(res => res.json()) + .then(data => { + this.setState({ feeds: data }); + }); + } + + render() { + const { feeds } = this.state; + return ( + <div className="feeds"> + {feeds.map(feed => ( + <Feed key={feed.id} img={feed.img} text={feed.text} /> + ))} + </div> + ); + } +} + +export default Feeds;
JavaScript
์˜ค์ž‰ ์—ฌ๊ธฐ์—๋Š” key prop ์ž˜ ๋ถ€์—ฌํ•ด์ฃผ์…จ๋„ค์š”! ์‚ด์ง ํ ์„ ์žก์ž๋ฉด,, ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ๋Š” ๊ฐ ํ”ผ๋“œ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— feeds -> feed๊ฐ€ ๋” ์ ์ ˆํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -1,9 +1,22 @@ +// eslint-disable-next-line import React from 'react'; +import Nav from './Nav'; +import Feeds from './Feeds'; +import MainR from './MainR'; +import './Main.scss'; -class Main extends React.Component { +class MainYoonHee extends React.Component { render() { - return null; + return ( + <div className="main-body"> + <Nav /> + <main> + <Feeds /> + <MainR /> + </main> + </div> + ); } } -export default Main; +export default MainYoonHee;
JavaScript
import ์ˆœ์„œ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”! ์ผ๋ฐ˜์ ์ธ convention์„ ๋”ฐ๋ผ ์ˆœ์„œ๋งŒ ์ž˜ ์ง€์ผœ์ฃผ์…”๋„ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง‘๋‹ˆ๋‹ค. ์•„๋ž˜ ์ˆœ์„œ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”. - React โ†’ Library(Package) โ†’ Component โ†’ ๋ณ€์ˆ˜ / ์ด๋ฏธ์ง€ โ†’ css ํŒŒ์ผ(scss ํŒŒ์ผ)
@@ -0,0 +1,25 @@ +import React from 'react'; + +class Recommend extends React.Component { + render() { + const { nickname, img } = this.props; + return ( + <li className="user main-right__user2"> + <div className="user-and-botton"> + <img + className="user__img user__img--brder-red" + alt={nickname} + src={img} + /> + <div className="user-id2"> + <div className="user__id">{nickname}</div> + <div className="text--gray">ํ•œ๊ตญ์–ดํ•œ๊ตญ์–ดํ•œ๊ตญ์–ด</div> + </div> + </div> + <button className="btn btn--hover nnn">ํŒ”๋กœ์šฐ</button> + </li> + ); + } +} + +export default Recommend;
JavaScript
`<li>`ํƒœ๊ทธ, `<div>`ํƒœ๊ทธ ๋‘˜ ์ค‘ ํ•˜๋‚˜๋กœ๋งŒ ๊ฐ์‹ธ์ฃผ์…”๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,22 @@ +import React from 'react'; + +class Story extends React.Component { + render() { + const { nickname, img } = this.props; + return ( + <li className="user main-right__user"> + <img + className="user__img user__img--brder-red" + alt={nickname} + src={img} + /> + <div className="user-id2"> + <div className="user__id">{nickname}</div> + <div className="text--gray">2์‹œ๊ฐ„ ์ „</div> + </div> + </li> + ); + } +} + +export default Story;
JavaScript
์—ฌ๊ธฐ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€-!
@@ -0,0 +1,281 @@ +/*------๊ณตํ†ต-----*/ +/*------๊ณตํ†ต-----*/ + +.main-body { + background-color: var(--color-boxgray); +} + +main { + display: flex; + margin-left: 100px; +} + +ul > li { + margin: 10px 0; +} + +a { + text-decoration: none; + color: inherit; +} + +.box { + background-color: white; + border: 1px solid var(--color-boxborder); +} +.box-padding { + padding: 10px; +} + +.text { + font-size: 12px; +} + +.text--gray { + margin-top: 3px; + font-size: 13px; + color: var(--color-textgray); +} + +.icon { + border: no; +} + +.btn { + padding: 5px 0px; + border: none; + border-radius: 5px; + background-color: transparent; + color: var(--color--btn-text); + font-weight: bold; +} +.btn--hover:hover { + color: white; + background-color: var(--color--btn-text); +} + +.westagram { + font-family: 'Lobster', cursive; + font-size: 25px; +} +.user { + display: flex; + align-items: center; + justify-content: flex-start; +} + +.user__img { + width: 30px; + height: 30px; + margin-right: 7px; + border-radius: 50%; + border: 1px solid var(--color-textgray); +} + +.user__img--brder-red { + border: 1px solid red; + border-spacing: 3px; +} + +.user__img--small { + width: 20px; + height: 18px; + margin-right: 5px; +} + +.user__img--big { + width: 40px; + height: 40px; +} + +.user__id { + font-size: 14px; + font-weight: bolder; +} + +.user-text2 { + display: flex; + flex-direction: column; +} + +.footer { + margin-top: 15px; +} + +/*--------nav--------*/ +/*--------nav--------*/ +/*--------nav--------*/ + +.nav { + display: flex; + justify-content: space-around; + align-items: center; + padding: 20px 0; + border-bottom: 1px solid var(--color-boxborder); + background-color: white; + + .nav__border { + height: 30px; + border-left: 2px solid black; + } + + .nav-left { + display: flex; + align-items: center; + justify-content: space-between; + width: 18%; + } + + .nav-center { + width: 20%; + } + + .nav-center > .search-bar { + height: 25px; + width: 100%; + border: 1px solid var(--color-boxborder); + background-color: var(--color-boxgray); + text-align: center; + } + .search-bar[value] { + color: var(--color-textgray); + } + + .nav-right { + display: flex; + justify-content: space-between; + width: 12%; + } +} + +/*----------feed----------*/ +/*----------feed----------*/ +/*----------feed----------*/ + +.feeds { + display: flex; + flex-direction: column; + width: 50%; + padding-top: 65px; + + .feed { + display: flex; + align-items: center; + flex-direction: column; + width: 100%; + margin-bottom: 50px; + border: 1px solid var(--color-boxborder); + background-color: white; + } + + .feed__head { + display: flex; + align-items: center; + justify-content: space-between; + width: 95%; + margin: 15px 20px; + } + + .feed__head > .user { + width: 22%; + } + + .feed__img { + width: 100%; + } + + .feed__icons { + display: flex; + justify-content: space-between; + width: 95%; + margin: 12px 0px; + } + + .feed__icons__left { + display: flex; + justify-content: space-between; + width: 18%; + } + + .feed__likes { + display: flex; + align-items: center; + width: 95%; + } + + .feed__text { + width: 95%; + margin-top: 15px; + } + + .feed__comment-box { + width: 100%; + height: 35px; + margin-top: 5px; + border-top: 1px solid var(--color-boxborder); + border-bottom: 1px solid var(--color-boxborder); + } + + .comment__input { + width: 90%; + height: 100%; + padding-left: 14px; + border: none; + } + + .comment__input[placehilder] { + color: var(--color-textgray); + } + + .comment__btn { + width: 8%; + height: 80; + } + + .feed__comment { + width: 100%; + } + .feed__comment-list { + width: 95%; + margin: 10px 0; + padding: 5px 14px; + } + + .feed__comment-list > li { + font-size: 13px; + } +} + +/*-------------main-right------------*/ +/*-------------main-right------------*/ +/*-------------main-right------------*/ + +.main-right { + position: fixed; + right: 40px; + width: 25%; + margin-top: 65px; + + .main-right__header { + display: flex; + align-items: flex-end; + justify-content: space-between; + margin-bottom: 15px; + padding: 0 5px; + } + + .user-and-botton { + width: 70%; + display: flex; + } + + .main-right__story > .box { + margin-top: 15px; + } + + .main-right__user2 { + display: flex; + justify-content: space-between; + margin: 2px; + } +}
Unknown
๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์†์„ฑ์€ ํŒ€์›๋“ค๊ณผ ์ƒ์˜ํ•˜์—ฌ common.scss ํŒŒ์ผ๋กœ ์˜ฎ๊ฒจ์ฃผ์„ธ์š”!