diff options
-rw-r--r-- | tbc/models.py | 7 | ||||
-rw-r--r-- | tbc/templates/tbc/book-review-details.html | 14 | ||||
-rw-r--r-- | tbc/templates/tbc/book-review.html | 6 | ||||
-rwxr-xr-x | tbc/views.py | 70 |
4 files changed, 88 insertions, 9 deletions
diff --git a/tbc/models.py b/tbc/models.py index 4b474f2..b3ccb30 100644 --- a/tbc/models.py +++ b/tbc/models.py @@ -1,6 +1,7 @@ from django.db import models from django.contrib.auth.models import User from PythonTBC import settings +from django.contrib.admin.models import LogEntry CATEGORY = (("fluid mechanics", "Fluid Mechanics"), @@ -149,3 +150,9 @@ class SampleNotebook(models.Model): def __unicode__(self): notebook = self.proposal.accepted.title or 'notebook' return '%s'%(notebook) + + +class ActivityLog(LogEntry): + proposal_id = models.IntegerField(null=True) + def __unicode__(self): + return 'Activity log for %d' %(proposal_id) diff --git a/tbc/templates/tbc/book-review-details.html b/tbc/templates/tbc/book-review-details.html index e55c23c..f304a66 100644 --- a/tbc/templates/tbc/book-review-details.html +++ b/tbc/templates/tbc/book-review-details.html @@ -33,4 +33,18 @@ <tr><td>Contributor: <td>{{ book.contributor.user.first_name }} {{ book.contributor.user.last_name }} <tr><td>Email: <td>{{ book.contributor.user.email }}<br> </table> +<table class='table'> + <tr> + <th>User</th> + <th>Time</th> + <th>Activity</th> + </tr> +{% for log in logs %} +<tr> + <td> {{ log.user }} </td> + <td> {{ log.action_time }} </td> + <td> {{ log.change_message }} </td> +</tr> +{% endfor %} +<table> {% endblock %} diff --git a/tbc/templates/tbc/book-review.html b/tbc/templates/tbc/book-review.html index 774998c..98d569a 100644 --- a/tbc/templates/tbc/book-review.html +++ b/tbc/templates/tbc/book-review.html @@ -13,4 +13,10 @@ <li><a href="{% url 'tbc:BookReview' book.id %}">{{ book.title }} {{ book.edition }} Edition</a> {% endfor %} </ol> +<center><h3>Reviewed Books</h3></center> +<ol> +{% for book in approved_books %} +<li><a href="{% url 'tbc:BookReview' book.id %}">{{ book.title }} {{ book.edition }} Edition</a></li> +{% endfor %} +<ol> {% endblock %} diff --git a/tbc/views.py b/tbc/views.py index d50beb8..0809cb5 100755 --- a/tbc/views.py +++ b/tbc/views.py @@ -1,8 +1,11 @@ +from django.utils.encoding import force_text +from django.contrib.contenttypes.models import ContentType from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response, redirect from django.views.decorators.csrf import csrf_exempt from django.core.context_processors import csrf from django.contrib.auth import authenticate, login, logout +from django.contrib.admin.models import CHANGE from models import * from tbc.forms import * import os @@ -16,6 +19,19 @@ import json from email.mime.text import MIMEText +def add_log(user, object, flag, message, proposal_id=None): + '''Creates log entry of the user activities.''' + ActivityLog( + user_id=user.id, + content_type_id=ContentType.objects.get_for_model(object).id, + object_id=object.id, + object_repr=force_text(object), + action_flag=flag, + change_message=message, + proposal_id = proposal_id, + ).save() + + def email_send(to,subject,msg): try: smtpObj = smtplib.SMTP('localhost') @@ -122,6 +138,7 @@ def UserLogin(request): curr_user = authenticate(username=username, password=password) if curr_user is not None: login(request, curr_user) + add_log(curr_user, curr_user, CHANGE, 'Logged in') else: form = UserLoginForm() context['form'] = form @@ -149,7 +166,8 @@ def UserRegister(request): if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): - form.save() + user = form.save() + add_log(user, user, CHANGE, 'Registered') return HttpResponseRedirect('/login/?signup=done') else: context = {} @@ -177,6 +195,7 @@ def UserProfile(request): data = form.save(commit=False) data.user = request.user data.save() + add_log(user, user, CHANGE,'Profile entry') return HttpResponseRedirect('/') else: context = {} @@ -200,6 +219,7 @@ def UserLogout(request): user = request.user if user.is_authenticated() and user.is_active: logout(request) + add_log(user, user, CHANGE, 'Logged out') return redirect('/?logout=done') @@ -256,6 +276,7 @@ def UpdatePassword(request): if new_password == confirm: user.set_password(new_password) user.save() + add_log(user, user, CHANGE, 'Password updated') form = UserLoginForm() context['password_updated'] = True context['form'] = form @@ -291,6 +312,8 @@ def SubmitBook(request): context['user'] = curr_user curr_book = Book.objects.order_by("-id")[0] curr_book_id = curr_book.id + proposal_id = Proposal.objects.get(accepted=curr_book) + add_log(curr_user, curr_book, CHANGE, 'Book submitted', proposal_id) return HttpResponseRedirect('/upload-content/'+str(curr_book_id)) else: context.update(csrf(request)) @@ -323,6 +346,13 @@ def SubmitProposal(request): can_submit_new = True if can_submit_new: if request.method == 'POST': + try: + proposal = Proposal.objects.get(id=proposal_id) + except: + proposal = Proposal() + proposal.user = user_profile + proposal.status = 'Pending' + proposal.save() book_titles = request.POST.getlist('title') book_authors = request.POST.getlist('author') book_categories = request.POST.getlist('category') @@ -331,8 +361,12 @@ def SubmitProposal(request): book_editions = request.POST.getlist('edition') book_years = request.POST.getlist('year_of_pub') book_chapters = request.POST.getlist('no_chapters') + textbooks = proposal.textbooks.all() for item in range(3): - tempbook = TempBook() + if textbooks: + tempbook = textbooks[item] + else: + tempbook = TempBook() tempbook.title = book_titles[item] tempbook.author = book_authors[item] tempbook.category = book_categories[item] @@ -342,11 +376,9 @@ def SubmitProposal(request): tempbook.year_of_pub = book_years[item] tempbook.no_chapters = book_chapters[item] tempbook.save() - proposal = Proposal() - proposal.user = user_profile - proposal.save() - for book in list(TempBook.objects.all())[-3:]: - proposal.textbooks.add(book) + if not textbooks: + proposal.textbooks.add(tempbook) + add_log(curr_user, proposal, CHANGE, 'Proposed Books', proposal.id) return HttpResponseRedirect('/?proposal=submitted') else: book_forms = [] @@ -393,6 +425,7 @@ def ReviewProposals(request, proposal_id=None, textbook_id=None): proposal.status = "samples" proposal.accepted = new_book proposal.save() + add_log(request.user, proposal, CHANGE, 'Proposal accepted', proposal.id) return HttpResponse("Approved") else: new_proposals = Proposal.objects.filter(status="pending") @@ -422,6 +455,7 @@ def DisapproveProposal(request, proposal_id=None): context.update(csrf(request)) proposal = Proposal.objects.get(id=proposal_id) if request.method == 'POST': + add_log(request.user, proposal, CHANGE, 'Sample disapproved', proposal_id) changes_required = request.POST['changes_required'] subject = "Python-TBC: Corrections Required in the sample notebook" message = "Hi, "+proposal.user.user.first_name+",\n"+\ @@ -436,11 +470,12 @@ def DisapproveProposal(request, proposal_id=None): return render_to_response('tbc/disapprove-sample.html', context) -def AllotBook(requrest, proposal_id=None): +def AllotBook(request, proposal_id=None): context = {} proposal = Proposal.objects.get(id=proposal_id) proposal.status = "book alloted" proposal.save() + add_log(request.user, proposal, CHANGE, 'Book alloted', proposal_id) return HttpResponseRedirect("/book-review/?book_alloted=done") @@ -451,6 +486,7 @@ def RejectProposal(request, proposal_id=None): if request.method == 'POST': proposal.status = 'rejected' proposal.save() + add_log(request.user, proposal, CHANGE, 'Proposal rejected', proposal.id) remarks = request.POST['remarks'] subject = "Python-TBC: Rejection of Proposal" message = "Dear "+proposal.user.user.first_name+"\nYour proposal has been\ @@ -468,6 +504,7 @@ def SubmitSample(request, proposal_id=None, old_notebook_id=None): context.update(csrf(request)) if request.method == "POST": curr_proposal = Proposal.objects.get(id=proposal_id) + add_log(request.user, curr_proposal, CHANGE, 'Sample Submitted', curr_proposal.id) if old_notebook_id: old_notebook = SampleNotebook.objects.get(id=old_notebook_id) old_notebook.proposal = curr_proposal @@ -523,6 +560,8 @@ def UpdateBook(request): data.save() context.update(csrf(request)) context['form'] = book_form + proposal = Proposal.objects.get(accepted=book_to_update) + add_log(current_user, book_to_update, CHANGE, 'Book updated', proposal.id) return HttpResponseRedirect('/update-content/'+str(book_to_update.id)) else: book_form = BookForm() @@ -562,6 +601,8 @@ def SubmitCode(request): screenshot.book = curr_book screenshot.save() book = Book.objects.order_by("-id")[0] + proposal = Proposal.objects.get(accepted=book) + add_log(user, curr_book, CHANGE, 'Chapters and Screenshots added', proposal.id) subject = "Python-TBC: Book Submission" message = "Hi "+curr_book.reviewer.name+",\n"+\ "A book has been submitted on the Python TBC interface.\n"+\ @@ -604,6 +645,8 @@ def UpdateContent(request, book_id=None): screenshot.image = request.FILES['image'+str(i)] screenshot.book = current_book screenshot.save() + proposal = Proposal.objects.get(accepted=current_book) + add_log(user, current_book, CHANGE, 'book updated', proposal.id) subject = "Python-TBC: Book Updated" message = "Hi "+current_book.reviewer.name+",\n"+\ "Submission for a book has been updated on the Python TBC interface.\n"+\ @@ -615,7 +658,7 @@ def UpdateContent(request, book_id=None): "ISBN: "+current_book.isbn+"\n"+\ "Follow the link to review the book: \n"+\ "http://dev.fossee.in/book-review/"+str(current_book.id) - email_send(current_book.reviewer.email, subject, message) + email_send(current_book.reviewer.email, subject, message, current_book.id) return HttpResponseRedirect('/?update_book=done') else: context.update(csrf(request)) @@ -679,6 +722,9 @@ def BookReview(request, book_id=None): book = Book.objects.get(id=book_id) chapters = Chapters.objects.filter(book=book) images = ScreenShots.objects.filter(book=book) + proposal = Proposal.objects.get(accepted=book) + logs = ActivityLog.objects.filter(proposal_id=proposal.id) + context['logs'] = logs context['chapters'] = chapters context['images'] = images context['book'] = book @@ -691,6 +737,8 @@ def BookReview(request, book_id=None): if 'mail_notify' in request.GET: context['mail_notify'] = True books = Book.objects.filter(approved=False) + approved_books = Book.objects.filter(approved=True) + context['approved_books'] = approved_books context['books'] = books context['reviewer'] = request.user context.update(csrf(request)) @@ -707,6 +755,8 @@ def ApproveBook(request, book_id=None): book = Book.objects.get(id=book_id) book.approved = True book.save() + proposal = Proposal.objects.get(accepted=book) + add_log(user, book, CHANGE, 'Book approved', proposal.id) file_path = os.path.abspath(os.path.dirname(__file__)) zip_path = "/".join(file_path.split("/")[1:-2]) zip_path = "/"+zip_path+"/Python-Textbook-Companions/" @@ -755,8 +805,10 @@ def NotifyChanges(request, book_id=None): context = {} if is_reviewer(request.user): book = Book.objects.get(id=book_id) + proposal = Proposal.objects.get(accepted=book) if request.method == 'POST': changes_required = request.POST['changes_required'] + add_log(request.user, book, CHANGE, 'Changes notification', proposal.id) subject = "Python-TBC: Corrections Required" message = "Hi, "+book.contributor.user.first_name+",\n"+\ "Book titled, "+book.title+" requires following changes: \n"+\ |