Skip to content

Commit

Permalink
add user point
Browse files Browse the repository at this point in the history
  • Loading branch information
gothack329 committed Mar 4, 2018
1 parent f6c590d commit af1b606
Show file tree
Hide file tree
Showing 34 changed files with 203 additions and 51 deletions.
Binary file modified article/__pycache__/admin.cpython-36.pyc
Binary file not shown.
Binary file modified article/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file modified article/__pycache__/views.cpython-36.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion article/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Register your models here.
class ArticleAdmin(admin.ModelAdmin):
list_display = ('id','section','title','author','author_id','publish_time','readtime','visible')
list_display = ('id','section','title','author_id','publish_time','readtime','visible')
#search_fields = ('section__name',)
admin.site.register(Article,ArticleAdmin)

Expand Down
17 changes: 17 additions & 0 deletions article/migrations/0017_remove_article_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.0.2 on 2018-03-04 05:45

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('article', '0016_comment_invalid'),
]

operations = [
migrations.RemoveField(
model_name='article',
name='author',
),
]
Binary file not shown.
10 changes: 4 additions & 6 deletions article/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class Article(models.Model):
id=models.AutoField(primary_key=True)
section=models.ForeignKey(Section,on_delete=models.CASCADE,default=0)
author_id = models.ForeignKey(Profile,on_delete=models.CASCADE,default=0)
author = models.CharField(max_length=128,default='')
#author_id = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
publish_time = models.DateTimeField(auto_now_add=True,editable=False)
cover = models.ImageField(upload_to='static/upload/', blank=True, null=True)
title = models.CharField(max_length=1024)
Expand All @@ -55,20 +53,17 @@ class Meta:
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['title','author','author_id','section','tag','cover','detail']
fields = ['title','author_id','section','tag','cover','detail']
error_messages = {
'section':{'required': '请选择主题分区'},
'author':{'required': '请输入作者名称'},
'cover':{'required': '请上传封面图'},
'detail':{'required': '请输入正文内容'},
'title':{'required': '请输入文章标题'},

}
widgets = {
'detail':TinyMCE(attrs={'cols':'100%','rows':30}),
'cover':FileInput(attrs={'style':'width:50%','class':'form-control','placeholder':"封面"}),
'tag':TextInput(attrs={'class':'form-control','placeholder':"添加标签,回车确认",'data-role':'tagsinput'}),
'author':TextInput(attrs={'class':'form-control','placeholder':"署名"}),
'title':TextInput(attrs={'style':'width:50%','class':'form-control','placeholder':"标题"}),
'section':widgets.Select(choices=Section.objects.values_list('id','name'),attrs={'class':'form-control','placeholder':"标题"}),
}
Expand Down Expand Up @@ -97,6 +92,9 @@ class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ('article','user','comment','ip','invalid')
error_messages = {
'user':{'required': '请先登录','invalid_choice':'请先登录!'},
}
widgets = {
'comment':TinyMCE(attrs={'cols':'100%','rows':10}),
}
Expand Down
2 changes: 1 addition & 1 deletion article/templates/article/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ <h1>{% if art %}{{art.title}}{% endif %} </h1>
message: '{{ error|escape }}',
},{
// settings
type: 'warning',
type: 'danger',
delay: 5000,
timer: 1000,
animate: {
Expand Down
4 changes: 2 additions & 2 deletions article/templates/article/publish.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h4>{{operation}}文章</h4>
message: '{{ error|escape }}',
},{
// settings
type: 'warning',
type: 'danger',
delay: 5000,
timer: 1000,
animate: {
Expand All @@ -60,7 +60,7 @@ <h4>{{operation}}文章</h4>
<form method="post" class="form-inline textarea" data-ajax="false" enctype="multipart/form-data" novalidate>
{% csrf_token %}
<input type="hidden" name="author_id" value="{{request.user.id}}">
<p>{{article_form.section}} - {{article_form.title}} - {{article_form.author}}</p>
<p>{{article_form.section}} - {{article_form.title}}</p>
<p>上传封面图片 : {{article_form.cover}}</p>
<span class="sep margintop"></span>
<p>正文</p>
Expand Down
17 changes: 16 additions & 1 deletion article/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.template import RequestContext,Template,Context,loader,defaultfilters
from django.contrib import messages
from .models import *
from userpage.models import *


# Create your views here.
Expand All @@ -23,8 +24,15 @@ def detail(request, article_id):
if comment_form.is_valid():
instance = comment_form.save(commit=False)
instance.ip = request.META['REMOTE_ADDR']

instance.save()

point=1
author = User.objects.get(pk=art.author_id.id)
commenter = User.objects.get(pk=request.user.id)
updatepoint = Point.objects.create(user=author,point_record=point,event="commented")
updatepoint = Point.objects.create(user=commenter,point_record=-point,event="comment")


return redirect('.')
else:
messages.error(request, comment_form.errors)
Expand All @@ -47,12 +55,19 @@ def publish(request):
instance.ip = request.META['REMOTE_ADDR']
art = instance.save()

point=2
u = User.objects.get(pk=instance.author_id.id)
updatepoint = Point.objects.create(user=u,point_record=point,event="publish")
#updatepoint.save()

return redirect('/article/%d/' % (instance.pk,))
else:
messages.error(request, article_form.errors)
else:
article_form = ArticleForm(instance=None)



return render(request,'article/publish.html',{'article_form':article_form,'operation':'发布'})


Expand Down
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified edconline/__pycache__/views.cpython-36.pyc
Binary file not shown.
21 changes: 14 additions & 7 deletions edconline/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,28 @@ def homepage(request):
for i in query:
data[i[0]]=i[1]

if request.user.is_authenticated :# and request.user.has_perm('cmdb.permit')):
username = request.user.username
else:
username = None
#if request.user.is_authenticated :# and request.user.has_perm('cmdb.permit')):
# username = request.user.username
#else:
# username = None

imgs = os.listdir('static/upload/')
cover = random.sample(imgs,1)[0]


if 'catalog' in data:
qset = (Q(section__name=data['catalog']))
arts = Article.objects.filter(qset).order_by('-publish_time')
arts = Article.objects.filter(qset).filter(visible='Y').order_by('-publish_time')
search_keywords = data['catalog']
elif 'keywords' in data:
qset = (Q(title__icontains = data['keywords'])|Q(detail__icontains = data['keywords']))
arts = Article.objects.filter(qset).filter(visible='Y').order_by('-publish_time')
search_keywords = data['keywords']
else:
arts = Article.objects.all().order_by('-publish_time')
arts = Article.objects.filter(visible='Y').order_by('-publish_time')
search_keywords = None

return render(request,'index.html',{'cover':cover,'arts':arts})
return render(request,'index.html',{'cover':cover,'arts':arts,'keywords':search_keywords})



Expand Down
Binary file added static/upload/1440x900.jpg
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 static/upload/1920x1200_1.jpg
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 static/upload/1920x1200_1_epcZwyW.jpg
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 static/upload/Sunshine_1920x1080.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 16 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,23 @@ <h1>{% if title %}{{title}}{% endif %} </h1>
<button class="btn btn-info btn-sidebar" style="width:70%;margin-top: 10px" type="submit" onclick="window.location.href='/userpage/register/'">
注册
</button>

{% endifnotequal %}

<form action="/" method="get" id="searchform" name="searchform">
<div class="input-group" style="margin-top:20px">

<input type="text" class="form-control input-lg" style="height:35px" placeholder="搜索文章..." name="keywords">
<span class="input-group-addon btn btn-primary" id="search">
<span class="glyphicon glyphicon-search" > </span>
</span>

</div>
</form>
<script type="text/javascript">
$("#search").click(function() {//button的click事件
$("#searchform").submit();
});
</script>
</div>

<div class="card">
Expand Down
6 changes: 6 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ <h1>{% if title %}{{title}}{% endif %} </h1>

{% block content %}

{% if keywords %}
<div class="card">
<a href="/">物志</a> › {{keywords}}

</div>
{% endif %}
{% for i in arts %}
<div class="card">
<div class="preview row">
Expand Down
Binary file modified userpage/__pycache__/admin.cpython-36.pyc
Binary file not shown.
Binary file modified userpage/__pycache__/models.cpython-36.pyc
Binary file not shown.
Binary file modified userpage/__pycache__/views.cpython-36.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion userpage/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ class ProfileAdmin(admin.ModelAdmin):
admin.site.register(Profile,ProfileAdmin)



class PointAdmin(admin.ModelAdmin):
list_display = ('user','point_record','record_time','event')
admin.site.register(Point,PointAdmin)
26 changes: 26 additions & 0 deletions userpage/migrations/0007_point.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 2.0.2 on 2018-03-04 12:13

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('userpage', '0006_auto_20180303_2241'),
]

operations = [
migrations.CreateModel(
name='Point',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('point_record', models.IntegerField()),
('record_time', models.DateTimeField(auto_now_add=True)),
('event', models.CharField(choices=[('publish', '发布文章'), ('comment', '发表评论'), ('commented', '文章被评论')], max_length=16)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Binary file not shown.
41 changes: 30 additions & 11 deletions userpage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.forms import ModelForm,Textarea,TextInput,ClearableFileInput,FileInput
from django.db.models.signals import post_save
from django.dispatch import receiver
#from notification.models import Msg


# Create your models here.

Expand All @@ -13,21 +13,12 @@ class Profile(models.Model):
location = models.CharField(max_length=30, blank=True)
#birth_date = models.DateField(null=True, blank=True)
avatar = models.ImageField(upload_to='static/avatar/', default='static/avatar/default.png', blank=True, null=True)
point = models.IntegerField()
point = models.IntegerField(default=0)
unread = models.IntegerField(default=0)

def __str__(self):
return self.user.username

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance,point=10)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()


class UserForm(ModelForm):
class Meta:
Expand All @@ -50,3 +41,31 @@ class Meta:
widgets = {
'avatar':ClearableFileInput(attrs={'class':'form-control','placeholder':"头像"}),
}


class Point(models.Model):
id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
point_record = models.IntegerField()
record_time = models.DateTimeField(auto_now_add=True,editable=False)
event = models.CharField(choices=(('publish','发布文章'),('comment','发表评论'),('commented','文章被评论'),('register','新用户注册')),max_length=16)

def __str__(self):
return '%s %s %d' % (self.user,self.event,self.point_record)


@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()

@receiver(post_save, sender=Point)
def update_user_point(sender, instance, created, **kwargs):
if created:
user = Profile.objects.get(user=instance.user)
user.point += instance.point_record
user.save()
3 changes: 2 additions & 1 deletion userpage/templates/userpage/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
一个月内自动登陆
<span class="pull-right"><a href="#">忘记密码?</a></span>
</p>
<input type="hidden" value='{{referer}}' name="referer">
<p style="text-align:center">
<button class="btn btn-primary" type="submit" >登录</button>
</p>
Expand All @@ -52,7 +53,7 @@
$.notify({
message: '{{ error|escape }}',
},{
type: 'warning',
type: 'danger',
delay: 5000,
timer: 1000,
animate: {
Expand Down
19 changes: 16 additions & 3 deletions userpage/templates/userpage/profile.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "base.html"%}
{% load timetonow %}
{% load displayName %}

{% block header %}
<title> 物志 › {{request.user}} </title>
Expand All @@ -23,7 +24,7 @@
message: '{{ error|escape }}',
},{
// settings
type: 'warning',
type: 'danger',
delay: 5000,
timer: 1000,
animate: {
Expand Down Expand Up @@ -85,7 +86,10 @@
<a href="javascript:;">消息提醒 &nbsp; {% ifnotequal unread 0 %}<span class="badge" style="background-color:#d73a49">{{unread}}</span>{% endifnotequal %}</a>
</li>
<li role="presentation" >
<a href="javascript:;">编辑资料</a>
<a href="javascript:;">金币明细</a>
</li>
<li role="presentation" >
<a href="javascript:;">修改资料</a>
</li>
{% endifequal %}
</ul>
Expand Down Expand Up @@ -137,7 +141,16 @@
{% endfor %}
</div>


<div class="profile-tab" >
<p></p>
{% for i in points %}
<div class="row member_comments">
<p>{{i.record_time|timetonow}},{{i.event}},{{i.point_record}}</p>
</div>
{% endfor %}
</div>


<div class="profile-tab" >
{# 编辑资料 #}
<p></p>
Expand Down
4 changes: 2 additions & 2 deletions userpage/templates/userpage/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<label style="width:100px;text-align:right" for="check_code">验证码:</label>
<input type="text" class="form-control" name="check_code" id="check_code" placeholder="请输入验证码">
</p>

<input type="hidden" name="referer" value="{{referer}}">
<p class="mid">
<button class="btn btn-primary" type="submit" >注册</button>
</p>
Expand All @@ -67,7 +67,7 @@
$.notify({
message: '{{ error|escape }}',
},{
type: 'warning',
type: 'danger',
delay: 5000,
timer: 1000,
animate: {
Expand Down
Empty file.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit af1b606

Please sign in to comment.