Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changyu #3

Open
wants to merge 2 commits into
base: yuna
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added db.sqlite3
Binary file not shown.
Empty file added events/__init__.py
Empty file.
Binary file added events/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added events/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file added events/__pycache__/calendar.cpython-38.pyc
Binary file not shown.
Binary file added events/__pycache__/forms.cpython-38.pyc
Binary file not shown.
Binary file added events/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file added events/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file added events/__pycache__/views.cpython-38.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions events/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions events/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class EventsConfig(AppConfig):
name = 'events'
35 changes: 35 additions & 0 deletions events/calendar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from datetime import datetime, timedelta
from calendar import HTMLCalendar
from .models import Event

class Calendar(HTMLCalendar):
def __init__(self, year=None, month=None):
self.year = year
self.month = month
super(Calendar, self).__init__()

def formatday(self, day, events):
events_per_day = events.filter(start_time__day=day)
d = ''
for event in events_per_day:
d += f'<li> {event.get_html_url} </li>'

if day != 0:
return f"<td><span class='date'>{day}</span><ul class='event_line'> {d} </ul></td>"
return '<td></td>'

def formatweek(self, theweek, events):
week = ''
for d, weekday in theweek:
week += self.formatday(d, events)
return f'<tr> {week} </tr>'

def formatmonth(self, withyear=True):
events = Event.objects.filter(start_time__year=self.year, start_time__month=self.month)

cal = f'<table class="calendar">\n'
cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
cal += f'{self.formatweekheader()}\n'
for week in self.monthdays2calendar(self.year, self.month):
cal += f'{self.formatweek(week, events)}\n'
return cal
16 changes: 16 additions & 0 deletions events/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.forms import ModelForm, DateInput
from .models import Event

class EventForm(ModelForm):
class Meta:
model = Event
widgets = {
'start_time': DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
'end_time': DateInput(attrs={'type': 'date'}, format='%Y-%m-%d'),
}
fields = '__all__'

def __init__(self, *args, **kwargs):
super(EventForm, self).__init__(*args, **kwargs)
self.fields['start_time'].input_formats = ('%Y-%m-%d',)
self.fields['end_time'].input_formats = ('%Y-%m-%d',)
28 changes: 28 additions & 0 deletions events/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.0.6 on 2020-05-30 05:36

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Event',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('start_time', models.DateTimeField(verbose_name='시작시간')),
('end_time', models.DateTimeField(verbose_name='마감시간')),
('title', models.CharField(max_length=50, verbose_name='이벤트 이름')),
('description', models.TextField(verbose_name='상세')),
],
options={
'verbose_name': '이벤트 데이터',
'verbose_name_plural': '이벤트 데이터',
},
),
]
Empty file added events/migrations/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
21 changes: 21 additions & 0 deletions events/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.db import models
from django.urls import reverse

class Event(models.Model):

start_time = models.DateTimeField("시작시간")
end_time = models.DateTimeField("마감시간")
title = models.CharField("이벤트 이름", max_length=50)
description = models.TextField("상세")

class Meta:
verbose_name = "이벤트 데이터"
verbose_name_plural = "이벤트 데이터"

def __str__(self):
return self.title

@property
def get_html_url(self):
url = reverse('edit', args=(self.id,))
return f'<a href="{url}"> {self.title} </a>'
89 changes: 89 additions & 0 deletions events/static/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
h1 {
text-align : center;
font-family: 'Serif';
}

.calendar{
width: 50vw;
font-size: 1rem;
margin : auto;
background: #fff;
border: 1px;
color: #000;
text-align : center;
}

.month {
font-size: 1rem;
}

.date {
font-size: 1rem;
}

.calendar .month {
color : #222222;
font-weight: 800;
}

.calendar th {
padding: 2vw;
text-align: center;
font-size: 1.5rem;
}

.calendar tr {
color: #e66b6b;
font-weight: 700;
text-transform: uppercase;
}

.calendar td {
width: 1rem;
height: 4.5em;
padding: .25em .25em;
transition: background .5s;
}


.event_line {
height: 100%;
padding: 0;
list-style: none;
margin : 0;
}

a {
transition: color .2s;
}

span {
color : black;
}

a:link { color: red;}
a:visited { color: red;}
a:hover { color: blue; text-decoration: none;}

.left {
float : left;
margin : 10px;
}

.right {
float : right;
margin : 10px;
}

.form {
margin: auto;
}

