Skip to content
Merged
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
27 changes: 27 additions & 0 deletions JiYouMCC/0021/0021.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# 第 0021 题: 通常,登陆某个网站或者 APP,需要使用用户名和密码。密码是如何加密后存储起来的呢?请使用 Python 对密码加密。
import os
from hashlib import sha256
from hmac import HMAC

SALT_LENGTH = 8


def encrypt_password(password, salt=None):
if salt is None:
salt = os.urandom(SALT_LENGTH)
if isinstance(password, unicode):
password = password.encode('UTF-8')
result = password
for i in xrange(8):
result = HMAC(result, salt, sha256).digest()
return salt + result


def validate_password(hashed, input_password):
return hashed == encrypt_password(input_password, salt=hashed[:SALT_LENGTH])


password = 'secret'
encrypt = encrypt_password(password)
print validate_password(encrypt, password)
21 changes: 21 additions & 0 deletions JiYouMCC/0022/0022.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# 第 0022 题: iPhone 6、iPhone 6 Plus 早已上市开卖。请查看你写得 第 0005 题的代码是否可以复用。
# using PIL in http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow
# 我承认我是先看到22才看到5的
from PIL import Image


def change_image_size(image_path='0005.jpg', size=(1136, 640)):
im = Image.open(image_path)
size = (size[1], size[0]) if im.size[1] > im.size[0] else size
im.thumbnail(size, Image.ANTIALIAS)
im.save('result-' + image_path)

change_image_size('0005-r.jpg')
change_image_size('0005.jpg')

# ip6
change_image_size(image_path='0005.jpg', size=(1334, 750))

# ip6plus
change_image_size(image_path='0005.jpg', size=(1920, 1080))
17 changes: 17 additions & 0 deletions JiYouMCC/0023/0023.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
第 0023 题: 使用 Python 的 Web 框架,做一个 Web 版本 留言簿 应用。
---------------------------------------

以前在sea上弄过一个差不多的:http://jillyou.sinaapp.com/guest/
不过sea弄django syncdb比较痛苦,所有sql拼接出的
直接django真够傻瓜的……

代码版本用的sqlite3,本地调试的时候用的mysql

'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'guestbook',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
Binary file added JiYouMCC/0023/guestbook/db.sqlite3
Binary file not shown.
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions JiYouMCC/0023/guestbook/guestbook/commits/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.contrib import admin
from models import Message

