diff --git a/denote/forms.py b/denote/forms.py index f68148f..db5bf4f 100644 --- a/denote/forms.py +++ b/denote/forms.py @@ -21,6 +21,7 @@ from django.forms import ModelForm from django import forms from denote.models import * +from datetime import datetime class NoteForm(ModelForm): text = forms.CharField(widget=forms.Textarea(attrs={'rows':'20', 'cols':'150'})) @@ -28,10 +29,13 @@ class NoteForm(ModelForm): class Meta: model = Note - exclude = ('author', 'transformed_text', 'long_summary', 'short_summary', 'created_date', 'modified_date', 'category', 'visibility') + exclude = ('author', 'transformed_text', 'long_summary', 'short_summary', 'created_date', 'modified_date', 'category') class UserForm(ModelForm): + + password = forms.CharField(required=False) + def __init__(self, *args, **kwargs): super(UserForm, self).__init__(*args, **kwargs) self.fields['first_name'].label = 'Name' @@ -39,4 +43,4 @@ class UserForm(ModelForm): class Meta: model = User - fields = ('first_name', 'username', 'password') + fields = ('first_name', 'username', 'password', 'home_notes_visibility') diff --git a/denote/models.py b/denote/models.py index 43556ff..e68b17c 100644 --- a/denote/models.py +++ b/denote/models.py @@ -33,6 +33,7 @@ from search import Search class User(AbstractUser): hidden_categories = models.TextField(blank=True) + home_notes_visibility = models.IntegerField(default=0, choices=[(0, 'private'), (1, 'registered'), (2, 'public')]) def getPreference(self, name): if name == 'hidden_categories': @@ -58,6 +59,16 @@ class Category(models.Model): name = models.CharField(max_length=50, unique=True, blank=False) class Note(models.Model): + + PRIVATE = 0 + REGISTERED = 1 + PUBLIC = 2 + + VISIBILITY = ((PRIVATE, 'Private'), + (REGISTERED, 'Registered'), + (PUBLIC, 'Public') + ) + author = models.ForeignKey(User, null=False, on_delete=models.CASCADE) title = models.CharField(max_length=100, blank=False) long_summary = models.CharField(max_length=255) @@ -66,7 +77,7 @@ class Note(models.Model): transformed_text = models.TextField() created_date = models.DateTimeField() modified_date = models.DateTimeField() - visibility = models.IntegerField(default=0) + visibility = models.IntegerField(default=PRIVATE, choices=VISIBILITY) category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL) def _wrap(self, text, limit, max_limit): @@ -124,7 +135,10 @@ class Note(models.Model): @receiver(post_save, sender=Note) def post_save_note_signal(sender, **kwargs): s = Search() - s.edit_note(kwargs['instance'].id) + if kwargs['created']: + s.index_note(kwargs['instance'].id) + else: + s.edit_note(kwargs['instance'].id) @receiver(pre_delete, sender=Note) def pre_delete_note_signal(sender, **kwargs): diff --git a/denote/static/css/denote.css b/denote/static/css/denote.css index 7737b72..d2f86f0 100644 --- a/denote/static/css/denote.css +++ b/denote/static/css/denote.css @@ -111,7 +111,7 @@ div#categories div.name img margin : 1em; } -#main_panel .note .title a +.note .title a { color: black; text-decoration: none; diff --git a/denote/static/js/denote.js b/denote/static/js/denote.js index c83d1e6..4ddd085 100644 --- a/denote/static/js/denote.js +++ b/denote/static/js/denote.js @@ -96,6 +96,7 @@ function category_setup() category = categories.childNodes[i]; if (category.nodeType != Node.ELEMENT_NODE) continue; categoryId = category.getAttribute("category_id"); + if (categoryId == null) continue; hide = false; for(a=0; a Dénote{% if user.get_full_name|length != 0 %} - {{ user.get_full_name }}{% endif %} + {% block head %} {% endblock %} @@ -9,8 +13,9 @@ -
Settings Disconnect
-
{% csrf_token %}
+
{% if authenticated %}Settings Disconnect
+ {% endif %} +
{% csrf_token %}


