-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/wafflestudio18-5/team2-server…
… into Feature/#18
- Loading branch information
Showing
12 changed files
with
1,056 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
asgiref==3.3.1 | ||
certifi==2020.12.5 | ||
chardet==4.0.0 | ||
Django==3.1.3 | ||
django-debug-toolbar==3.1.1 | ||
django-rest-framework==0.1.0 | ||
djangorestframework==3.12.2 | ||
idna==2.10 | ||
mailjet-rest==1.3.4 | ||
mysqlclient==2.0.1 | ||
python-dotenv==0.15.0 | ||
pytz==2020.4 | ||
requests==2.25.1 | ||
sqlparse==0.4.1 | ||
|
||
urllib3==1.26.2 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Generated by Django 3.1 on 2020-12-30 14:14 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('story', '0003_auto_20201223_1556'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='story', | ||
name='uid', | ||
), | ||
migrations.AddField( | ||
model_name='story', | ||
name='body', | ||
field=models.JSONField(default={}), | ||
), | ||
migrations.AlterField( | ||
model_name='story', | ||
name='featured_image', | ||
field=models.URLField(null=True), | ||
), | ||
migrations.DeleteModel( | ||
name='StoryBlock', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 3.1 on 2021-01-01 08:28 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('story', '0004_auto_20201230_2314'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='story', | ||
name='body', | ||
field=models.JSONField(default=list), | ||
), | ||
migrations.AlterField( | ||
model_name='story', | ||
name='featured_image', | ||
field=models.URLField(blank=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,49 @@ | ||
from rest_framework import serializers | ||
from .models import Story, StoryBlock | ||
|
||
BlockType = (StoryBlock.TEXT, StoryBlock.IMAGE) | ||
from .models import Story | ||
from user.serializers import UserSerializer | ||
|
||
class StorySerializer(serializers.ModelSerializer): | ||
|
||
writer_id = serializers.IntegerField(source='writer.id', read_only=True) | ||
title = serializers.CharField(default='Untitled', allow_blank=True) | ||
subtitle = serializers.SerializerMethodField() | ||
writer = UserSerializer(read_only=True) | ||
title = serializers.CharField(max_length=100, allow_blank=True) | ||
subtitle = serializers.CharField(max_length=140, allow_blank=True) | ||
body = serializers.JSONField() | ||
featured_image = serializers.URLField(allow_blank=True) | ||
created_at = serializers.DateTimeField(read_only=True) | ||
updated_at = serializers.DateTimeField(read_only=True) | ||
published_at = serializers.DateTimeField(allow_null=True, default=None, read_only=True) | ||
published = serializers.BooleanField(default=False, read_only=True) | ||
blocks = serializers.SerializerMethodField(required=False, allow_null=True) | ||
created_at = serializers.DateTimeField(read_only=True) | ||
published_at = serializers.DateTimeField(allow_null=True, default=None, read_only=True) | ||
|
||
class Meta: | ||
model = Story | ||
fields = ( | ||
'id', | ||
'uid', | ||
'writer_id', | ||
'writer', | ||
'title', | ||
'subtitle', | ||
'body', | ||
'featured_image', | ||
'created_at', | ||
'updated_at', | ||
'published_at', | ||
'published', | ||
'blocks', | ||
'published_at', | ||
) | ||
|
||
def validate(self, data): | ||
return data | ||
|
||
def create(self, validated_data): | ||
user = self.context['request'].user | ||
validated_data['writer'] = user | ||
|
||
if validated_data['title'] == '': | ||
validated_data['title'] = 'Untitled' | ||
|
||
story = super(StorySerializer, self).create(validated_data) | ||
|
||
if 'blocks' in self.context['request'].data: | ||
blocks = self.context['request'].data['blocks'] | ||
for i in range(len(blocks)): | ||
# if blocks[i]['block_type'] not in BlockType: | ||
# raise serializers.ValidationError('Only "text" or "image" are allowed for block_type') | ||
StoryBlock.objects.create(story=story, order=i+1, content=blocks[i]['content'], block_type=blocks[i]['block_type']) | ||
|
||
return story | ||
|
||
def update(self, instance, validated_data): | ||
story = super(StorySerializer, self).update(instance, validated_data) | ||
if 'blocks' in self.context['request'].data: | ||
blocks = self.context['request'].data['blocks'] | ||
for block in blocks: | ||
try: | ||
block_to_update = StoryBlock.objects.get(story=story, order=block['order']) | ||
if 'content' in block: | ||
block_to_update.content = block['content'] | ||
if 'block_type' in block: | ||
block_to_update.block_type = block['block_type'] | ||
block_to_update.save() | ||
except StoryBlock.DoesNotExist: | ||
StoryBlock.objects.create(story=story, order=block['order'], content=block['content'], block_type=block['block_type']) | ||
if 'title' in validated_data and validated_data['title'] == '': | ||
validated_data['title'] = 'Untitled' | ||
|
||
story = super(StorySerializer, self).update(instance, validated_data) | ||
return story | ||
|
||
def get_blocks(self, story): | ||
return StoryBlockSerializer(story.blocks, context=self.context, many=True).data | ||
|
||
def get_subtitle(self, story): | ||
data = self.context['request'].data | ||
if 'subtitle' in data: | ||
subtitle = data['subtitle'] | ||
if subtitle == '': | ||
if 'blocks' in data: | ||
try: | ||
first_block = story.blocks.filter(block_type='text').first() | ||
subtitle = first_block.content # 140자로 잘라야 함 | ||
story.subtitle = subtitle | ||
story.save() | ||
except StoryBlock.DoesNotExist: | ||
pass | ||
else: | ||
subtitle = story.subtitle | ||
return subtitle | ||
|
||
class StoryBlockSerializer(serializers.ModelSerializer): | ||
order = serializers.IntegerField(required=False) | ||
|
||
class Meta: | ||
model = StoryBlock | ||
fields = '__all__' # story, order, block_type, content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from mailjet_rest import Client | ||
from django.conf import settings | ||
from datetime import datetime | ||
|
||
mailjet = Client(auth=(settings.MAILJET_API_KEY, settings.MAILJET_API_SECRET), version='v3.1') | ||
|
||
|
||
def send_access_token(email, signup=False, token=None): | ||
if signup: | ||
data = { | ||
'Messages': [ | ||
{ | ||
"From": { | ||
"Email": "[email protected]", | ||
"Name": "Wadium" | ||
}, | ||
"To": [ | ||
{ | ||
"Email": email, | ||
"Name": "" | ||
} | ||
], | ||
"TemplateID": 2118400, # 2118400 for signup, 2118280 for signin | ||
"TemplateLanguage": True, | ||
"Subject": "Finish creating your account on Wadium", | ||
"Variables": { | ||
"callback_uri": f"https://www.wadium.shop/callback/email?token={token}&operation=register" | ||
} | ||
} | ||
] | ||
} | ||
else: # login | ||
data = { | ||
'Messages': [ | ||
{ | ||
"From": { | ||
"Email": "[email protected]", | ||
"Name": "Wadium" | ||
}, | ||
"To": [ | ||
{ | ||
"Email": email, | ||
"Name": "" | ||
} | ||
], | ||
"TemplateID": 2118280, # 2118400 for signup, 2118280 for signin | ||
"TemplateLanguage": True, | ||
"Subject": "Sign in to Wadium", | ||
"Variables": { | ||
"callback_uri": f"https://www.wadium.shop/callback/email?token={token}&operation=login" | ||
} | ||
} | ||
] | ||
} | ||
result = mailjet.send.create(data=data) | ||
try: | ||
if result.json()['Messages'][0]['Status'] == 'success': | ||
time = datetime.strptime(result.headers['date'], '%a, %d %b %Y %H:%M:%S %Z') | ||
return True, time | ||
except KeyError: | ||
pass | ||
return False, None |
Oops, something went wrong.