diff --git a/article/__pycache__/admin.cpython-36.pyc b/article/__pycache__/admin.cpython-36.pyc index 138dee6..7c3c43a 100644 Binary files a/article/__pycache__/admin.cpython-36.pyc and b/article/__pycache__/admin.cpython-36.pyc differ diff --git a/article/__pycache__/models.cpython-36.pyc b/article/__pycache__/models.cpython-36.pyc index 433674b..4db1440 100644 Binary files a/article/__pycache__/models.cpython-36.pyc and b/article/__pycache__/models.cpython-36.pyc differ diff --git a/article/__pycache__/views.cpython-36.pyc b/article/__pycache__/views.cpython-36.pyc index ff3fc18..c6e8e2b 100644 Binary files a/article/__pycache__/views.cpython-36.pyc and b/article/__pycache__/views.cpython-36.pyc differ diff --git a/article/admin.py b/article/admin.py index 994e347..7eca4d5 100644 --- a/article/admin.py +++ b/article/admin.py @@ -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) diff --git a/article/migrations/0017_remove_article_author.py b/article/migrations/0017_remove_article_author.py new file mode 100644 index 0000000..bf3a0f1 --- /dev/null +++ b/article/migrations/0017_remove_article_author.py @@ -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', + ), + ] diff --git a/article/migrations/__pycache__/0017_remove_article_author.cpython-36.pyc b/article/migrations/__pycache__/0017_remove_article_author.cpython-36.pyc new file mode 100644 index 0000000..4261bfd Binary files /dev/null and b/article/migrations/__pycache__/0017_remove_article_author.cpython-36.pyc differ diff --git a/article/models.py b/article/models.py index ad3db8c..0b46fbd 100644 --- a/article/models.py +++ b/article/models.py @@ -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) @@ -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':"标题"}), } @@ -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}), } diff --git a/article/templates/article/detail.html b/article/templates/article/detail.html index 0791168..52c8a4d 100644 --- a/article/templates/article/detail.html +++ b/article/templates/article/detail.html @@ -105,7 +105,7 @@

{% if art %}{{art.title}}{% endif %}

message: '{{ error|escape }}', },{ // settings - type: 'warning', + type: 'danger', delay: 5000, timer: 1000, animate: { diff --git a/article/templates/article/publish.html b/article/templates/article/publish.html index 67fb5d9..8d0258c 100644 --- a/article/templates/article/publish.html +++ b/article/templates/article/publish.html @@ -44,7 +44,7 @@

{{operation}}文章

message: '{{ error|escape }}', },{ // settings - type: 'warning', + type: 'danger', delay: 5000, timer: 1000, animate: { @@ -60,7 +60,7 @@

{{operation}}文章

{% csrf_token %} -

{{article_form.section}} - {{article_form.title}} - {{article_form.author}}

+

{{article_form.section}} - {{article_form.title}}

上传封面图片 : {{article_form.cover}}

正文

diff --git a/article/views.py b/article/views.py index d7ddf35..f86f373 100644 --- a/article/views.py +++ b/article/views.py @@ -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. @@ -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) @@ -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':'发布'}) diff --git a/db.sqlite3 b/db.sqlite3 index 93f04a5..d6d464c 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/edconline/__pycache__/views.cpython-36.pyc b/edconline/__pycache__/views.cpython-36.pyc index 6bed60e..0b41233 100644 Binary files a/edconline/__pycache__/views.cpython-36.pyc and b/edconline/__pycache__/views.cpython-36.pyc differ diff --git a/edconline/views.py b/edconline/views.py index b697538..79b7abf 100644 --- a/edconline/views.py +++ b/edconline/views.py @@ -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}) diff --git a/static/upload/1440x900.jpg b/static/upload/1440x900.jpg new file mode 100644 index 0000000..bd56eaf Binary files /dev/null and b/static/upload/1440x900.jpg differ diff --git a/static/upload/1920x1200_1.jpg b/static/upload/1920x1200_1.jpg new file mode 100644 index 0000000..062c03d Binary files /dev/null and b/static/upload/1920x1200_1.jpg differ diff --git a/static/upload/1920x1200_1_epcZwyW.jpg b/static/upload/1920x1200_1_epcZwyW.jpg new file mode 100644 index 0000000..062c03d Binary files /dev/null and b/static/upload/1920x1200_1_epcZwyW.jpg differ diff --git a/static/upload/Sunshine_1920x1080.png b/static/upload/Sunshine_1920x1080.png new file mode 100644 index 0000000..6e64e11 Binary files /dev/null and b/static/upload/Sunshine_1920x1080.png differ diff --git a/templates/base.html b/templates/base.html index cdbf050..30e2f90 100644 --- a/templates/base.html +++ b/templates/base.html @@ -107,8 +107,23 @@