diff --git a/denote/templates/base_user.html b/denote/templates/base_user.html index 0e0bf19..7a7f4fd 100644 --- a/denote/templates/base_user.html +++ b/denote/templates/base_user.html @@ -1,9 +1,8 @@ {% extends "base.html" %} {% block head %} {% endblock %} {% block left %} diff --git a/denote/templates/public_note.html b/denote/templates/public_note.html new file mode 100644 index 0000000..4614d52 --- /dev/null +++ b/denote/templates/public_note.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} + +{% block left %} +{% for note in public_notes %} +
+
+
{{ note.title }}
+
{{ note.created_date }}
+
{{ note.short_summary }}
+
+
+{% endfor %} +{% endblock %} + +{% block content %} +
+
{{ note.title }}
+
{{ note.transformed_text|safe }}
+
+{% endblock %} diff --git a/denote/templates/public_notes.html b/denote/templates/public_notes.html new file mode 100644 index 0000000..42c4518 --- /dev/null +++ b/denote/templates/public_notes.html @@ -0,0 +1,26 @@ +{% extends "base.html" %} + +{% block left %} +{% for note in public_notes %} +
+
+
{{ note.title }}
+
{{ note.created_date }}
+
{{ note.short_summary }}
+
+
+{% endfor %} +{% endblock %} + +{% block content %} +{% for note in notes %} +
+ +
{{ note.modified_date }}
+
{{ note.long_summary }}
+
+{% endfor %} +{% if notes|length == 0 %} +Any note +{% endif %} +{% endblock %} diff --git a/denote/templates/user_index.html b/denote/templates/user_index.html index 10693b9..f63090a 100644 --- a/denote/templates/user_index.html +++ b/denote/templates/user_index.html @@ -12,4 +12,7 @@
{{ note.long_summary }}
{% endfor %} +{% if notes|length == 0 %} +Any note +{% endif %} {% endblock %} diff --git a/denote/templates/user_note.html b/denote/templates/user_note.html index 539e0e2..9a0d0c2 100644 --- a/denote/templates/user_note.html +++ b/denote/templates/user_note.html @@ -2,12 +2,16 @@ {% block content %}
- {% if note != None %} + {% if note and note_form != None %}
{{ note.title }}
- {% if note.category != None %} + {% else %} +
{{ note.title }}
+ {% endif %} + {% if note and note.category != None %}
{{ note.category.name }}
{% endif %}
{{ note.transformed_text|safe }}
+ {% if note and note_form != None %}
{% csrf_token %}
{% csrf_token %} @@ -21,54 +25,7 @@
- Markdown syntax

- - - - - -
-
_italic_
italic
-
**bold**
bold
-
~~line through~~
line through
-
~underline~
underline
-
>Citation

-
-	    * Unordered list
-	    * Second element
-	  
-
    -
  • Unordered list -
  • Second element -
-
-	    1. Ordered list
-	    1. Second element
-	  
-
    -
  1. Ordered list -
  2. Second element -
