浏览代码

Keywords in DB

Denis Merigoux 9 年之前
父节点
当前提交
dcde6f2960
共有 5 个文件被更改,包括 108 次插入2 次删除
  1. 3 1
      counter/admin.py
  2. 46 0
      counter/migrations/0007_auto_20170113_1603.py
  3. 23 0
      counter/models.py
  4. 26 0
      counter/utils.py
  5. 10 1
      counter/views.py

+ 3 - 1
counter/admin.py

@@ -1,8 +1,10 @@
1 1
 from django.contrib import admin
2 2
 
3 3
 # Register your models here.
4
-from .models import Counter, Reset, Like
4
+from .models import Counter, Reset, Like, Keyword, Hashtag
5 5
 
6 6
 admin.site.register(Counter)
7 7
 admin.site.register(Reset)
8 8
 admin.site.register(Like)
9
+admin.site.register(Keyword)
10
+admin.site.register(Hashtag)

+ 46 - 0
counter/migrations/0007_auto_20170113_1603.py

@@ -0,0 +1,46 @@
1
+# -*- coding: utf-8 -*-
2
+# Generated by Django 1.10.4 on 2017-01-13 16:03
3
+from __future__ import unicode_literals
4
+
5
+from django.db import migrations, models
6
+import django.db.models.deletion
7
+
8
+
9
+class Migration(migrations.Migration):
10
+
11
+    dependencies = [
12
+        ('counter', '0006_counter_sort_by_score'),
13
+    ]
14
+
15
+    operations = [
16
+        migrations.CreateModel(
17
+            name='Hashtag',
18
+            fields=[
19
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20
+            ],
21
+            options={
22
+                'verbose_name': 'hashtags',
23
+            },
24
+        ),
25
+        migrations.CreateModel(
26
+            name='Keyword',
27
+            fields=[
28
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
29
+                ('text', models.CharField(max_length=128, unique=True, verbose_name='texte')),
30
+            ],
31
+            options={
32
+                'verbose_name': 'mot-clé',
33
+                'verbose_name_plural': 'mots-clés',
34
+            },
35
+        ),
36
+        migrations.AddField(
37
+            model_name='hashtag',
38
+            name='keyword',
39
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='counter.Keyword', verbose_name='hashtag'),
40
+        ),
41
+        migrations.AddField(
42
+            model_name='hashtag',
43
+            name='reset',
44
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='counter.Reset', verbose_name='remise à zéro'),
45
+        ),
46
+    ]

+ 23 - 0
counter/models.py

@@ -67,3 +67,26 @@ class Like(models.Model):
67 67
 
68 68
     def __str__(self):
69 69
         return '%s aime %s' % (self.liker, self.reset)
70
+
71
+
72
+class Keyword(models.Model):
73
+    text = models.CharField('texte', max_length=128, unique=True)
74
+
75
+    class Meta:
76
+        verbose_name = 'mot-clé'
77
+        verbose_name_plural = 'mots-clés'
78
+
79
+    def __str__(self):
80
+        return '#%s' % (self.text)
81
+
82
+
83
+class Hashtag(models.Model):
84
+    keyword = models.ForeignKey('Keyword', verbose_name='hashtag')
85
+    reset = models.ForeignKey('Reset', verbose_name='remise à zéro')
86
+
87
+    class Meta:
88
+        verbose_name = 'hashtag'
89
+        verbose_name_plural = 'hashtags'
90
+
91
+    def __str__(self):
92
+        return '%s pour %s' % (self.keyword, self.reset)

+ 26 - 0
counter/utils.py

@@ -0,0 +1,26 @@
1
+import re
2
+import unicodedata
3
+from counter.models import Keyword
4
+
5
+
6
+# Analyse the reason of the reset and retrieve all the keyword objects that it
7
+# contains, creating the objects in the DB on-the-fly if necessary
8
+def parseSeumReason(reason):
9
+    hashtags = re.findall(r'(?<=#)[^#\s]+', reason, re.I)
10
+    hashtags = [removeAccentsToLowercase(u) for u in hashtags]
11
+    keywords = []
12
+    for hashtag in hashtags:
13
+        try:
14
+            keyword = Keyword.objects.get(text=hashtag)
15
+            keywords.append(keyword)
16
+        except Keyword.DoesNotExist:
17
+            keyword = Keyword(text=hashtag)
18
+            keyword.save()
19
+            keywords.append(keyword)
20
+    return keywords
21
+
22
+
23
+def removeAccentsToLowercase(input_str):
24
+    nfkd_form = unicodedata.normalize('NFKD', input_str)
25
+    return u"".join([c.lower() for c in nfkd_form
26
+                     if not unicodedata.combining(c)])

+ 10 - 1
counter/views.py

@@ -1,5 +1,5 @@
1 1
 from django.shortcuts import render
2
-from counter.models import Counter, Reset, Like
2
+from counter.models import Counter, Reset, Like, Keyword, Hashtag
3 3
 from django.contrib.auth.models import User
4 4
 from babel.dates import format_timedelta, format_datetime
5 5
 from datetime import datetime, timedelta
@@ -18,6 +18,7 @@ import math
18 18
 import copy
19 19
 import functools
20 20
 from django.utils import timezone
21
+from counter.utils import parseSeumReason
21 22
 
22 23
 # JSS above this limit will not be displayed on the home page col graph
23 24
 JSS_limit = 7
@@ -275,6 +276,7 @@ def resetCounter(request):
275 276
         data = dict(request.POST)
276 277
 
277 278
         who = Counter.objects.get(pk=int(data['who'][0]))
279
+        reason = data['reason'][0]
278 280
         if 'counter' in data.keys():
279 281
             counter = Counter.objects.get(pk=int(data['counter'][0]))
280 282
         else:
@@ -294,6 +296,13 @@ def resetCounter(request):
294 296
             return HttpResponseRedirect(data['redirect'][0])
295 297
 
296 298
         reset.save()
299
+
300
+        # Now we deal with the hashtags
301
+        keywords = parseSeumReason(reason)
302
+        for keyword in keywords:
303
+            hashtag = Hashtag(reset=reset, keyword=keyword)
304
+            hashtag.save()
305
+
297 306
         # We send the emails only to those who want
298 307
         emails = [u.email for u in Counter.objects.all()
299 308
                   if u.email_notifications]