tBKwtWS 6 tahun lalu
induk
melakukan
6a38c875a3

+ 31 - 4
README.md

@@ -39,11 +39,38 @@ Fill in the passwords, don't change the database name, it's hardcoded in pyRot,
 1. `GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO pyrot;`
 1. `GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO pyrot;`
 
-## DB authentication
+## Configuration
 
-For now, set the database authentication details in the following files:
-1. website/website/settings.py
-1. rotbot/bot.py
+1. Set the database authentication details in the following file, for the robot: `rotbot/bot.py`
+1. Create file `website/website/local_settings.py` and fill in the key and password:
+```
+# SECURITY WARNING: keep the secret key used in production secret!
+SECRET_KEY = ''
+
+# SECURITY WARNING: don't run with debug turned on in production!
+DEBUG = True
+
+ALLOWED_HOSTS = ['*']
+
+
+# Database
+# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
+
+DATABASES = {
+    # 'default': {
+    #     'ENGINE': 'django.db.backends.sqlite3',
+    #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+    # }
+    'default': {
+        'ENGINE': 'django.db.backends.postgresql_psycopg2',
+        'NAME': 'website',
+        'USER': 'website',
+        'PASSWORD': '',
+        'HOST': '127.0.0.1',
+        'PORT': '5432',
+    }
+}
+```
 
 ## Create the database tables and static files.
 

+ 3 - 9
rotbot/events/common.py

@@ -90,18 +90,12 @@ class Lastact():
 
 class MessageStatistics():
     def update(self, event, type):
-        if type == 'message':
-            type='m'
-        elif type == 'action':
-            type='a'
-        elif type == 'notice':
-            type='n'
         channel = queries.create_ifnot_onrecord(self, 'channel', event.target)
         user = queries.create_ifnot_onrecord(self, 'user', event.source.nick)
