aboutsummaryrefslogtreecommitdiffstats
path: root/notes/models.py
diff options
context:
space:
mode:
authorFilipp Lepalaan <f@0x00.co>2013-01-31 20:08:20 +0200
committerFilipp Lepalaan <f@0x00.co>2013-01-31 20:08:20 +0200
commitc0121b9b7f5041f6434a2e2dd24d3c68ed84b582 (patch)
treee3e8d0a05c45dabcc2a0399f902c6a59c92a9f4f /notes/models.py
parente613dc2b8b17e6f58a9666bee03578bb7ffc4067 (diff)
downloadopus-c0121b9b7f5041f6434a2e2dd24d3c68ed84b582.tar.gz
opus-c0121b9b7f5041f6434a2e2dd24d3c68ed84b582.tar.bz2
opus-c0121b9b7f5041f6434a2e2dd24d3c68ed84b582.zip
Initial commit
Diffstat (limited to 'notes/models.py')
-rw-r--r--notes/models.py63
1 files changed, 63 insertions, 0 deletions
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()