{% if title %}{{title}}{% endif %}

- {% endifnotequal %} + + +
+ + + + + + +
+
+
diff --git a/templates/index.html b/templates/index.html index 5aed8cb..49b82e1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -16,6 +16,12 @@

{% if title %}{{title}}{% endif %}

{% block content %} + {% if keywords %} +
+ 物志 › {{keywords}} + +
+ {% endif %} {% for i in arts %}
diff --git a/userpage/__pycache__/admin.cpython-36.pyc b/userpage/__pycache__/admin.cpython-36.pyc index 708c75f..2c3399e 100644 Binary files a/userpage/__pycache__/admin.cpython-36.pyc and b/userpage/__pycache__/admin.cpython-36.pyc differ diff --git a/userpage/__pycache__/models.cpython-36.pyc b/userpage/__pycache__/models.cpython-36.pyc index e41f2d4..b683636 100644 Binary files a/userpage/__pycache__/models.cpython-36.pyc and b/userpage/__pycache__/models.cpython-36.pyc differ diff --git a/userpage/__pycache__/views.cpython-36.pyc b/userpage/__pycache__/views.cpython-36.pyc index 6e50130..c34e16b 100644 Binary files a/userpage/__pycache__/views.cpython-36.pyc and b/userpage/__pycache__/views.cpython-36.pyc differ diff --git a/userpage/admin.py b/userpage/admin.py index 2a6354c..2cc5dab 100644 --- a/userpage/admin.py +++ b/userpage/admin.py @@ -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) diff --git a/userpage/migrations/0007_point.py b/userpage/migrations/0007_point.py new file mode 100644 index 0000000..16d2d19 --- /dev/null +++ b/userpage/migrations/0007_point.py @@ -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)), + ], + ), + ] diff --git a/userpage/migrations/__pycache__/0007_point.cpython-36.pyc b/userpage/migrations/__pycache__/0007_point.cpython-36.pyc new file mode 100644 index 0000000..1bbdd64 Binary files /dev/null and b/userpage/migrations/__pycache__/0007_point.cpython-36.pyc differ diff --git a/userpage/models.py b/userpage/models.py index 04a8357..4614387 100644 --- a/userpage/models.py +++ b/userpage/models.py @@ -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. @@ -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: @@ -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() diff --git a/userpage/templates/userpage/login.html b/userpage/templates/userpage/login.html index c91c0ca..1d665d6 100644 --- a/userpage/templates/userpage/login.html +++ b/userpage/templates/userpage/login.html @@ -39,6 +39,7 @@ 一个月内自动登陆 忘记密码?

+

