Udi Bauman avatar Udi Bauman committed 10b9cce

status change auditing

Comments (0)

Files changed (19)

Binary file modified.

Add a comment to this file

django_ultrasound/common/current_user/__init__.py

Empty file added.

django_ultrasound/common/current_user/middleware.py

+from django.db.models import signals
+from django.utils.functional import curry
+from django.utils.decorators import decorator_from_middleware
+
+import registration
+
+class CurrentUserMiddleware(object):
+
+    def process_request(self, request):
+        if request.method in ('GET', 'HEAD', 'OPTIONS', 'TRACE'):
+            # This request shouldn't update anything,
+            # so no singal handler should be attached.
+            return
+
+        if hasattr(request, 'user') and request.user.is_authenticated():
+            user = request.user
+        else:
+            user = None
+
+        update_users = curry(self.update_users, user)
+        signals.pre_save.connect(update_users, dispatch_uid=request, weak=False)
+
+    def update_users(self, user, sender, instance, **kwargs):
+        registry = registration.FieldRegistry()
+        if sender in registry:
+            for field in registry.get_fields(sender):
+                setattr(instance, field.name, user)
+
+    def process_response(self, request, response):
+        signals.pre_save.disconnect(dispatch_uid=request)
+        return response
+
+record_current_user = decorator_from_middleware(CurrentUserMiddleware)

django_ultrasound/common/current_user/models.py

+from django.db import models
+from django.contrib.auth.models import User
+import logging
+import registration
+from django.conf import settings
+
+
+class CurrentUserField(models.ForeignKey):
+    def __init__(self, **kwargs):
+        super(CurrentUserField, self).__init__(User, null=True, **kwargs)
+
+    def contribute_to_class(self, cls, name):
+        logging.debug("*"*100)
+        logging.debug( "Was here")
+        logging.debug(settings.MIDDLEWARE_CLASSES)
+        logging.debug( "*"*100)
+        super(CurrentUserField, self).contribute_to_class(cls, name)
+        registry = registration.FieldRegistry()
+        registry.add_field(cls, self)

django_ultrasound/common/current_user/registration.py

+class FieldRegistry(object):
+    _registry = {}
+
+    def add_field(self, model, field):
+        reg = self.__class__._registry.setdefault(model, [])
+        reg.append(field)
+
+    def get_fields(self, model):
+        return self.__class__._registry.get(model, [])
+
+    def __contains__(self, model):
+        return model in self.__class__._registry

django_ultrasound/common/utils.py

+from django.template import Template
+from django.template.context import Context
+
+def evaluate_template(text, data):
+    t = Template(text)
+    c = Context(data)
+    return t.render(c)
Add a comment to this file

django_ultrasound/customers/__init__.pyc

Binary file removed.

django_ultrasound/customers/admin.py

 from django.contrib import admin
 from customers.models import *
+from django.utils.translation import ugettext as _
 
 
 def render_color_box(color, value=""):
 admin.site.register(CustomerEduInstitute, CustomerEduInstituteAdmin)
 
 class CustomerAdmin(admin.ModelAdmin):
-    list_display = ["user", "sex","customer_status_box"]
+    list_display = ["user", "sex","customer_status_box", "facebook_page"]
     list_filter = ["address", "customer_status"]
 
     def customer_status_box(self, obj):
         return render_color_box(obj.customer_status.color, obj.customer_status.name)
 
+    def facebook_page(self, obj):
+        if obj.link_to_page:
+            return "<a href='%s'>%s</a>" % (obj.link_to_page, _("FaceBook page"))
+        else:
+            return "(%s)" % _("Missing link")
+
+
     customer_status_box.allow_tags = True
+    facebook_page.allow_tags = True
 
 admin.site.register(Customer, CustomerAdmin)
 

django_ultrasound/customers/forms.py

+from django import forms
+
+class CustomerMoreDetailsForm(forms.Form):
+    pass

django_ultrasound/customers/models.py

     user = models.ForeignKey(User, verbose_name=_("user"), related_name="customer")
     id_number = models.PositiveIntegerField(_("ID number"), unique=True, help_text=_("Enter ID number"), null=True,
                                             blank=True)
