Skip to content

Commit

Permalink
Add weather fucntion
Browse files Browse the repository at this point in the history
  • Loading branch information
yaozeliangtest committed Apr 25, 2020
1 parent e899ad0 commit b9ee686
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 37 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ sqlparse = "==0.3.1"
swapper = "==1.1.2.post1"
whitenoise = "==3.3.1"
django-allauth = "*"
pandas = "==0.24.1"

[requires]
python_version = "3.8"
61 changes: 60 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 51 additions & 7 deletions article/templatetags/article_extras.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from django import template
from django.db.models.aggregates import Count
from ..models import ArticlePost,Category

from django.utils import timezone
import math
import datetime
import requests

register = template.Library()


Expand All @@ -20,11 +24,51 @@ def show_cats_tags(context):
}


@register.inclusion_tag('article/inclusions/_resume.html')
def show_resume():
return {}
@register.inclusion_tag('article/inclusions/_resume.html',takes_context=True)
def show_resume(context):
current_time = datetime.datetime.now().strftime("%Y/%m/%d")
return {'current_time':current_time}


@register.inclusion_tag('article/inclusions/_notification.html',takes_context=True)
def show_notification(context):
url = 'http://api.openweathermap.org/data/2.5/weather?q=Paris,fr&units=imperial&appid=2e37fd2364d867821f298280137eecc0'
r = requests.get(url).json()
paris_weather={}

if r['cod']==200:
paris_weather = {
'city':'Paris',
'temperature' :float("{0:.2f}".format((r['main']['temp']-32)* 5/9)),
'description' : r['weather'][0]['description'],
'icon' : r['weather'][0]['icon'],
'country':r['sys']['country']}

return {'paris_weather':paris_weather}



@register.filter(name='timesince_zh')
def time_since_zh(value):
now = timezone.now()
diff = now - value

if diff.days == 0 and diff.seconds >= 0 and diff.seconds < 60:
return '刚刚'

if diff.days == 0 and diff.seconds >= 60 and diff.seconds < 3600:
return str(math.floor(diff.seconds / 60)) + "分钟前"

if diff.days == 0 and diff.seconds >= 3600 and diff.seconds < 86400:
return str(math.floor(diff.seconds / 3600)) + "小时前"

if diff.days >= 1 and diff.days < 30:
return str(diff.days) + "天前"

if diff.days >= 30 and diff.days < 365:
return str(math.floor(diff.days / 30)) + "个月前"

if diff.days >= 365:
return str(math.floor(diff.days / 365)) + "年前"


@register.inclusion_tag('article/inclusions/_notification.html')
def show_notification():
return {}
Binary file modified db.sqlite3
Binary file not shown.
15 changes: 15 additions & 0 deletions demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import requests

url = 'http://api.openweathermap.org/data/2.5/weather?q=Paris,fr&units=imperial&appid=2e37fd2364d867821f298280137eecc0'
r = requests.get(url).json()
paris_weather={}

if r['cod']==200:
paris_weather = {
'city':'Paris',
'temperature' :float("{0:.2f}".format((r['main']['temp']-32)* 5/9)),
'description' : r['weather'][0]['description'],
'icon' : r['weather'][0]['icon'],
'country':r['sys']['country']}


Binary file added media/avatar/20200424/my_photo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions my_blog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@
]


AUTHENTICATION_BACKENDS = (
# Django 后台可独立于 allauth 登录
'django.contrib.auth.backends.ModelBackend',
# AUTHENTICATION_BACKENDS = (
# # Django 后台可独立于 allauth 登录
# 'django.contrib.auth.backends.ModelBackend',

# 配置 allauth 独有的认证方法,如 email 登录
'allauth.account.auth_backends.AuthenticationBackend',
)
# # 配置 allauth 独有的认证方法,如 email 登录
# 'allauth.account.auth_backends.AuthenticationBackend',
# )



Expand Down
Binary file added static/images/self.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 43 additions & 1 deletion templates/article/inclusions/_notification.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
<div class="row notification">
<!-- <div class="row notification">
<div class="card col-10 ml-md-2 mb-3">
<div class="card-body">
<p class="card-text">
<span><i class="fas fa-bullhorn fa-2x"></i> 持续修改更新中</span>
</p>
</div>
</div>
</div> -->

<div class="row">
<article class="media col-10 ml-md-2 mb-3 ">
<div class="media-left">
<figure class="image is-60x60">
<img src="http://openweathermap.org/img/w/{{ paris_weather.icon }}.png" alt="Image">
</figure>
</div>
<div class="media-content">
<div class="content">
<p>
<span class="title">{{ paris_weather.city }}, {{ paris_weather.country }}</span>
<br>
<span class="subtitle">{{ paris_weather.temperature }} °c, {{ paris_weather.description }}</span>
</p>
</div>
</div>

</article>
</div>
<!-- <div class="row">
<div class="col-10 ml-md-2 mb-1">
<div class="media">
<div class="media-left">
<figure class="image is-80x80">
<img
src="http://openweathermap.org/img/w/{{ paris_weather.icon }}.png"
alt="Image"
/>
</figure>
</div>
<div class="media-body">
<p>
{{ paris_weather.temperature }} °C, {{ paris_weather.city }}
</p>
<p>Today: {{ paris_weather.description }}</p>
</div>
</div>
</div>
</div> -->