@@ -52,7 +53,7 @@ $.notify({ message: '{{ error|escape }}', },{ - type: 'warning', + type: 'danger', delay: 5000, timer: 1000, animate: { diff --git a/userpage/templates/userpage/profile.html b/userpage/templates/userpage/profile.html index 626af9a..ad038c1 100644 --- a/userpage/templates/userpage/profile.html +++ b/userpage/templates/userpage/profile.html @@ -1,5 +1,6 @@ {% extends "base.html"%} {% load timetonow %} +{% load displayName %} {% block header %} 物志 › {{request.user}} @@ -23,7 +24,7 @@ message: '{{ error|escape }}', },{ // settings - type: 'warning', + type: 'danger', delay: 5000, timer: 1000, animate: { @@ -85,7 +86,10 @@ 消息提醒   {% ifnotequal unread 0 %}{{unread}}{% endifnotequal %}
  • - 编辑资料 + 金币明细 +
  • +
  • + 修改资料
  • {% endifequal %} @@ -137,7 +141,16 @@ {% endfor %}
    - +
    +

    + {% for i in points %} +
    +

    {{i.record_time|timetonow}},{{i.event}},{{i.point_record}}

    +
    + {% endfor %} +
    + +
    {# 编辑资料 #}

    diff --git a/userpage/templates/userpage/register.html b/userpage/templates/userpage/register.html index 00f5e94..e617ce9 100644 --- a/userpage/templates/userpage/register.html +++ b/userpage/templates/userpage/register.html @@ -52,7 +52,7 @@

    - +

    @@ -67,7 +67,7 @@ $.notify({ message: '{{ error|escape }}', },{ - type: 'warning', + type: 'danger', delay: 5000, timer: 1000, animate: { diff --git a/userpage/templatetags/__init__.py b/userpage/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/userpage/templatetags/__pycache__/__init__.cpython-36.pyc b/userpage/templatetags/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..5da0d32 Binary files /dev/null and b/userpage/templatetags/__pycache__/__init__.cpython-36.pyc differ diff --git a/userpage/templatetags/__pycache__/displayName.cpython-36.pyc b/userpage/templatetags/__pycache__/displayName.cpython-36.pyc new file mode 100644 index 0000000..acbe23b Binary files /dev/null and b/userpage/templatetags/__pycache__/displayName.cpython-36.pyc differ diff --git a/userpage/templatetags/displayName.py b/userpage/templatetags/displayName.py new file mode 100644 index 0000000..7f8d52b --- /dev/null +++ b/userpage/templatetags/displayName.py @@ -0,0 +1,9 @@ +from django import template +from django.template.defaultfilters import stringfilter + +register = template.Library() + +@register.filter(name='displayName') +@stringfilter +def displayName(value, arg): + return eval('value.get_'+arg+'_display()') diff --git a/userpage/views.py b/userpage/views.py index b9e8f6f..87fe095 100644 --- a/userpage/views.py +++ b/userpage/views.py @@ -21,7 +21,7 @@ def profile(request, username): inst_user = User.objects.get(username=username) inst_profile = Profile.objects.get(user=inst_user) - arts = Article.objects.filter(author_id=inst_profile) + arts = Article.objects.filter(author_id=inst_profile).filter(visible="Y") comments = Comment.objects.filter(user=inst_profile).filter(invalid='N') if username == request.user.username: @@ -29,7 +29,7 @@ def profile(request, username): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) - + if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() @@ -54,8 +54,11 @@ def profile(request, username): inst_profile.save() # end notification + # point record + point = Point.objects.filter(user=inst_user).order_by('-record_time') - return render(request, 'userpage/profile.html', {'member':inst_user,'arts':arts,'comments':comments,'user_form': user_form,'profile_form': profile_form,'notice':msgs,'unread':unread}) + + return render(request, 'userpage/profile.html', {'member':inst_user,'arts':arts,'comments':comments,'user_form': user_form,'profile_form': profile_form,'notice':msgs,'points':point,'unread':unread}) else: return render(request, 'userpage/profile.html', {'member':inst_user,'arts':arts,'comments':comments}) @@ -72,20 +75,28 @@ def userlogin(request): post_check_code = request.POST.get('check_code') session_check_code = request.session['check_code'] user = authenticate(request, username=username, password=password) - print(request.environ,request.path) + if user is not None: if post_check_code.lower() == session_check_code.lower() : login(request, user) if request.POST.get('auto_login'): request.session.set_expiry(60 * 60 * 24 *30) - return redirect(request.path) + return redirect(request.POST['referer']) else: errors.append('请输入正确的验证码!') else: errors.append('用户名不存在或密码错误!') return render(request, 'userpage/login.html', {'errors':errors}) else: - return render(request,'userpage/login.html') + if 'HTTP_REFERER' in request.environ: + refer = request.environ['HTTP_REFERER'] + if '/userpage/register/' not in refer: + pass + else: + refer = '/' + else: + refer = '/' + return render(request,'userpage/login.html',{'referer':refer}) def userlogout(request): logout(request) @@ -94,10 +105,8 @@ def userlogout(request): def register(request): errors = [] - if request.method == 'GET': - obj = forms.RegisterForm() # return render(request,'register.html',{'form':obj}) - elif request.method == 'POST': + if request.method == 'POST': # print(request.POST) obj = forms.RegisterForm(request.POST) post_check_code = request.POST.get('check_code') @@ -107,8 +116,7 @@ def register(request): if post_check_code.lower() == session_check_code.lower(): # values = obj.clean() data = obj.cleaned_data - print(data) - # models.User.objects.create( + #print(data) username= data.get('username') password= data.get('pwd') email= data.get('email') @@ -117,12 +125,28 @@ def register(request): u.set_password(password) u.save() - return redirect('/userpage/login/') + point=10 + updatepoint = Point.objects.create(user=u,point_record=point,event="register") + + user = authenticate(request, username=username, password=password) + if user is not None: + login(request, user) + + return redirect(request.POST['referer']) else: errors = obj.errors print(errors) - - return render(request,'userpage/register.html',{'form':obj}) + else: + if 'HTTP_REFERER' in request.environ: + refer = request.environ['HTTP_REFERER'] + if '/userpage/register/' not in refer: + pass + else: + refer = '/' + else: + refer = '/' + obj = forms.RegisterForm() + return render(request,'userpage/register.html',{'form':obj,'referer':refer})