+    link_to_page = models.URLField(_("Link to FaceBook page"), verify_exists=False, max_length=500, null=True, blank=True)
     customer_status = models.ForeignKey(CustomerStatus, verbose_name=_("Status"), related_name=_("customers"))
     check_data = models.BooleanField(_("check deatels on the enternce"), default=False)
     update_data = models.BooleanField(_("we have all the deatles we need"), default=False)
-    sex = models.IntegerField(_("man/women"), choices=((1, _("Man")), (2, _("Woman"))), null=True, blank=True)
+    sex = models.IntegerField(_("man/woman"), choices=((1, _("Man")), (2, _("Woman"))), null=True, blank=True)
     photo = models.FileField(_("photo"), upload_to="static/photos", null=True, blank=True)
     birth_day = models.DateField(_("birth day"), null=True, blank=True)
     address = models.ForeignKey(CustomerAddress, verbose_name=_("home Address"), related_name=_("customer"), null=True,
     education_institute = models.ForeignKey(CustomerEduInstitute, verbose_name=_("education institute"), null=True,
                                             blank=True)
 
+
+    def __init__(self, *args, **kwargs):
+        super(Customer, self).__init__(*args, **kwargs)
+        self.original = {}
+        id = getattr(self, 'id', None)
+
+        for field in self._meta.fields:
+            if id:
+                self.original[field.name] = getattr(self, field.name, None)
+            else:
+                self.original[field.name] = None
+
+
+    def get_old_value(self, field_name):
+        return self.original.get(field_name, None)
+
+
Add a comment to this file

django_ultrasound/customers/models.pyc

Binary file removed.

django_ultrasound/customers/views.py

 from django.contrib.auth.decorators import login_required
 from django.shortcuts import render_to_response
+from customers.forms import CustomerMoreDetailsForm
+from customers.models import CANDIDATE
+from common.utils import evaluate_template
 
 @login_required
 def customer_home(request):
     user = request.user
     customer = user.customer.get()
     customer_status = customer.customer_status
-    content = customer_status.html_page.content
-    title = customer_status.html_page.title
     data = {
-        "content": content,
-        "title": title,
+        "user_first_name": user.first_name,
+        # add any fields that will be inserted into the HTML page
     }
+    data["title"] = evaluate_template(customer_status.html_page.title, data)
+    data["content"] = evaluate_template(customer_status.html_page.content, data)
+    if customer.customer_status == CANDIDATE:
+        data = customer_more_details_form_home(data)
     return render_to_response("home.html", data)
 
 
+def customer_more_details_form_home(data):
+    data.update({
+        "more_details_form": CustomerMoreDetailsForm()
+    })
+    return data
Add a comment to this file

django_ultrasound/customers/views.pyc

Binary file removed.

django_ultrasound/employees/admin.py

 
 class ActivityAdmin(admin.ModelAdmin):
     list_display = ["employee", "activity", "old_customer_status", "new_customer_status", "date_of_activity"]
-    list_filter = ["employee", "activity"]
+    list_filter = ["employee", "activity", "customer", "old_customer_status", "new_customer_status"]
     
     
 admin.site.register(Activity, ActivityAdmin)

django_ultrasound/employees/forms.py

+from django import forms
+
+class ChangeCustomerStatusForm(forms.Form):
+    
+
+
+    def __init__(self, customer_id):
+        self.customer_id = customer_id
+
+

django_ultrasound/employees/models.py

 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 from django.contrib.auth.models import User
-from customers.models import CustomerStatus
+from customers.models import CustomerStatus, Customer
+from common.current_user.models import CurrentUserField
+
 
 class EmployeeType(models.Model):
     name = models.CharField(_("name"), max_length=100)
         return u"%s (%s %s) - %s" % (self.user.username, self.user.first_name, self.user.last_name, self.employee_type)
 
 
-ACTIVITY_TYPES = ((1, _("change the customer status")),
+CHANGE_CUSTOMER_STATUS = 1
+ACTIVITY_TYPES = ((CHANGE_CUSTOMER_STATUS, _("change the customer status")),
                            )
 
 class Activity (models.Model):
-    employee = models.ForeignKey(Employee,verbose_name=_("the employee"), help_text=_("ID number of the user done the activity"))
-    activity=models.IntegerField(_("the activity the employee did"),choices=ACTIVITY_TYPES)
+#    employee = models.ForeignKey(Employee,verbose_name=_("the employee"), help_text=_("ID number of the user done the activity"))
+    employee = CurrentUserField(verbose_name=_("the employee"), help_text=_("ID number of the user done the activity"))
+    activity=models.IntegerField(_("the activity the employee did"),choices=ACTIVITY_TYPES, default=CHANGE_CUSTOMER_STATUS)
+    customer = models.ForeignKey(Customer, verbose_name=_("customer"), null=True, blank=True)
     old_customer_status = models.ForeignKey(CustomerStatus, verbose_name=_("old status"), related_name="activities_1", null=True, blank=True)
     new_customer_status = models.ForeignKey(CustomerStatus, verbose_name=_("new status"), related_name="activities_2", null=True, blank=True)
    
     #event=models.ForeignKey(event,verbose_name=_("event") 
     date_of_activity = models.DateTimeField(_("date of doing the activity"), auto_now=True)
-    
 
     def __unicode__(self):
-        return u"%s activity %d" % (self.employee, self.activity)
+        return u"%s activity %d" % (self.employee, self.activity)
+
+    class Meta:
+        verbose_name_plural = _("Activities")
+
+def log_activity(sender, instance, **kwargs):
+    if sender == Customer:
+        old_status = instance.get_old_value("customer_status")
+        new_status = instance.customer_status
+        if old_status != new_status:
+            activity = Activity()
+            activity.activity = CHANGE_CUSTOMER_STATUS
+            activity.customer = instance
+            activity.old_customer_status = old_status
+            activity.new_customer_status = new_status
+            #employee will be updated using the CurrentUserMiddleware
+            activity.save()
+
+models.signals.post_save.connect(log_activity, sender=Customer)

django_ultrasound/employees/views.py

 from django.contrib.auth.models import User
 from django.http import HttpResponse
+from django.contrib.auth.decorators import login_required
 
 def setup_first_user(request):
     if User.objects.filter(username="admin").count() == 0:
         return HttpResponse("User already exists")
 
 
+@login_required
+def change_customer_status(request, customer_id):
+    pass

django_ultrasound/facebookconnect/views.py

             # create Customer
             customer = Customer()
             customer.user = contrib_user
+            customer.fb_key = fb.facebook_id
+            customer.link_to_page = "http://www.facebook.com/?uid=%s" % customer.fb_key
             customer.customer_status = CustomerStatus.objects.get(code=REQUESTING_TO_BE_CANDIDATE)
             customer.save()
 

django_ultrasound/settings.py

 from djangoappengine.settings_base import *
 
 import os
+import logging
 
 SECRET_KEY = '=r-$b*8hglm+858&9t043hlm6-&6-3d3vfc4((7yd0dbrakhvi'
 
 # the application api key given by facebook
 FACEBOOK_API_KEY = '0f2cd1705e6eed547f35151ac8e817b2'
 # the applications ecret key given by facebook
-FACEBOOK_APPLICATION_SECRET = 'ef796081cc54f631ec50671174f7ba86'
+FACEBOOK_APPLICATION_SECRET = 'ef796081cc54f631ec50671174f7ba86'
+
+MIDDLEWARE_CLASSES = (
+'django.middleware.common.CommonMiddleware',
+'django.contrib.sessions.middleware.SessionMiddleware',
+'django.middleware.csrf.CsrfViewMiddleware',
+'django.contrib.auth.middleware.AuthenticationMiddleware',
+'common.current_user.middleware.CurrentUserMiddleware',
+'django.contrib.messages.middleware.MessageMiddleware',
+)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.