Skip to content

Commit

Permalink
Merge pull request #69 from AnjaneyuluBatta505/history#25
Browse files Browse the repository at this point in the history
History
  • Loading branch information
chaitu210 committed Jul 18, 2016
2 parents 6bd503a + 12b32f3 commit 39e7122
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
</div>
</div>
{% endfor %}
<div class="form-group">
<label>History:</label>
<div class="controls">
<select name="history" class="form-control">
<option value=""> Please choose previous edit </option>
{% for history in history_list %}
<option value="{{history.id}}">{{ history.created_at }}</option>
{% endfor %}
</select>
<p class="help-inline"><small>Revert to previous edits</small></p>
</div>
</div>
</div>
<br clear="all">
<div class=" col-md-12 buttons_row text-left">
Expand Down Expand Up @@ -112,5 +124,19 @@


});

// revert to history
$("select[name=history]").change(function(e){
var id = $(this).val()
if(!id){
return false;
}
$.post("", {"history_id": id, "csrfmiddlewaretoken": "{{ csrf_token }}"}, function(response){
if(response.content){
$('[name=content]').val(response.content);
CKEDITOR.instances.id_content.setData(response.content)
}
})
});
</script>
{% endblock %}
32 changes: 23 additions & 9 deletions django_blog_it/django_blog_it/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ class PostEditView(UpdateView):
template_name = "dashboard/blog/new_blog_add.html"
form_class = BlogPostForm

def dispatch(self, request, *args, **kwargs):
if request.POST:
instance = self.get_object()
if request.POST.get("history_id"):
history_post = instance.history.filter(id=request.POST.get("history_id")).last()
if history_post:
return JsonResponse({"content": history_post.content})
return super(PostEditView, self).dispatch(request, *args, **kwargs)

def get_object(self):
return get_object_or_404(Post, slug=self.kwargs['blog_slug'])

Expand All @@ -171,6 +180,7 @@ def form_invalid(self, form):

def form_valid(self, form):
previous_status = self.get_object().status
previous_content = self.get_object().content
self.blog_post = form.save(commit=False)
self.blog_post.user = self.request.user
if self.request.user.is_superuser or get_user_role(self.request.user) != 'Author':
Expand All @@ -188,16 +198,19 @@ def form_valid(self, form):
else:
blog_tag = Tags.objects.create(name=s.strip())
self.blog_post.tags.add(blog_tag)

if self.blog_post.status == previous_status:
self.blog_post.create_activity(
user=self.request.user, content="updated")
else:
if previous_content != self.blog_post.content:
self.blog_post.create_activity(
user=self.request.user,
content="changed status from " +
str(previous_status) + " to " + str(blog_post.status)
)
user=self.request.user, content=previous_content)
# if self.blog_post.status == previous_status:
# self.blog_post.create_activity(
# user=self.request.user, content="updated")
# else:
# self.blog_post.create_activity(
# user=self.request.user,
# content="changed status from " +
# str(previous_status) + " to " + str(blog_post.status)
# )

messages.success(self.request, 'Successfully updated your blog post')
data = {'error': False,
'response': 'Successfully updated your blog post'}
Expand All @@ -216,6 +229,7 @@ def get_context_data(self, **kwargs):
context['blog_name'] = self.get_object()
context['status_choices'] = STATUS_CHOICE,
context['categories_list'] = categories_list
context['history_list'] = self.get_object().history.all()
return context


Expand Down

0 comments on commit 39e7122

Please sign in to comment.