1 change: 1 addition & 0 deletions templates/article/inclusions/_resume.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ <h6 class="card-title">About Me

<a href="https://github.com/yaozeliang/">
<i class="fab fa-github" style="color: lightslategrey"></i></a>
{{ current_time }}
</h6>
<!-- <p class="card-text mb-1"><b>所以我们奋力向前,逆水行舟,直至被推回往昔岁月</b> </p> -->
<p class="card-text mb-0"><span><i class="far fa-envelope "></i></span> [email protected]</p>
Expand Down
3 changes: 2 additions & 1 deletion templates/article/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ <h5>
</span>
<span>
<i class="fas fa-calendar-alt" style="color:coral"></i>
{{ article.created|date:'Y-m-d' }}
<!-- {{ article.created|date:'Y-m-d' }} -->
{{ article.created|timesince_zh }}
</span>
</p>
</div>
Expand Down
25 changes: 17 additions & 8 deletions templates/header.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
<!-- 引入notifications的模板标签 -->
{% load notifications_tags %}
{% notifications_unread as unread_count %}

{% load static %}
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<!-- 导航栏商标 -->
<a class="navbar-brand" href="#">YAO Zeliang</a>
<!-- <a class="navbar-brand" href="#"></a> -->

<a class="navbar-brand" href="#">
<span><img src="{% static 'images/self.jpg' %}" width="30" height="30" class="d-inline-block align-top">
YAO Zeliang</span>
</a>

<!-- 导航入口 -->
<div>
<ul class="navbar-nav">
<!-- 条目 -->
<li class="nav-item">
<a class="nav-link" href="{% url 'article:article_list' %}">文章</a>
<li class="nav-item ">
<a class="nav-link " href="{% url 'article:article_list' %}">文章</a>
</li>

<!-- <li class="nav-item"></li>
<a class="nav-link" href="{% url 'article:article_create' %}">写文章</a>
</li> -->


{% if user.is_authenticated %}
<!-- 如果用户已经登录,则显示用户名下拉框 -->
<!-- <li class="nav-item"><img src="User.socialaccount_set.all.0.get_avatar_url" alt=""></li> -->

<li class="nav-item dropdown">
<a
class="nav-link dropdown-toggle"
Expand All @@ -46,9 +51,13 @@
<span class="badge badge-danger">{{ unread_count }}</span>
{% endif %}
</a>
<a class="dropdown-item" href='{% url "userprofile:edit" user.id %}'>个人信息</a>
<!-- <a class="dropdown-item" href='{% url "userprofile:edit" user.id %}'>个人信息</a> -->

{% if user.is_superuser %}
<a class="dropdown-item" href="{% url 'article:article_create' %}"
>写文章</a>
{% endif %}

<a class="dropdown-item" href="#" onclick="user_delete()">删除用户</a>
<a class="dropdown-item" href='{% url "userprofile:logout" %}'>退出登录</a>
</div>
Expand Down
7 changes: 4 additions & 3 deletions templates/userprofile/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@

<br>

<h6>没有账号?<a href='{% url "userprofile:register" %}'>点击注册</a></h6>
<h6>忘记密码了? <a href='{% url "password_reset_recover" %}'>重置密码</a></h6>
<p>没有账号?<a href='{% url "userprofile:register" %}'> 注册</a></p>
<p>忘记密码?<a href='{% url "password_reset_recover" %}'> 重置</a></p>


</div>
</div>
</div>

{% endblock content %}
{% endblock content %}

6 changes: 3 additions & 3 deletions templates/userprofile/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% load static %}
{% load i18n %}
{% load account socialaccount %}
{% block title %} 登录 {% endblock title %}
{% block title %} 注册 {% endblock title %}
{% block content %}
<div class="container">
<div class="row">
Expand Down Expand Up @@ -34,9 +34,9 @@
<div class="col-md-4">
<button type="submit" class="btn btn-primary">提交</button>
<a class="btn btn-info" href="{% provider_login_url 'github' process='login' %}"><span><i class="fab fa-github"></i> Github登录</span></a>

</div>



</form>
</div>
</div>
Expand Down
14 changes: 8 additions & 6 deletions userprofile/urls.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from django.urls import path
from . import views
from .views import user_login,user_logout,user_delete,profile_edit,RegisterView,user_register



app_name = 'userprofile'

urlpatterns = [
# 用户登录
path('login/', views.user_login, name='login'),
path('logout/', views.user_logout, name='logout'),
path('register/', views.user_register, name='register'),
path('delete/<int:id>/', views.user_delete, name='delete'),
path('edit/<int:id>/', views.profile_edit, name='edit'),
path('login/', user_login, name='login'),
path('logout/', user_logout, name='logout'),
path('register/', user_register, name='register'),
# path('register/', RegisterView.as_view(), name='register'),

path('delete/<int:id>/', user_delete, name='delete'),
path('edit/<int:id>/', profile_edit, name='edit'),
]
Loading

0 comments on commit b9ee686

Please sign in to comment.