adaptive-learning / proso-apps

Django modules for an adaptive practice system (currently used by http://slepemapy.cz/ or https://anatom.cz/)
MIT License
2 stars 7 forks source link

Feature/proso events #178

Closed hkarasek closed 8 years ago

hkarasek commented 8 years ago

Add basic support for proso_events to proso-apps.

papousek commented 8 years ago

Okřídlená prasátka létající okolo pláčou :-(

papousek commented 8 years ago

Firstly, update remote in local copy.

$ git remote update

create a backup branch:

$ git checkout origin/feature/proso-events
$ git checkout -b feature/proso-events-old
$ git push origin feature/proso-events-old

Your branch contains uggly merge commit (in fact we want to merge only one commit here):

history

Now, clean the history of the original branch:

$ git checkout -
$ git reset origin/master --hard
$ git cherry-pick acd184d

history

The commit changes 2 things: (1) instantiation of the logger and (2) formatting. We want to do only (1). If you want to change formatting, please do it in separate commits. For now, we remove formatting changes from the commit:

$ git reset HEAD^
$ git add -p
diff --git a/proso_common/models.py b/proso_common/models.py
index e723b4f..13daf8d 100644
--- a/proso_common/models.py
+++ b/proso_common/models.py
@@ -12,11 +12,13 @@ from proso.django.response import BadRequestException
 from proso.func import function_name
 from threading import currentThread
 from proso.events.client import EventsLogger, Pusher, EventClient
+import logging
 import abc
 import hashlib
 import importlib
 import json
 import os
+import datetime

 _is_user_overriden_from_url = {}
 _is_time_overriden_from_url = {}
Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]? y
@@ -25,10 +27,18 @@ _custom_configs = {}
 _custom_config_filters = {}

+class ProsoEventsLogger(EventsLogger):
+    def emit(self, event_type: str, data: dict, tags: list = [], time: datetime.datetime = datetime.datetime.now()):
+        try:
+            super().emit(event_type, data, tags, time)
+        except:
+            logging.getLogger('django.request').error('Wrong configuration of proso-events. Events are dropped.')
+
+
 def get_events_logger():
-    return EventsLogger(
+    return ProsoEventsLogger(
         get_config('proso_common', 'events.db_file', default=os.path.join(settings.DATA_DIR, 'events.log')),
-        get_config('proso_common', 'events.source_name', required=True)
+        get_config('proso_common', 'events.source_name', default='default')
     )

Stage this hunk [y,n,q,a,d,/,K,j,J,g,s,e,?]? y
@@ -135,7 +145,7 @@ def instantiate_from_config_list(app_name, key, pass_parameters=None, config_nam
     return [
         instantiate_from_json(config, pass_parameters=pass_parameters)
         for config in configs
-    ]
+        ]

 class CommonMiddleware(object):
Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]? n
@@ -193,7 +203,6 @@ def get_integrity_checks():

 class IntegrityCheck:
-
     def get_seed(self):
         return self._seed

Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]? n
@@ -212,7 +221,6 @@ class IntegrityCheck:

 class ConfigManager(models.Manager):
-
     def from_content(self, content, app_name=None, key=None):
         try:
             content = json.dumps(content, sort_keys=True)
Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]? n
@@ -229,7 +237,6 @@ class ConfigManager(models.Manager):

 class Config(models.Model):
-
     app_name = models.CharField(max_length=100, null=True, blank=True)
     key = models.CharField(max_length=100, null=True, blank=True)
     content = models.TextField(null=False, blank=False)
Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]? n
@@ -248,7 +255,6 @@ class Config(models.Model):

 class CustomConfigManager(models.Manager):
-
     def try_create(self, app_name, key, value, user_id, condition_key=None, condition_value=None):
         if not get_config_original('proso_common', 'config.is_custom_config_allowed', default=False):
             raise BadRequestException('Custom configuration is not allowed.')
Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]? n
@@ -312,7 +318,6 @@ class CustomConfigManager(models.Manager):

 class CustomConfig(models.Model):
-
     config = models.ForeignKey(Config)
     user = models.ForeignKey(User)
     condition_key = models.CharField(max_length=255, null=True, blank=True, default=None)
Stage this hunk [y,n,q,a,d,/,K,g,e,?]? n
$ git stash
$ git commit --author="Jan Karásek <h.karasek@gmail.com>" -m "Fix: proso-events does not require the correct configuration. Log error instead"

Finally, the branch is ready and we can update the pull request:

git push origin feature/proso-events --force

and merged. Since the pull request contains only one commit, we prefer rebasing:

$ git checkout master
$ git pull origin master
$ git rebase -i origin/feature/proso-events
$ git push origin master