diff options
Diffstat (limited to '97suifangqa/apps/sciblog')
-rw-r--r-- | 97suifangqa/apps/sciblog/fixtures/initial_data.json | 4 | ||||
-rw-r--r-- | 97suifangqa/apps/sciblog/models.py | 38 | ||||
-rw-r--r-- | 97suifangqa/apps/sciblog/urls.py | 25 | ||||
-rw-r--r-- | 97suifangqa/apps/sciblog/views.py | 52 |
4 files changed, 95 insertions, 24 deletions
diff --git a/97suifangqa/apps/sciblog/fixtures/initial_data.json b/97suifangqa/apps/sciblog/fixtures/initial_data.json index dc7daec..6038ffe 100644 --- a/97suifangqa/apps/sciblog/fixtures/initial_data.json +++ b/97suifangqa/apps/sciblog/fixtures/initial_data.json @@ -248,8 +248,8 @@ "sample": [], "indicators": [ 3, - 1, - 2 + 2, + 1 ], "type": 1 } diff --git a/97suifangqa/apps/sciblog/models.py b/97suifangqa/apps/sciblog/models.py index d6a63d4..8c8d0cf 100644 --- a/97suifangqa/apps/sciblog/models.py +++ b/97suifangqa/apps/sciblog/models.py @@ -330,6 +330,43 @@ class BaseLine(models.Model): # }}} +# UserCollection {{{ +class UserCollection(models.Model): + """ + record the collections/catches for user + 记录用户收藏的"注释"/"文章",以及标注看懂了的文章 + """ + user = models.OneToOneField(User, verbose_name=u"用户", + related_name="user_collection") + # collect annotation + # -> BlogAnnotation.collected_by + #collectedAnnotation = models.ManyToManyField("BlogAnnotation", + # verbose_name=u"收藏的注释", + # related_name="collected_annotations",) + lastCollectAnnotationTime = models.DateTimeField(u"上次收藏注释时间", + null=True, blank=True) + # catch blog + # -> SciBlog.catched_by + #catchedBlog = models.ManyToManyField("SciBlog", + # verbose_name=u"收藏的文章", + # related_name="catched_blogs",) + lastCatchBlogTime = models.DateTimeField(u"上次读懂文章时间", + null=True, blank=True) + # collect blog + # -> SciBlog.collected_by + #collectedBlog = models.ManyToManyField("SciBlog", + # verbose_name=u"收藏的文章", + # related_name="collected_blogs",) + lastCollectBlogTime = models.DateTimeField(u"上次收藏文章时间", + null=True, blank=True) + + class Meta: + verbose_name_plural = u"用户收藏信息" + + def __unicode__(self): + return "< UserCollection: for %s >" % self.user.username +# UserCollection }}} + admin.site.register([ Source, @@ -340,4 +377,5 @@ admin.site.register([ BaseLine, ResultContent, BlogAnnotation, + UserCollection, ]) diff --git a/97suifangqa/apps/sciblog/urls.py b/97suifangqa/apps/sciblog/urls.py index 722984b..47559d4 100644 --- a/97suifangqa/apps/sciblog/urls.py +++ b/97suifangqa/apps/sciblog/urls.py @@ -1,7 +1,10 @@ +# -*- coding: utf-8 -*- + from django.conf.urls import patterns, url -from .models import SciBlog, BlogAnnotation -from .views import * +from sciblog.models import SciBlog, BlogAnnotation +from sciblog.views import * + blog_blocks = '|'.join(['source','abstract', 'knowledge','follow', 'results', 'comment','refrences', 'test','results-detail']) urlpatterns = patterns('', @@ -11,7 +14,17 @@ urlpatterns = patterns('', url(r'^query/?$', query, name='query_blogs'), url(r'^collection/?$', blog_collection, name='blog_collection'), url(r'^(\d+)/(%s)?/?$' % blog_blocks, blog_detail, name='blog_detail'), - url(r'^(\d+)/collect/?$', add_user_to_m2m, name='blog_collect', kwargs={'m2m':'collected_by'}), - url(r'^(\d+)/understand/?$', add_user_to_m2m, name='blog_understood', kwargs={'m2m':'catched_by'}), - url(r'^annotation/(\d+)/collect/?$', add_user_to_m2m, name='annotation_collect', kwargs={'m2m':'collected_by', 'model': BlogAnnotation}), - ) + url(r'^(?P<objid>\d+)/collect/?$', + add_user_to_m2m, + name='blog_collect', + kwargs={'m2m':'collected_by', 'model': SciBlog}), + url(r'^(?P<objid>\d+)/understand/?$', + add_user_to_m2m, + name='blog_understood', + kwargs={'m2m':'catched_by', 'model': SciBlog}), + url(r'^annotation/(?P<objid>\d+)/collect/?$', + add_user_to_m2m, + name='annotation_collect', + kwargs={'m2m':'collected_by', 'model': BlogAnnotation}), +) + diff --git a/97suifangqa/apps/sciblog/views.py b/97suifangqa/apps/sciblog/views.py index 7c6a4e7..1bd7cf8 100644 --- a/97suifangqa/apps/sciblog/views.py +++ b/97suifangqa/apps/sciblog/views.py @@ -1,15 +1,19 @@ # -*- coding: utf-8 -*- -import json from django.http import HttpResponse, HttpResponseNotFound from django.views.generic.list_detail import object_detail from django.shortcuts import render, get_object_or_404 from django.contrib.auth.decorators import login_required +from django.utils.timezone import utc -from .models import SciBlog, ResultContent -from .forms import SciBlogSearchForm, ProperNounSearchForm +from sciblog import models as sm +from sciblog.forms import SciBlogSearchForm, ProperNounSearchForm from info.forms import QuerySearchForm + +import datetime import itertools +import json + def blog_detail(request, blogid, block): u''' @@ -18,10 +22,10 @@ def blog_detail(request, blogid, block): ''' block = block or "source" template = 'sciblog/blog_detail_%s.html' % block - blog = get_object_or_404(SciBlog, id=blogid) + blog = get_object_or_404(sm.SciBlog, id=blogid) blockid = block - + # 用户已经收藏? collected = request.user.is_authenticated() and blog.collected_by.filter(id=request.user.id) @@ -94,7 +98,7 @@ def query(request): (hasppnouns, ppnouns) = generator_has_content(limit(objects_of_sqs(ppnounsqs),10)) return render(request, 'sciblog/blog_list.html', locals()) else: - (has_blogs, blogs) = generator_has_content(limit(SciBlog.objects.all(),10)) + (has_blogs, blogs) = generator_has_content(limit(sm.SciBlog.objects.all(),10)) return render(request, 'sciblog/blog_list.html', locals()) @@ -109,34 +113,50 @@ def blog_collection(request): @login_required -def add_user_to_m2m(request, objid, m2m ='collected_by', model = SciBlog): +def add_user_to_m2m(request, objid, m2m='collected_by', model=sm.SciBlog): u''' 响应收藏按钮和'懂了'按钮的点击 ''' - + result = {"error":False, 'added':True} result['id'] = objid result['model'] = model._meta.verbose_name_plural + model_name = model.__name__ user = request.user try: - obj = model.objects.get(id=objid) - m2m = getattr(obj, m2m) - if not m2m.filter(id = user.id): + obj = model.objects.get(id=objid) + model_m2m = getattr(obj, m2m) + if not model_m2m.filter(id=user.id): # 还没有收藏或点击懂了 - m2m.add(user) + model_m2m.add(user) + result['added'] = True + # utc time + now_utc = datetime.datetime.utcnow().replace(tzinfo=utc) + # UserCollection + uc, created = sm.UserCollection.objects.get_or_create(user=user) + if model_name == 'SciBlog': + if m2m == 'collected_by': + uc.lastCollectBlogTime = now_utc + elif m2m == 'catched_by': + uc.lastCatchBlogTime = now_utc + elif model_name == 'BlogAnnotation': + if m2m == 'collected_by': + uc.lastCollectAnnotationTime = now_utc + # save + uc.save() else: # 已经收藏或点击了懂了 - m2m.remove(user) + model_m2m.remove(user) result['added'] = False - result['times'] = m2m.count() + result['times'] = model_m2m.count() except: result['error'] = True result['added'] = False return HttpResponse(json.dumps(result), - mimetype = 'application/json') + mimetype = 'application/json') def show_result(request, resultid): - result = get_object_or_404(ResultContent, id=resultid) + result = get_object_or_404(sm.ResultContent, id=resultid) blog = result.blog return render(request, 'sciblog/blog_detail_results-detail.html', locals()) |