From c0121b9b7f5041f6434a2e2dd24d3c68ed84b582 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Thu, 31 Jan 2013 20:08:20 +0200 Subject: Initial commit --- notes/__init__.py | 0 notes/models.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++ notes/templates/edit.html | 31 +++++++++++++++++++++++ notes/templates/index.html | 27 ++++++++++++++++++++ notes/templates/view.html | 15 +++++++++++ notes/tests.py | 16 ++++++++++++ notes/views.py | 51 +++++++++++++++++++++++++++++++++++++ 7 files changed, 203 insertions(+) create mode 100644 notes/__init__.py create mode 100644 notes/models.py create mode 100644 notes/templates/edit.html create mode 100644 notes/templates/index.html create mode 100644 notes/templates/view.html create mode 100644 notes/tests.py create mode 100644 notes/views.py (limited to 'notes') diff --git a/notes/__init__.py b/notes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notes/models.py b/notes/models.py new file mode 100644 index 0000000..2cd5ca6 --- /dev/null +++ b/notes/models.py @@ -0,0 +1,63 @@ +import re +from datetime import datetime +from django.db import models +from django.contrib.auth.models import User +from django.db.models.signals import post_save +from django.dispatch import receiver + +class Tag(models.Model): + title = models.CharField(max_length=128) + + class Meta: + ordering = ['title'] + +class Note(models.Model): + user = models.ForeignKey(User) + shared = models.BooleanField(default=True) + title = models.CharField(max_length=140, null=True) + tags = models.ManyToManyField(Tag, null=True, blank=True) + + def get_date(self): + return self.version_set.all()[0].created_at + + def get_user(self): + pass + + def content(self): + try: + return self.version_set.latest().content + except Version.DoesNotExist: + return '' + + def updated_at(self): + return self.version_set.latest().created_at + + def get_absolute_url(self): + return '/notes/%d/' % self.pk + + class Meta: + ordering = ['-id'] + +class Version(models.Model): + note = models.ForeignKey(Note) + user = models.ForeignKey(User) + content = models.TextField() + created_at = models.DateTimeField(default=datetime.now()) + + class Meta: + get_latest_by = 'created_at' + +class Attachment(models.Model): + content = models.FileField(upload_to='uploads') + note = models.ForeignKey(Note) + +@receiver(post_save, sender=Version) +def version_saved(sender, instance, created, **kwargs): + + tags = re.findall('#(\w+)', instance.content) + + for t in tags: + tag = Tag.objects.get_or_create(title=t)[0] + instance.note.tags.add(tag) + + instance.note.save() diff --git a/notes/templates/edit.html b/notes/templates/edit.html new file mode 100644 index 0000000..6d2ec1e --- /dev/null +++ b/notes/templates/edit.html @@ -0,0 +1,31 @@ +{% extends request.is_ajax|yesno:"blank.html,index.html" %} + +{% block content %} +
+
+ Notes +

New Note

+ Done +
+
+
+ {% csrf_token %} + {{ form }} + + {% if form.is_bound %} + Delete + {% endif %} +
+
+
+ + +{% endblock content %} diff --git a/notes/templates/index.html b/notes/templates/index.html new file mode 100644 index 0000000..c996511 --- /dev/null +++ b/notes/templates/index.html @@ -0,0 +1,27 @@ +{% extends request.is_ajax|yesno:"blank.html,default.html" %} + +{% block content %} +
+
+ Tags +

Notes

+ New +
+
+ + +
+ +
+
+
+ +{% endblock content %} diff --git a/notes/templates/view.html b/notes/templates/view.html new file mode 100644 index 0000000..a6d3473 --- /dev/null +++ b/notes/templates/view.html @@ -0,0 +1,15 @@ +{% extends request.is_ajax|yesno:"blank.html,index.html" %} +{% load markup %} + +{% block content %} +
+
+ Notes +

{{ note.title }}

+ Edit +
+
+

{{ version.content|restructuredtext }}

+
+
+{% endblock content %} diff --git a/notes/tests.py b/notes/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/notes/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/notes/views.py b/notes/views.py new file mode 100644 index 0000000..386b0c0 --- /dev/null +++ b/notes/views.py @@ -0,0 +1,51 @@ +from django import forms +from django.shortcuts import render, redirect +from notes.models import Note, Attachment, Tag, Version + +class NoteForm(forms.Form): + title = forms.CharField() + content = forms.CharField(widget=forms.Textarea(attrs={'rows': 20, + 'style': 'width:100%;height:100%'})) + shared = forms.BooleanField() + attachment = forms.FileField(required=False) + +def edit(request, note_id=None): + + note = Note(user_id=1) + + if note_id: + note = Note.objects.get(pk=note_id) + + if request.method == 'POST': + form = NoteForm(request.POST, request.FILES) + + if not form.is_valid(): + return render(request, 'edit.html', {'form': form}) + + note.title = form.cleaned_data.get('title') + note.save() + + version = Version(note=note, user_id=1) + version.content = form.cleaned_data.get('content') + version.shared = form.cleaned_data.get('shared') + version.save() + + return render(request, 'view.html', {'note': note, 'version': version}) + + form = NoteForm(initial={'content': note.content, 'shared': note.shared}) + + return render(request, 'edit.html', {'form': form}) + +def index(request, tag_id=None): + notes = Note.objects.filter(user_id=1) + + if tag_id: + notes = notes.filter(tags__pk=tag_id) + + tags = Tag.objects.distinct() + return render(request, 'index.html', {'notes': notes, 'tags': tags}) + +def view(request, note_id): + note = Note.objects.get(pk=note_id) + version = note.version_set.latest() + return render(request, 'view.html', {'note': note, 'version': version}) -- cgit v1.2.3