서승완
Builds for 1 pipeline passed in 11 minutes 30 seconds

Merge branch 'usergroup' into 'master'

Usergroup



See merge request !7
from django.http import JsonResponse, Http404
from django.http import Http404, JsonResponse
from ..services import files
......
from django.http import JsonResponse, Http404
from django.http import Http404, JsonResponse
from ..services import groups
......
from django.http import JsonResponse, Http404
from django.http import Http404, JsonResponse
from ..services import users
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11.29 on 2020-06-13 17:18
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('khubox', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='user',
name='password',
field=models.CharField(max_length=77),
),
]
......@@ -33,7 +33,7 @@ class GroupUser(models.Model):
class User(models.Model):
email = models.CharField(max_length=255)
password = models.CharField(max_length=60)
password = models.CharField(max_length=77)
name = models.CharField(max_length=50)
root_folder = models.CharField(max_length=36)
created_at = models.DateTimeField()
......
import json
import uuid
from django.utils import timezone
from ..aws import s3_delete
from ..models import File, Group, GroupUser, User
# 그룹 생성
def create(request):
return {'result': True}
# TODO: Auth
request.user_id = 1
# Load
try:
received = json.loads(request.body.decode('utf-8'))
except json.decoder.JSONDecodeError:
return {'result': False, 'error': '입력이 잘못되었습니다.'}
# Validate
if 'name' not in received or received['name'] == '':
return {'result': False, 'error': '입력이 누락되었습니다.'}
# Create
root_folder = uuid.uuid4()
group = Group.objects.create(
owner_id=request.user_id,
name=received['name'],
root_folder=root_folder,
invite_code=uuid.uuid4(),
created_at=timezone.now()
)
GroupUser.objects.create(
group_id=group.id,
user_id=request.user_id,
joined_at=timezone.now()
)
File.objects.create(
id=root_folder,
owner_user_id=request.user_id,
owner_group_id=group.id,
type='folder',
name='group_%s' % group.id,
size=0,
created_at=timezone.now()
)
return {'result': True, 'group_id': group.id}
# 그룹 초대장 조회
def find_invite(request, invite_code):
return {'result': True}
# TODO: Auth
request.user_id = 1
# Query
group = Group.objects.filter(invite_code=invite_code)
# Check Exists
if len(group) == 0:
return {'result': False, 'error': '존재하지 않는 초대장입니다.'}
# Structure
data = {
'id': group[0].id,
'name': group[0].name
}
# Check Joined
joined = GroupUser.objects.filter(group_id=group[0].id, user_id=request.user_id)
if len(joined) == 0:
data['joined'] = False
else:
data['joined'] = True
return {'result': True, 'data': data}
# 그룹 초대장 사용
def use_invite(request, invite_code):
# TODO: Auth
request.user_id = 1
# Query
group = Group.objects.filter(invite_code=invite_code)
# Check Exists
if len(group) == 0:
return {'result': False, 'error': '존재하지 않는 초대장입니다.'}
# Check Joined
joined = GroupUser.objects.filter(group_id=group[0].id, user_id=request.user_id)
if len(joined) != 0:
return {'result': False, 'error': '이미 가입된 그룹입니다.'}
# Join
GroupUser.objects.create(
group_id=group[0].id,
user_id=request.user_id,
joined_at=timezone.now()
)
return {'result': True}
# 그룹 목록
def list_me(request):
return {'result': True}
# TODO: Auth
request.user_id = 1
# Query
joined = GroupUser.objects.filter(user_id=request.user_id).values_list('group_id', flat=True)
groups = Group.objects.filter(id__in=joined)
# Structure
data = []
for group in groups:
data.append({
'id': group.id,
'name': group.name,
'root_folder': group.root_folder,
})
return {'result': True, 'data': data}
# 그룹 조회
def find_item(request, group_id):
return {'result': True}
# TODO: Auth
request.user_id = 1
# Check Joined
joined = GroupUser.objects.filter(group_id=group_id, user_id=request.user_id)
if len(joined) == 0:
return {'result': False, 'error': '입력이 잘못되었습니다.'}
# Query
group = Group.objects.filter(id=group_id)
# Check Exists
if len(group) == 0:
return {'result': False, 'error': '존재하지 않는 그룹입니다.'}
# Structure
data = {
'id': group[0].id,
'name': group[0].name,
'root_folder': group[0].root_folder,
}
# If Owner
if group[0].owner_id == request.user_id:
user_ids = GroupUser.objects.filter(group_id=group_id).values_list('user_id', flat=True)
users = User.objects.filter(id__in=user_ids)
user_data = []
for user in users:
user_data.append({
'id': user.id,
'name': user.name,
})
data['user'] = user_data
data['invite_code'] = group[0].invite_code
data['created_at'] = group[0].created_at
data['is_owner'] = True
return {'result': True, 'data': data}
# 그룹 수정
def update_item(request, group_id):
# TODO: Auth
request.user_id = 1
# Load
try:
received = json.loads(request.body.decode('utf-8'))
except json.decoder.JSONDecodeError:
return {'result': False, 'error': '입력이 잘못되었습니다.'}
# Validate
if 'name' not in received or received['name'] == '':
return {'result': False, 'error': '입력이 누락되었습니다.'}
# Query
group = Group.objects.filter(id=group_id)
# Check Exists
if len(group) == 0:
return {'result': False, 'error': '존재하지 않는 그룹입니다.'}
# Check Owner
if group[0].owner_id != request.user_id:
return {'result': False, 'error': '권한이 없습니다.'}
# Update
group[0].name = received['name']
group[0].save()
return {'result': True}
# 그룹 삭제
def delete_item(request, group_id):
# TODO: Auth
request.user_id = 1
# Query
group = Group.objects.filter(id=group_id)
# Check Exists
if len(group) == 0:
return {'result': False, 'error': '존재하지 않는 그룹입니다.'}
# Check Owner
if group[0].owner_id != request.user_id:
return {'result': False, 'error': '권한이 없습니다.'}
# S3 Delete
del_list = File.objects.filter(owner_group_id=group_id).values_list('id', flat=True)
s3_delete(del_list)
# Delete
del_list.update(is_trashed=1, deleted_at=timezone.now())
GroupUser.objects.filter(group_id=group_id).delete()
Group.objects.filter(id=group_id).delete()
return {'result': True}
# 그룹 사용자 삭제
def remove_user(request, group_id, user_id):
# TODO: Auth
request.user_id = 1
# Query
group = Group.objects.filter(id=group_id)
# Check Owner
if group[0].owner_id != request.user_id:
return {'result': False, 'error': '권한이 없습니다.'}
# Check Me
if int(user_id) == request.user_id:
return {'result': False, 'error': '본인은 삭제할 수 없습니다.'}
# Remove
GroupUser.objects.filter(group_id=group_id, user_id=user_id).delete()
return {'result': True}
......
import datetime
import json
import jwt
import uuid
from django.conf import settings
from django.contrib.auth.hashers import make_password, check_password
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.utils import timezone
from ..models import File, User
# 회원가입
def create(request):
# Load
try:
received = json.loads(request.body.decode('utf-8'))
except json.decoder.JSONDecodeError:
return {'result': False, 'error': '입력이 잘못되었습니다.'}
# Validate
if 'email' not in received \
or 'password' not in received \
or 'name' not in received:
return {'result': False, 'error': '입력이 누락되었습니다.'}
# Validate Email
try:
validate_email(received['email'])
except ValidationError:
return {'result': False, 'error': '이메일 형식이 잘못되었습니다.'}
# Validate Password
if len(received['password']) < 8:
return {'result': False, 'error': '비밀번호는 최소 8글자 입니다.'}
# Validate Name
if len(received['name']) > 50:
return {'result': False, 'error': '이름은 최대 50글자 입니다.'}
# Check Duplicates
is_exists = User.objects.filter(email=received['email'])
if len(is_exists) > 0:
return {'result': False, 'error': '이미 사용중인 이메일 주소 입니다.'}
# Insert
root_folder = uuid.uuid4()
user = User.objects.create(
email=received['email'],
password=make_password(received['password']),
name=received['name'],
root_folder=root_folder,
created_at=timezone.now()
)
File.objects.create(
id=root_folder,
owner_user_id=user.id,
type='folder',
name='user_%s' % user.id,
size=0,
created_at=timezone.now()
)
return {'result': True}
# 로그인
def login(request):
return {'result': True}
# Load
try:
received = json.loads(request.body.decode('utf-8'))
except json.decoder.JSONDecodeError:
return {'result': False, 'error': '입력이 잘못되었습니다.'}
# Validate
if 'email' not in received \
or 'password' not in received:
return {'result': False, 'error': '입력이 누락되었습니다.'}
# Select
user = User.objects.filter(email=received['email'])
# Not Exists
if len(user) != 1:
return {'result': False, 'error': '로그인에 실패하였습니다.'}
# Check
if check_password(received['password'], user[0].password) is False:
return {'result': False, 'error': '로그인에 실패하였습니다.'}
# Token Generate
token = jwt.encode({'id': user[0].id, 'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=6)},
key=settings.SECRET_KEY, algorithm='HS256')
return {'result': True, 'token': token.decode('utf-8')}
# 회원정보 조회
def find_me(request):
return {'result': True}
# TODO: Auth
request.user_id = 1
# Query
user = User.objects.filter(id=request.user_id)
# Check Exists
if len(user) != 1:
return {'result': False, 'error': '잘못된 요청입니다.'}
# Serialize
data = {
'id': user[0].id,
'email': user[0].email,
'name': user[0].name,
'root_folder': user[0].root_folder,
'created_at': user[0].created_at
}
return {'result': True, 'data': data}
# 회원정보 수정
def update_me(request):
# TODO: Auth
request.user_id = 1
# Load
try:
received = json.loads(request.body.decode('utf-8'))
except json.decoder.JSONDecodeError:
return {'result': False, 'error': '입력이 잘못되었습니다.'}
# Validate
if 'name' not in received \
and ('old_password' not in received and 'password' not in received):
return {'result': False, 'error': '입력이 누락되었습니다.'}
# Query
user = User.objects.filter(id=request.user_id)
# Check Exists
if len(user) != 1:
return {'result': False, 'error': '잘못된 요청입니다.'}
# Change Name
if 'name' in received:
user[0].name = received['name']
# Change Password
if 'old_password' in received and 'password' in received:
if check_password(received['old_password'], user[0].password) is False:
return {'result': False, 'error': '이전 비밀번호가 잘못되었습니다.'}
if len(received['password']) < 8:
return {'result': False, 'error': '비밀번호는 최소 8글자 입니다.'}
user[0].password = make_password(received['password'])
# Save
user[0].save()
return {'result': True}
......
......@@ -20,6 +20,7 @@ jmespath==0.10.0
pip-tools==5.1.2
placebo==0.9.0
pycparser==2.20
PyJWT==1.7.1
PyMySQL==0.9.3
python-dateutil==2.6.1
python-slugify==4.0.0
......