-
![Picture](https://bits.wikimedia.org/images/wikimedia-button.png)
Picture
-
#[Inline Picture](https://bits.wikimedia.org/images/wikimedia-button.png)
Picture
-
[Link](http://www.wikipedia.org)
Link

-
-	    Code : 4 whitespaces ahead
-	  
-
-
# Title # or
-	    Title
-	    =====
-

Title

-
## Sub title ## or
-	    Sub title
-	    ---------
-

Sub title

-
### Sub sub title ###
-

Sub sub title

-
-
- {% else %} + {% else %}
{% csrf_token %} {{ note_form.as_p }} @@ -81,7 +38,7 @@
-
+ {% endif %} Markdown syntax

@@ -131,6 +88,6 @@
- {% endif %} +
{% endblock %} diff --git a/denote/urls.py b/denote/urls.py index f0b35fd..65595b4 100644 --- a/denote/urls.py +++ b/denote/urls.py @@ -28,6 +28,7 @@ urlpatterns = patterns('', url(r'^user/edit$','denote.views.edit_user', name='edit_user'), url(r'^note/add$', 'denote.views.add_note', name='add_note'), url(r'^note/(\d+)$', 'denote.views.note', name='note'), + url(r'^note/(\d+)/(\d+)$', 'denote.views.public_note', name='public_note'), url(r'^category/edit/(\d)$','denote.views.edit_category', name='edit_category'), url(r'^preferences$', 'denote.views.preferences', name='preferences'), url(r'^search$', 'denote.views.search', name='search'), diff --git a/denote/views.py b/denote/views.py index 41c4c6d..2fd340a 100644 --- a/denote/views.py +++ b/denote/views.py @@ -21,7 +21,7 @@ import os from datetime import datetime -from django.http import HttpResponseRedirect, HttpResponse, Http404 +from django.http import HttpResponseRedirect, HttpResponse, Http404, HttpResponseForbidden from django.contrib.auth.decorators import login_required from django.contrib.auth import authenticate, login, logout from django.shortcuts import render @@ -115,6 +115,11 @@ def edit_user(request): return render(request, 'edit_user.html', c) def _prepare_note_context(user): + if not user.is_authenticated(): + return { + 'authenticated' : False, + } + categories = Category.objects.filter(author=user.id).order_by('name') notes_by_category = [] need_refresh = False @@ -134,6 +139,7 @@ def _prepare_note_context(user): context = { 'user': user, + 'authenticated' : True, 'notes_by_category': notes_by_category, 'categories': categories, 'notes_without_category': notes_without_category, @@ -167,7 +173,8 @@ def add_note(request): if 'cancel' in request.POST: return HttpResponseRedirect('/') else: - form = NoteForm() + note = Note(visibility=user.home_notes_visibility) + form = NoteForm(instance=note) context = _prepare_note_context(user) context['note_form'] = form @@ -201,6 +208,36 @@ def note(request, note_id): return render(request, 'user_note.html', context) +def public_note(request, user_id, note_id): + user = request.user + + try: + note = Note.objects.get(pk=note_id, author=user_id) + except: + raise Http404 + + if note is None: + raise Http404 + + if not user or not user.is_authenticated(): + if note.visibility != Note.PUBLIC: + return HttpResponseForbidden() + else: + if note.visibility == Note.PRIVATE and\ + user_id != user.id: + return HttpResponseForbidden() + + if user.is_authenticated(): + public_notes = Note.objects.filter(author=user_id, visibility__gte=Note.REGISTERED).order_by('-modified_date') + else: + public_notes = Note.objects.filter(author=user_id, visibility__gte=Note.PUBLIC).order_by('-modified_date') + + context = _prepare_note_context(user) + context['note'] = note + context['public_notes'] = public_notes + + return render(request, 'public_note.html', context) + @login_required def edit_category(request, category_id): user = request.user @@ -237,7 +274,6 @@ def preferences(request): else: raise Http404 -@login_required def search(request): context = _prepare_note_context(request.user) @@ -251,11 +287,17 @@ def search(request): s = Search() note_list = s.search(text) - notes = Note.objects.filter(pk__in=note_list, author=request.user) - context['notes'] = notes - context['note_form'] = NoteForm() + if request.user.is_authenticated(): + notes = Note.objects.filter(pk__in=note_list, author=request.user) - return render(request, 'user_index.html', context) + context['notes'] = notes + context['note_form'] = NoteForm() + + return render(request, 'user_index.html', context) + else: + notes = Note.objects.filter(pk__in=note_list, visibility__gte=Note.PUBLIC) + context['notes'] = notes + return render(request, 'public_notes.html', context) @login_required def generate_search_index(request):