-        if not self.db.one('SELECT id FROM rotbot_message WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s AND type=%(type)s', network_id=self.network.id, channel_id=channel.id ,user_id=user.id, type=type):  # Not on record.
-            self.db.run('INSERT INTO rotbot_message (network_id, channel_id, user_id, type, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, %(type)s, 1)', network_id=self.network.id, channel_id=channel.id ,user_id=user.id, type=type)   # Create record.
+        if not self.db.one('SELECT id FROM rotbot_' + type + ' WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel.id ,user_id=user.id):  # Not on record.
+            self.db.run('INSERT INTO rotbot_' + type + ' (network_id, channel_id, user_id, type_type, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel.id ,user_id=user.id)   # Create record.
         else:   # On record.
-            self.db.run('UPDATE rotbot_message SET amount = amount +1 WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s AND type=%(type)s', network_id=self.network.id, channel_id=channel.id ,user_id=user.id, type=type)  # Increment record.
+            self.db.run('UPDATE rotbot_' + type + ' SET amount = amount +1 WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel.id ,user_id=user.id)  # Increment record.
 
 class Inform():
         def owners(self, connection, message):

+ 1 - 1
rotbot/events/on_join.py

@@ -11,7 +11,7 @@ def process_event(self, connection, event):
     user = queries.create_ifnot_onrecord(self, 'user', event.source.nick)
 
     # Save to join event database.
-    if not self.db.one('SELECT id FROM rotbot_join WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND "user_id"=%(user_id)s', network_id=self.network.id, channel_id=channel.id, user_id=user.id):   # No record yet
+    if not self.db.one('SELECT id FROM rotbot_join WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel.id, user_id=user.id):   # No record yet
         self.db.run('INSERT INTO rotbot_join (network_id, channel_id, user_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel.id, user_id=user.id)   # Create record.
     else:
         self.db.run('UPDATE rotbot_join SET amount = amount + 1 WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel.id, user_id=user.id)   # Update existing record.

+ 30 - 0
website/rotbot/migrations/0003_auto_20191114_1920.py

@@ -0,0 +1,30 @@
+# Generated by Django 2.2.6 on 2019-11-14 18:20
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('rotbot', '0002_auto_20191114_1739'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='MessageTypes',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('type', models.CharField(choices=[('m', 'Message'), ('a', 'Action'), ('n', 'Notice')], max_length=1)),
+            ],
+        ),
+        migrations.RemoveField(
+            model_name='message',
+            name='type',
+        ),
+        migrations.AlterField(
+            model_name='message',
+            name='user',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='rotbot.User'),
+        ),
+    ]

+ 42 - 0
website/rotbot/migrations/0004_auto_20191114_2005.py

@@ -0,0 +1,42 @@
+# Generated by Django 2.2.6 on 2019-11-14 19:05
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('rotbot', '0003_auto_20191114_1920'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='Action',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('amount', models.PositiveIntegerField(default=0)),
+                ('channel', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rotbot.Channel')),
+                ('network', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rotbot.Network')),
+                ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rotbot.User')),
+            ],
+        ),
+        migrations.CreateModel(
+            name='Notice',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('amount', models.PositiveIntegerField(default=0)),
+                ('channel', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rotbot.Channel')),
+                ('network', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rotbot.Network')),
+                ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rotbot.User')),
+            ],
+        ),
+        migrations.DeleteModel(
+            name='MessageTypes',
+        ),
+        migrations.AlterField(
+            model_name='message',
+            name='user',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='rotbot.User'),
+        ),
+    ]

+ 33 - 8
website/rotbot/models.py

@@ -177,14 +177,39 @@ class Message(models.Model):
     amount = models.PositiveIntegerField(
         default=0,
     )
-    TYPE_CHOICES = [
-        ('m', 'Message'),
-        ('a', 'Action'),
-        ('n', 'Notice'),
-    ]
-    type = models.CharField(
-        max_length=1,
-        choices=TYPE_CHOICES,
+
+class Action(models.Model):
+    network = models.ForeignKey(
+        'Network',
+        on_delete=models.PROTECT,
+    )
+    channel = models.ForeignKey(
+        'Channel',
+        on_delete=models.PROTECT,
+    )
+    user = models.ForeignKey(
+        'User',
+        on_delete=models.PROTECT,
+    )
+    amount = models.PositiveIntegerField(
+        default=0,
+    )
+
+class Notice(models.Model):
+    network = models.ForeignKey(
+        'Network',
+        on_delete=models.PROTECT,
+    )
+    channel = models.ForeignKey(
+        'Channel',
+        on_delete=models.PROTECT,
+    )
+    user = models.ForeignKey(
+        'User',
+        on_delete=models.PROTECT,
+    )
+    amount = models.PositiveIntegerField(
+        default=0,
     )
 
 class Kick(models.Model):

+ 24 - 3
website/rotbot/views.py

@@ -6,7 +6,7 @@ from django.contrib.auth.decorators import login_required, permission_required
 from django.forms import modelformset_factory, inlineformset_factory
 
 from website.settings import APPLICATION_NAME
-from .models import Network, Host, Channel, User, Message
+from .models import Network, Host, Channel, User, Message, Action, Notice
 from .forms import NetworkForm, HostForm
 
 def default_keywords(additional_keywords=None):
@@ -22,6 +22,16 @@ def networks(request):
     channels = Channel.objects.all()
     users = User.objects.all()
     messages = Message.objects.all()
+    actions = Action.objects.all()
+    notices = Notice.objects.all()
+
+    total_messages = 0
+    for message in messages:
+        total_messages += message.amount
+    for action in actions:
+        total_messages += action.amount
+    for notice in notices:
+        total_messages += notice.amount
 
     context = {
         'title': 'RotBot',
@@ -32,7 +42,7 @@ def networks(request):
         'network_amount': networks.count(),
         'channel_amount': channels.count(),
         'user_amount': users.count(),
-        'message_amount': messages.count(),
+        'message_amount': total_messages,
     }
     return render(request, 'rotbot/networks.html', context)
 
@@ -42,6 +52,17 @@ def network(request, network_slug):
     channels = Channel.objects.filter(network=network)
     users = User.objects.filter(network=network)
     messages = Message.objects.filter(network=network)
+    actions = Action.objects.filter(network=network)
+    notices = Notice.objects.filter(network=network)
+
+    total_messages = 0
+    for message in messages:
+        print(message.amount)
+        total_messages += message.amount
+    for action in actions:
+        total_messages += action.amount
+    for notice in notices:
+        total_messages += notice.amount
 
     context = {
         'parent_title': 'Networks',
@@ -54,7 +75,7 @@ def network(request, network_slug):
         'network': network,
         'channel_amount': channels.count(),
         'user_amount': users.count(),
-        'message_amount': messages.count(),
+        'message_amount': total_messages,
     }
     return render(request, 'rotbot/network.html', context)
 

+ 26 - 0
website/website/local_settings.py

@@ -0,0 +1,26 @@
+# SECURITY WARNING: keep the secret key used in production secret!
+SECRET_KEY = 'f_nxx)y&fw6*u#dd_du6y9&)1$q+a0!y38+6sk151+6)@1kxge'
+
+# SECURITY WARNING: don't run with debug turned on in production!
+DEBUG = True
+
+ALLOWED_HOSTS = ['*']
+
+
+# Database
+# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
+
+DATABASES = {
+    # 'default': {
+    #     'ENGINE': 'django.db.backends.sqlite3',
+    #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+    # }
+    'default': {
+        'ENGINE': 'django.db.backends.postgresql_psycopg2',
+        'NAME': 'website',
+        'USER': 'website',
+        'PASSWORD': 'oGPnbiqh55QKLhmnKQgS92h74j0e9d6LE58cSsD1',
+        'HOST': '127.0.0.1',
+        'PORT': '5432',
+    }
+}

+ 19 - 21
website/website/settings.py

@@ -12,19 +12,25 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
 
 import os
 
+# Store environment dependant settings in local_settings.py, whuch is untracked by git.
+try:
+    from website.local_settings import *
+except ImportError:
+    pass
+
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 # Quick-start development settings - unsuitable for production
 # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
 
-# SECURITY WARNING: keep the secret key used in production secret!
-SECRET_KEY = 'f_nxx)y&fw6*u#dd_du6y9&)1$q+a0!y38+6sk151+6)@1kxge'
-
-# SECURITY WARNING: don't run with debug turned on in production!
-DEBUG = True
-
-ALLOWED_HOSTS = ['*']
+# # SECURITY WARNING: keep the secret key used in production secret!
+# SECRET_KEY = 'f_nxx)y&fw6*u#dd_du6y9&)1$q+a0!y38+6sk151+6)@1kxge'
+#
+# # SECURITY WARNING: don't run with debug turned on in production!
+# DEBUG = False
+#
+# ALLOWED_HOSTS = ['']
 
 
 # Application definition
@@ -79,20 +85,12 @@ WSGI_APPLICATION = 'website.wsgi.application'
 # Database
 # https://docs.djangoproject.com/en/2.2/ref/settings/#databases
 
-DATABASES = {
-    # 'default': {
-    #     'ENGINE': 'django.db.backends.sqlite3',
-    #     'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
-    # }
-    'default': {
-        'ENGINE': 'django.db.backends.postgresql_psycopg2',
-        'NAME': 'website',
-        'USER': 'website',
-        'PASSWORD': 'oGPnbiqh55QKLhmnKQgS92h74j0e9d6LE58cSsD1',
-        'HOST': '127.0.0.1',
-        'PORT': '5432',
-    }
-}
+# DATABASES = {
+#     'default': {
+#         'ENGINE': 'django.db.backends.sqlite3',
+#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
+#     }
+# }
 
 
 # Password validation