Dénote

Dénote Git Source Tree

Root/denote/models.py

1# -*- coding: utf-8 -*-
2"""
3 Copyright 2015 Grégory Soutadé
4
5 This file is part of Dénote.
6
7 Dénote is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Dénote is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Dénote. If not, see <http://www.gnu.org/licenses/>.
19"""
20
21from datetime import datetime
22import re
23
24from django.db import models
25from django.contrib.auth.models import AbstractUser
26from django.http import HttpResponse
27
28import markdown2
29
30class User(AbstractUser):
31 hidden_categories = models.TextField(blank=True)
32
33 def getPreference(self, name):
34 if name == 'hidden_categories':
35 return HttpResponse('{"hidden_categories" : [' + self.hidden_categories + ']}', 'application/json')
36 else:
37 raise Http404
38
39 def setPreference(self, name, value):
40 if name == 'hidden_categories':
41 categories = []
42 for c in value.split(','):
43 if c == '-1' or \
44 (c and Category.objects.filter(id=c).exists()):
45 categories.append(c)
46 self.hidden_categories = ','.join(categories)
47 self.save()
48 return HttpResponse('')
49 else:
50 raise Http404
51
52class Category(models.Model):
53 author = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
54 name = models.CharField(max_length=50, unique=True, blank=False)
55
56class Note(models.Model):
57 author = models.ForeignKey(User, null=False, on_delete=models.CASCADE)
58 title = models.CharField(max_length=100, blank=False)
59 long_summary = models.CharField(max_length=255)
60 short_summary = models.CharField(max_length=255)
61 text = models.TextField(blank=False)
62 transformed_text = models.TextField()
63 created_date = models.DateTimeField()
64 modified_date = models.DateTimeField()
65 visibility = models.IntegerField(default=0)
66 category = models.ForeignKey(Category, null=True, on_delete=models.SET_NULL)
67
68 def _wrap(self, text, limit, max_limit):
69 if len(text) < limit: return text
70
71 lower_limit = upper_limit = limit
72
73 while text[lower_limit-1] != ' ' and\
74 lower_limit > 1:
75 lower_limit -= 1
76
77 while text[upper_limit-1] != ' ' and\
78 upper_limit < len(text):
79 upper_limit += 1
80
81 lower = limit - lower_limit
82 upper = upper_limit - limit
83
84 if lower > max_limit and upper > max_limit:
85 cur_limit = limit + max_limit
86 else:
87 if lower < upper:
88 cur_limit = limit - lower
89 else:
90 cur_limit = limit + upper
91
92 if cur_limit and text[cur_limit-1] == ' ':
93 cur_limit -= 1
94
95 return text[:cur_limit] + '...'
96
97 def _summarize(self):
98 # Remove markup
99 self.long_summary = re.sub(r'<[^>]+>', '', self.transformed_text)
100 self.long_summary = re.sub(r'&[^;]+;', '', self.long_summary)
101 # Remove return
102 self.long_summary = re.sub(r'\n', ' ', self.long_summary)
103 self.long_summary = re.sub(r'\r', ' ', self.long_summary)
104 # Remove duplicated spaces
105 self.long_summary = re.sub(r' [ ]+', ' ', self.long_summary)
106 self.long_summary = self.long_summary.strip()
107 self.short_summary = self.long_summary[:]
108
109 self.long_summary = self._wrap(self.long_summary, 100, 5)
110 self.short_summary = self._wrap(self.short_summary, 30, 5)
111
112 def save(self):
113 self.modified_date = datetime.now()
114 self.transformed_text = markdown2.markdown(self.text, extras=['fenced-code-blocks'])
115 self._summarize()
116 super(Note, self).save()
117
118def manage_category(user, cat_name):
119 category = None
120 if cat_name:
121 category = Category.objects.filter(name=cat_name)
122 # Create a new one
123 if not category:
124 if len(cat_name) > 50: cat_name = cat_name[:50]
125 category = Category(author=user, name=cat_name)
126 category.save()
127 else:
128 category = category[0]
129 return category

Archive Download this file

Branches

Tags