diff options
Diffstat (limited to 'pytask/profile')
-rwxr-xr-x | pytask/profile/__init__.py | 0 | ||||
-rw-r--r-- | pytask/profile/events.py | 0 | ||||
-rw-r--r-- | pytask/profile/forms.py | 93 | ||||
-rw-r--r-- | pytask/profile/management/__init__.py | 1 | ||||
-rw-r--r-- | pytask/profile/management/commands/__init__.py | 0 | ||||
-rw-r--r-- | pytask/profile/management/commands/seed_db.py | 61 | ||||
-rwxr-xr-x | pytask/profile/models.py | 78 | ||||
-rw-r--r-- | pytask/profile/templatetags/__init__.py | 0 | ||||
-rw-r--r-- | pytask/profile/templatetags/user_tags.py | 12 | ||||
-rwxr-xr-x | pytask/profile/tests.py | 23 | ||||
-rw-r--r-- | pytask/profile/urls.py | 16 | ||||
-rw-r--r-- | pytask/profile/utils.py | 32 | ||||
-rwxr-xr-x | pytask/profile/views.py | 149 |
13 files changed, 465 insertions, 0 deletions
diff --git a/pytask/profile/__init__.py b/pytask/profile/__init__.py new file mode 100755 index 0000000..e69de29 --- /dev/null +++ b/pytask/profile/__init__.py diff --git a/pytask/profile/events.py b/pytask/profile/events.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pytask/profile/events.py diff --git a/pytask/profile/forms.py b/pytask/profile/forms.py new file mode 100644 index 0000000..649be82 --- /dev/null +++ b/pytask/profile/forms.py @@ -0,0 +1,93 @@ +import os +import PIL + +from django import forms + +from registration.forms import RegistrationFormUniqueEmail +from registration.models import RegistrationProfile + +from pytask.utils import make_key +from pytask.profile.models import GENDER_CHOICES, Profile + +class CustomRegistrationForm(RegistrationFormUniqueEmail): + """Used instead of RegistrationForm used by default django-registration + backend, this adds aboutme, dob, gender, address, phonenum to the default + django-registration RegistrationForm""" + + full_name = forms.CharField(required=True, max_length=50, + label="Name as on your bank account", + help_text="Any DD/Cheque will be issued on \ + this name") + + aboutme = forms.CharField(required=True, max_length=1000, label=u"About Me", + help_text="A write up about yourself to aid the\ + reviewer in judging your eligibility for a task.\ + It can have your educational background, CGPA,\ + field of interests etc.," + ) + + + dob = forms.DateField(help_text = "YYYY-MM-DD", required=True, label=u'date of birth') + gender = forms.ChoiceField(choices = GENDER_CHOICES, required=True, label=u'gender') + + address = forms.CharField(required=True, max_length=200, help_text="This \ + information will be used while sending DD/Cheque") + phonenum = forms.CharField(required=True, max_length=10, + label="Phone Number") + + def clean_aboutme(self): + """ Empty not allowed """ + + data = self.cleaned_data['aboutme'] + if not data.strip(): + raise forms.ValidationError("Please write something about\ + yourself") + + return data + + def clean_address(self): + """ Empty not allowed """ + + data = self.cleaned_data['address'] + if not data.strip(): + raise forms.ValidationError("Please enter an address") + + return data + + def clean_phonenum(self): + """ should be of 10 digits """ + + data = self.cleaned_data['phonenum'] + + if (not data.strip()) or \ + (data.strip("1234567890")) or \ + (len(data)!= 10): + raise forms.ValidationError("This is not a valid phone number") + + return data + + + def save(self,profile_callback=None): + + new_user = RegistrationProfile.objects.create_inactive_user( + username=self.cleaned_data['username'], + password=self.cleaned_data['password1'], + email=self.cleaned_data['email']) + + new_profile = Profile(user=new_user, + aboutme=self.cleaned_data['aboutme'], + dob=self.cleaned_data['dob'], + gender=self.cleaned_data['gender'], + address=self.cleaned_data['address'], + phonenum=self.cleaned_data['phonenum'], + uniq_key=make_key(Profile), + ) + new_profile.save() + + return new_user + +class EditProfileForm(forms.ModelForm): + + class Meta: + model = Profile + fields = ['full_name', 'aboutme', 'gender', 'dob', 'address', 'phonenum'] diff --git a/pytask/profile/management/__init__.py b/pytask/profile/management/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/pytask/profile/management/__init__.py @@ -0,0 +1 @@ + diff --git a/pytask/profile/management/commands/__init__.py b/pytask/profile/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pytask/profile/management/commands/__init__.py diff --git a/pytask/profile/management/commands/seed_db.py b/pytask/profile/management/commands/seed_db.py new file mode 100644 index 0000000..e011ee8 --- /dev/null +++ b/pytask/profile/management/commands/seed_db.py @@ -0,0 +1,61 @@ +import sys +from datetime import datetime +from django.core.management.base import NoArgsCommand + +from django.contrib.auth.models import User + +from pytask.profile.models import Profile, Notification +from pytask.utils import make_key + +def seed_db(): + """ a method to seed the database with random data """ + + + for i in range(21,1,-1): + + username = 'user'+str(i) + email = username+'@example.com' + password = '123456' + full_name = "User "+str(i) + dob = datetime.now() + gender = "M" + aboutme = "I am User"+str(i) + address = "I live in street"+str(i) + phonenum = "1234567890" + + new_user = User.objects.create_user(username=username, + email=email, + password=password) + + new_profile = Profile() + new_profile.user = new_user + new_profile.full_name = full_name + new_profile.dob = dob + new_profile.aboutme = aboutme + new_profile.gender = gender + new_profile.address = address + new_profile.phonenum = phonenum + if i%2 == 0: + new_profile.rights = "CT" + elif i%3 == 0: + new_profile.rights = "CR" + new_profile.save() + + new_user.is_superuser = True + new_user.is_staff = True + new_user.save() + + for i in range(10): + Notification(sent_to=new_user, sent_date=datetime.now(), + subject="A subject here for"+str(i), + message="A message with mess"+str(i)+" html inside.\ + <br /><b>a bold text</b>", + uniq_key=make_key(Notification), + ).save() + +class Command(NoArgsCommand): + + def handle_noargs(self, **options): + """ Just copied the code from seed_db.py """ + + seed_db() diff --git a/pytask/profile/models.py b/pytask/profile/models.py new file mode 100755 index 0000000..48e8dc5 --- /dev/null +++ b/pytask/profile/models.py @@ -0,0 +1,78 @@ +from django.db import models + +from django.contrib.auth.models import User + +GENDER_CHOICES = (( 'M', 'Male'), ('F', 'Female')) + +RIGHTS_CHOICES = ( + ("DC", "Director"), + ("MG", "Manager"), + ("CR", "Co-ordinator"), + ("CT", "Contributor"),) + +ROLE_CHOICES = ( + ("DC", "Request sent by Director \ + to a user at lower level, asking him to act as a director"), + ("MG", "Request sent by Manager \ + to a user at lower level, asking him to act as a manager"),) + +class Profile(models.Model): + + uniq_key = models.CharField(max_length=20) + + full_name = models.CharField(max_length=50, verbose_name="Name as on bank\ + account", help_text="Any DD/Cheque will be\ + issued on this name") + user = models.ForeignKey(User, unique = True) + rights = models.CharField(max_length = 2, choices = RIGHTS_CHOICES, default = u"CT") + pynts = models.PositiveSmallIntegerField(default = 0) + + aboutme = models.TextField(blank = True, help_text="This information will\ + be used to judge the eligibility for any task") + + dob = models.DateField(verbose_name = u"Date of Birth", help_text = "YYYY-MM-DD") + gender = models.CharField(max_length = 1, choices = GENDER_CHOICES) + + address = models.TextField(blank = False, help_text="This information will\ + be used to send any DDs/Cheques") + phonenum = models.CharField(max_length = 15, blank = True, verbose_name = u"Phone Number") + + def __unicode__(self): + return unicode(self.user.username) + +class Notification(models.Model): + """ A model to hold notifications. + All these are sent by the site to users. + Hence there is no sent_from option. + """ + + uniq_key = models.CharField(max_length=20) + + sent_to = models.ForeignKey(User, related_name = "%(class)s_sent_to", blank = False) + + subject = models.CharField(max_length=100, blank=True) + message = models.TextField() + + sent_date = models.DateTimeField() + is_read = models.BooleanField(default = False) + is_deleted = models.BooleanField(default = False) + +class RoleRequest(models.Model): + """ A request sent by one user to the other. + Typically requesting to raise one's status. + """ + + uniq_key = models.CharField(max_length=20) + role = models.CharField(max_length=2, choices=ROLE_CHOICES) + is_accepted = models.BooleanField(default=False) + + message = models.TextField() + response = models.TextField() + + sent_to = models.ForeignKey(User, related_name = "%(class)s_sent_to", blank = False) + sent_from = models.ForeignKey(User, related_name = "%(class)s_sent_from", null = True, blank = True) + + sent_date = models.DateTimeField() + is_read = models.BooleanField(default = False) + is_deleted = models.BooleanField(default = False) + diff --git a/pytask/profile/templatetags/__init__.py b/pytask/profile/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pytask/profile/templatetags/__init__.py diff --git a/pytask/profile/templatetags/user_tags.py b/pytask/profile/templatetags/user_tags.py new file mode 100644 index 0000000..e03aa09 --- /dev/null +++ b/pytask/profile/templatetags/user_tags.py @@ -0,0 +1,12 @@ +from django import template + +register = template.Library() + +@register.filter +def notf_dsp(user): + + notf_cnt = user.notification_sent_to.filter(is_deleted=False, + is_read=False).count() + + return u'notifications(%s)'%notf_cnt if notf_cnt else u'notifications' + diff --git a/pytask/profile/tests.py b/pytask/profile/tests.py new file mode 100755 index 0000000..2247054 --- /dev/null +++ b/pytask/profile/tests.py @@ -0,0 +1,23 @@ +""" +This file demonstrates two different styles of tests (one doctest and one +unittest). These will both pass when you run "manage.py test". + +Replace these with more appropriate tests for your application. +""" + +from django.test import TestCase + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.failUnlessEqual(1 + 1, 2) + +__test__ = {"doctest": """ +Another way to test that 1 + 1 is equal to 2. + +>>> 1 + 1 == 2 +True +"""} + diff --git a/pytask/profile/urls.py b/pytask/profile/urls.py new file mode 100644 index 0000000..0949c66 --- /dev/null +++ b/pytask/profile/urls.py @@ -0,0 +1,16 @@ +from django.conf.urls.defaults import * + +from pytask.profile.views import view_profile, edit_profile,\ + browse_notifications, view_notification,\ + delete_notification, unread_notification + +urlpatterns = patterns('', + + (r'^view/$', view_profile), + (r'^edit/$', edit_profile), + (r'^notf/browse/$', browse_notifications), + (r'^notf/view/nid=(\w+)$', view_notification), + (r'^notf/del/nid=(\w+)$', delete_notification), + (r'^notf/unr/nid=(\w+)$', unread_notification), +) + diff --git a/pytask/profile/utils.py b/pytask/profile/utils.py new file mode 100644 index 0000000..84b5e00 --- /dev/null +++ b/pytask/profile/utils.py @@ -0,0 +1,32 @@ +from pytask.profile.models import Notification + +def get_notification(nid, user): + """ if notification exists, and belongs to the current user, return it. + else return None. + """ + + user_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date') + current_notifications = user_notifications.filter(uniq_key=nid) + if user_notifications: + current_notification = current_notifications[0] + + try: + newer_notification = current_notification.get_next_by_sent_date(sent_to=user, is_deleted=False) + newest_notification = user_notifications.reverse()[0] + if newest_notification == newer_notification: + newest_notification = None + except Notification.DoesNotExist: + newest_notification, newer_notification = None, None + + try: + older_notification = current_notification.get_previous_by_sent_date(sent_to=user, is_deleted=False) + oldest_notification = user_notifications[0] + if oldest_notification == older_notification: + oldest_notification = None + except: + oldest_notification, older_notification = None, None + + return newest_notification, newer_notification, current_notification, older_notification, oldest_notification + + else: + return None, None, None, None, None diff --git a/pytask/profile/views.py b/pytask/profile/views.py new file mode 100755 index 0000000..a8e48f4 --- /dev/null +++ b/pytask/profile/views.py @@ -0,0 +1,149 @@ +from django.shortcuts import render_to_response, redirect +from django.http import Http404 + +from django.contrib.auth.decorators import login_required +from django.core.context_processors import csrf +from django.views.decorators.csrf import csrf_protect + +from pytask.profile.forms import EditProfileForm +from pytask.profile.utils import get_notification + +@login_required +def view_profile(request): + """ Display the profile information. + """ + + user = request.user + profile = user.get_profile() + + context = {"user": user, + "profile": profile, + } + return render_to_response("profile/view.html", context) + +@login_required +def edit_profile(request): + """ Make only a few fields editable. + """ + + user = request.user + profile = user.get_profile() + + context = {"user": user, + "profile": profile, + } + + context.update(csrf(request)) + + if request.method == "POST": + form = EditProfileForm(request.POST, instance=profile) + + if form.is_valid(): + form.save() + return redirect("/profile/view") + else: + context.update({"form":form}) + return render_to_response("profile/edit.html", context) + else: + form = EditProfileForm(instance=profile) + context.update({"form":form}) + return render_to_response("profile/edit.html", context) + +@login_required +def browse_notifications(request): + """ get the list of notifications that are not deleted and display in + datetime order.""" + + user = request.user + + active_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date').reverse() + + context = {'user':user, + 'notifications':active_notifications, + } + + return render_to_response('profile/browse_notifications.html', context) + +@login_required +def view_notification(request, nid): + """ get the notification depending on nid. + Display it. + """ + + user = request.user + newest, newer, notification, older, oldest = get_notification(nid, user) + + if not notification: + raise Http404 + + notification.is_read = True + notification.save() + + context = {'user':user, + 'notification':notification, + 'newest':newest, + 'newer':newer, + 'older':older, + 'oldest':oldest, + } + + return render_to_response('profile/view_notification.html', context) + +@login_required +def delete_notification(request, nid): + """ check if the user owns the notification and delete it. + """ + + user = request.user + newest, newer, notification, older, oldest = get_notification(nid, user) + + if not notification: + raise Http404 + + notification.is_deleted = True + notification.save() + + context = {'user':user, + 'notification':notification, + 'newest':newest, + 'newer':newer, + 'older':older, + 'oldest':oldest, + } + + if older: + redirect_url = "/profile/notf/view/nid=%s"%older.uniq_key + else: + redirect_url = "/profile/notf/browse" + + return redirect(redirect_url) + +@login_required +def unread_notification(request, nid): + + """ check if the user owns the notification and delete it. + """ + + user = request.user + newest, newer, notification, older, oldest = get_notification(nid, user) + + if not notification: + raise Http404 + + notification.is_read = False + notification.save() + + context = {'user':user, + 'notification':notification, + 'newest':newest, + 'newer':newer, + 'older':older, + 'oldest':oldest, + } + + if older: + redirect_url = "/profile/notf/view/nid=%s"%older.uniq_key + else: + redirect_url = "/profile/notf/browse" + + return redirect(redirect_url) |