admin.site.register(Message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations


class Migration(migrations.Migration):
dependencies = []
operations = [
migrations.CreateModel(
name='Message',
fields=[
('id', models.AutoField(
verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name', models.CharField(max_length=30)),
('message', models.TextField(max_length=65535)),
('date', models.DateTimeField()),
],
options={
},
bases=(models.Model,),
),
]
Empty file.
10 changes: 10 additions & 0 deletions JiYouMCC/0023/guestbook/guestbook/commits/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.db import models


class Message(models.Model):
name = models.CharField(max_length=30)
message = models.TextField(max_length=65535)
date = models.DateTimeField()

def __unicode__(self):
return self.name + ':' + self.message[:25]
1 change: 1 addition & 0 deletions JiYouMCC/0023/guestbook/guestbook/commits/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from django.test import TestCase
36 changes: 36 additions & 0 deletions JiYouMCC/0023/guestbook/guestbook/commits/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
import datetime
from models import Message


def guestbook(request):
try:
name = request.COOKIES["name"]
except:
name = u"这家伙连名字也没有"
return render(request, 'guestbook.html', {'commits': selectTextMsg(), 'name': name})


def guesttalk(request):
response = HttpResponseRedirect("/guest")
if request.method == "GET":
name = request.GET.get('name', '')
commit = request.GET.get('commits', '')
date = datetime.datetime.now()
if len(name) > 0 and len(commit) > 0 and len(name) < 41 and len(commit) < 4096:
response.set_cookie("name", name.encode('utf8'))
insertTextMsg(name, date, commit)
return response


def insertTextMsg(user, date, message):
message = Message(name=user,
message=message,
date=date,)
message.save()


def selectTextMsg():
return Message.objects.all().order_by("-date")
41 changes: 41 additions & 0 deletions JiYouMCC/0023/guestbook/guestbook/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SECRET_KEY = '+m8si5lo@6467j75%wz#z+cvsxl=)u_c(2-o^&+&^++*-48t$5'
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'guestbook.commits',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'guestbook.urls'
WSGI_APPLICATION = 'guestbook.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'zh-cn'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'guestbook/template'),
)
25 changes: 25 additions & 0 deletions JiYouMCC/0023/guestbook/guestbook/template/guestbook.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<html>
<head>
<meta charset="utf-8">
<title>简易留言本</title>
</head>
<body>
<h1>简易留言本</h1>
<form action="/guesttalk" method="get">
名字:<input type="text" name="name" maxlength ="40" value="{{ name }}" />
<br>
<textarea name="commits" maxlength="1000" rows="10" cols="80" ></textarea>
<br>
<input type="submit">
<input type="reset">
</form>
<div>
{% for commit in commits %}
<div>
<h3>{{ commit.date|date:"Y-n-j H:i:s" }} {{ commit.name }}</h2>
</div>
<div style="word-break:break-all">{{ commit.message|escape|linebreaks|wordwrap:"75" }}</div>
{% endfor %}
</div>
</body>
</html>
9 changes: 9 additions & 0 deletions JiYouMCC/0023/guestbook/guestbook/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.conf.urls import patterns, include, url
from django.contrib import admin
from commits.views import guestbook, guesttalk

urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^guest/', guestbook),
url(r'^guesttalk', guesttalk),
)
5 changes: 5 additions & 0 deletions JiYouMCC/0023/guestbook/guestbook/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "guestbook.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
10 changes: 10 additions & 0 deletions JiYouMCC/0023/guestbook/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "guestbook.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
8 changes: 8 additions & 0 deletions JiYouMCC/0024/0024.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
第 0024 题: 使用 Python 的 Web 框架,做一个 Web 版本 TodoList 应用。
---------------------------------------------------
![任务列表](https://raw.githubusercontent.com/JiYouMCC/python/master/JiYouMCC/0024/0024.png)

admin/admin
- 无限呆蠢的UI请忽略,这个实在是艺术活儿,原来还想把任务DragDrop到状态下和Jire的Kanban一样,没心思折腾html5了
- 其实还加了duedate和createdate,不高兴整template了,不然弄起来就像any.do似的
- 排序UI不高兴整了,就是Models.orderby的问题
Binary file added JiYouMCC/0024/0024.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added JiYouMCC/0024/todoList/db.sqlite3
Binary file not shown.
10 changes: 10 additions & 0 deletions JiYouMCC/0024/todoList/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "todoList.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
44 changes: 44 additions & 0 deletions JiYouMCC/0024/todoList/static/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
label {
width: 50%;
display:inline-block;
text-align: right;
}

h1 {
text-align: center;
}

.helptext {
display: block;
text-align: right;
}

.b {
text-align:center;
}

table {
width: 100%;
text-align: center;
}

tr {
vertical-align:top;
}

.list {
width: 300px;
border-style: solid;
border-width: 1px;
text-align: center;
}

.next,.pre {
display: inline-block;
width:70px;
}

.detail{
display: inline-block;
width:150px;
}
64 changes: 64 additions & 0 deletions JiYouMCC/0024/todoList/templates/process.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<html>
<head>
<title>{{ user_name }}的任务列表</title>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static "global.css" %}" />
</head>
<body>
<h1>{{ user_name }}的任务列表</h1>
<div class='helptext'>我不是{{ user_name }}, 我要<a href="logout">退出</a></div>
<form action="/" method="post">
{% csrf_token %}
<table>
<tr>
<th>添加新的任务</th>
</tr>
<tr>
<td>
<input type="text" name="detail" style="width:900px" />
</td>
</tr>
<tr>
<td><input type="submit" value="添加" /></td>
</tr>
</table>
</form>
<table>
<tr>
{% for item in status %}
<th width="300px">{{ item.status }}</th>
{% endfor %}
</tr>
<tr>
{% for item in lists %}
<td width="300px" align="center">
{% for i in item %}
<div class='list'>
{% if not i.status = firststatus %}
<div class='pre'>
<form action="/" method="post">
{% csrf_token %}
<input type="hidden" name="pre" value="{{ i.id }}" />
<input type="submit" value="上一步">
</form>
</div>
{% endif %}
<div class='detail' title='{{ i.createdate|date:"Y-n-j H:i:s" }}'>
{{ i.detail}}
</div>
{% if not i.status = laststatus %}
<div class='next' >
<form action="/" method="post">
{% csrf_token %}
<input type="hidden" name="next" value="{{ i.id }}" />
<input type="submit" value="下一步">
</form>
</div>
{% endif %}
</div>
{% endfor %}
</td>
{% endfor %}
</tr>
</table>
</body>
23 changes: 23 additions & 0 deletions JiYouMCC/0024/todoList/templates/registration/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<html>
<head>
<title>登录</title>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static "global.css" %}" />
</head>
<body>
<h1>登录</h1>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<div class='b'>
<input type="submit" value="登录" />
<input type="hidden" name="next" value="{{ next|escape }}" />
</div>
</form>
<form action="/register">
<div class='b'>
<input type="submit" value="注册">
</div>
</form>
</body>
</html>
Loading