.form input, .form select, .form textarea {
border-radius: 5px;
border: 1px solid black;
outline: none;
background: none;
padding: 5px;
width: 100%;
}
15 changes: 15 additions & 0 deletions events/templates/events.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'events_base.html' %}

{% block content %}
<div class="container">
<br>
<h1>caulion events</h1>
{{calendar}}
<div class="outer">
<a class="btn btn-light left" href="{% url 'calendar' %}?{{ prev_month }}"><span>이전 달</span> </a>
<a class="btn btn-light right" href="{% url 'calendar' %}?{{ next_month }}"><span>다음 달</span> </a>
</div>
</div>

<a class="btn btn-primary right" href="{% url 'new' %}"><span>이벤트 생성</span> </a>
{% endblock %}
18 changes: 18 additions & 0 deletions events/templates/events_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% load static %}
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="{% static 'css/style.css' %}" rel="stylesheet" type="text/css" />
</head>
<header>

</header>

<body>
{% block content%}
{% endblock %}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
16 changes: 16 additions & 0 deletions events/templates/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends 'events_base.html' %}


{% block content %}

<div class="container">
<br>
<form method="POST">
{% csrf_token %}
<div class="form form-table">
{{ form }}
<button type="submit" class="btn btn-primary right"> Submit </button>
</div>
</form>
</div>
{% endblock %}
3 changes: 3 additions & 0 deletions events/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions events/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.contrib import admin
from django.urls import path
import events.views

urlpatterns = [
path('event', events.views.calendar_view, name="calendar"),
path('event/new/', events.views.event, name="new"),
path('event/edit/<int:event_id>', events.views.event, name="edit"),
]
56 changes: 56 additions & 0 deletions events/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django.shortcuts import render, redirect, get_object_or_404, reverse
import datetime
from .models import Event
import calendar
from .calendar import Calendar
from django.utils.safestring import mark_safe
from .forms import EventForm

def calendar_view(request):
today = get_date(request.GET.get('month'))

prev_month_var = prev_month(today)
next_month_var = next_month(today)

cal = Calendar(today.year, today.month)
html_cal = cal.formatmonth(withyear=True)
result_cal = mark_safe(html_cal)

context = {'calendar' : result_cal, 'prev_month' : prev_month_var, 'next_month' : next_month_var}

return render(request, 'events.html', context)

#현재 달력을 보고 있는 시점의 시간을 반환
def get_date(req_day):
if req_day:
year, month = (int(x) for x in req_day.split('-'))
return datetime.date(year, month, day=1)
return datetime.datetime.today()

#현재 달력의 이전 달 URL 반환
def prev_month(day):
first = day.replace(day=1)
prev_month = first - datetime.timedelta(days=1)
month = 'month=' + str(prev_month.year) + '-' + str(prev_month.month)
return month

#현재 달력의 다음 달 URL 반환
def next_month(day):
days_in_month = calendar.monthrange(day.year, day.month)[1]
last = day.replace(day=days_in_month)
next_month = last + datetime.timedelta(days=1)
month = 'month=' + str(next_month.year) + '-' + str(next_month.month)
return month

#새로운 Event의 등록 혹은 수정
def event(request, event_id=None):
if event_id:
instance = get_object_or_404(Event, pk=event_id)
else:
instance = Event()

form = EventForm(request.POST or None, instance=instance)
if request.POST and form.is_valid():
form.save()
return redirect('calendar')
return render(request, 'input.html', {'form': form})
Binary file added home/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added home/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file added home/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file added home/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file added home/__pycache__/views.cpython-38.pyc
Binary file not shown.
Binary file added home/migrations/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions home/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ㅎㅇ
7 changes: 7 additions & 0 deletions home/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from django.urls import path
import home.views

urlpatterns = [
path('', home.views.home, name='home'),
]
2 changes: 2 additions & 0 deletions home/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.shortcuts import render

# Create your views here.
def home(request):
return render(request, 'home.html')
Binary file added myproject/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added myproject/__pycache__/settings.cpython-38.pyc
Binary file not shown.
Binary file added myproject/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file added myproject/__pycache__/wsgi.cpython-38.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions myproject/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'home',
'notice',
'events',
]

MIDDLEWARE = [
Expand Down
4 changes: 3 additions & 1 deletion myproject/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('events.urls')),
path('', include('home.urls')),
]
Binary file added notice/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added notice/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file added notice/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file not shown.