Permalink
Browse files

first import

  • Loading branch information...
Mike Krieger
Mike Krieger committed Dec 10, 2008
0 parents commit 17eaea55fc446af80cf34f161537ef93a010371e
BIN .DS_Store
Binary file not shown.
@@ -0,0 +1 @@
+*.pyc
No changes.
@@ -0,0 +1,6 @@
+from django.contrib import admin
+
+class AdminSite(admin.AdminSite):
+ pass
+
+site = AdminSite()
No changes.
@@ -0,0 +1,53 @@
+from django.db import models
+from django.contrib.auth.admin import UserAdmin
+from django.contrib.auth.models import User
+from django.contrib import admin
+from greetingsweb.admin import site
+# Create your models here.
+
+class Template(models.Model):
+ name = models.CharField(blank=True, max_length=255)
+
+class Email(models.Model):
+ address = models.TextField()
+
+class Phone(models.Model):
+ number = models.TextField()
+
+class Person(models.Model):
+ emails = models.ManyToManyField(Email, null=True, blank=True)
+ phones = models.ManyToManyField(Phone, null=True, blank=True)
+ name = models.CharField(max_length=200, null=True, blank=True)
+
+class EmailInline(admin.StackedInline):
+ model = Email
+
+class PhoneInline(admin.StackedInline):
+ model = Phone
+
+
+class Card (models.Model):
+ from_person = models.ForeignKey(Person, related_name="from_person")
+ to_people = models.ManyToManyField(Person, related_name="to_people", null=True, blank=True)
+ audio_file = models.FileField(upload_to='audio_uploads/', null=True, blank=True)
+ image_file = models.FileField(upload_to='image_uploads/', null=True, blank=True)
+ template = models.ForeignKey(Template, null=True, blank=True)
+ text_content = models.TextField()
+ def __unicode__(self):
+ return "Card from %s to %s that says %s" % (self.from_person, self.to_people, self.text_content)
+
+
+
+class MyUserAdmin(UserAdmin):
+ inlines = []
+
+class ProfileAdmin(admin.ModelAdmin):
+ # inlines = [EmailInline, PhoneInline]
+ pass
+
+site.register(Template)
+site.register(Card)
+site.register(Email)
+site.register(Phone)
+site.register(Person, ProfileAdmin)
+site.register(User, MyUserAdmin)
@@ -0,0 +1,12 @@
+from django.conf.urls.defaults import *
+
+# Uncomment the next two lines to enable the admin:
+# from django.contrib import admin
+
+
+urlpatterns = patterns('',
+ url(r'greeting/(?P<greeting_id>(\d+))', 'greetingsweb.greetings.views.get_greeting', name="get-greeting"),
+ url(r'attach/', 'greetingsweb.greetings.views.add_attachment', name="add-attachment"),
+ url(r'makegreeting/', 'greetingsweb.greetings.views.make_greeting', name='make-greeting'),
+ url(r'newuser/', 'greetingsweb.greetings.views.new_user', name='new-user'),
+)
@@ -0,0 +1,152 @@
+# Create your views here.
+from django.http import HttpResponse, HttpResponseRedirect
+from django.shortcuts import render_to_response, get_object_or_404
+from django.contrib.auth.models import User
+from django.core.exceptions import ObjectDoesNotExist
+from django.template import Context, loader, RequestContext
+from django.contrib.auth.decorators import login_required
+from django.forms import ModelForm
+from django.db.models.query import Q
+from django.utils import simplejson
+from django.core.exceptions import ObjectDoesNotExist
+from greetingsweb.greetings.models import *
+import base64
+
+
+def get_greeting(request, greeting_id):
+ greeting = get_object_or_404(Card, pk=greeting_id)
+ json_response = dict()
+ json_response['from'] = greeting.from_person.username
+ json_response['to'] = str(greeting.to_people.all())
+ json_response['text'] = greeting.text_content
+ json_response['image_url'] = str(greeting.image_file)
+ json_response['template'] = greeting.template
+ json_response['audio_url'] = str(greeting.audio_file)
+
+ return HttpResponse(simplejson.dumps(json_response))
+
+def check_request_parameters(request, param_list):
+ for elem in param_list:
+ if elem not in request.REQUEST:
+ return False
+ return True
+
+def find_person_from_lists(email_list=None, phone_list=None):
+ person = None
+ if email_list:
+ for el in email_list:
+ try:
+ email = Email.objects.get(address=el.strip())
+ print email
+ person = Person.objects.get(emails=email)
+ return person
+ except Exception, e:
+ pass
+ if phone_list:
+ for el in phone_list:
+ try:
+ phone = Phone.objects.get(number=el.strip())
+ person = Person.objects.get(phones=phone)
+ return person
+ except Exception, e:
+ pass
+ return None
+
+
+
+
+def new_user(request):
+ json_response = dict()
+ try:
+
+ if check_request_parameters(request, ("email_list", "phone_list", "name") ):
+ email_list = request.REQUEST['email_list'].split(',')
+ phone_list = request.REQUEST['phone_list'].split(',')
+ user = find_person_from_lists(email_list=email_list, phone_list=phone_list)
+ if not user:
+ user = Person()
+ user.name = request.REQUEST['name']
+ user.save()
+
+ # true unless otherwise
+
+ json_response['success'] = True
+ for email in email_list:
+ if len(Email.objects.filter(address=email)) > 0:
+ json_response['success'] = False
+ json_response['error'] = "Duplicate email"
+ else:
+ new_email = Email()
+ new_email.address = email
+ new_email.save()
+ print "adding ", new_email.address
+ user.emails.add(new_email)
+
+ for phone in phone_list:
+ if len(Phone.objects.filter(number=phone)) > 0:
+ json_response['success'] = False
+ json_response['error'] = "Duplicate phone"
+ else:
+ new_phone = Phone()
+ new_phone.number = phone
+ new_phone.save()
+ user.phones.add(new_phone)
+
+ json_response['new_id'] = user.id
+ else:
+ json_response['success'] = False
+ json_response['error'] = "missing parameters"
+
+ except Exception, e:
+ json_response['success'] = False
+ json_response['error'] = str(e)
+
+ return HttpResponse(simplejson.dumps(json_response))
+
+def get_or_create_person_from_phone(phone):
+ try:
+ user = find_person_from_lists(phone_list=(phone,))
+ if user:
+ return user
+ else:
+ user = Person()
+ user.save()
+ user.phones.add(phone)
+ return user
+ except Exception, e:
+ return None
+
+def make_greeting(request):
+ json_response = dict()
+ try:
+ if check_request_parameters(request, ("from_user_id", "to_people_phones") ):
+ new_card = Card()
+ new_card.from_person = Person.objects.get(pk=request.REQUEST['from_user_id'])
+
+
+ new_card.save()
+ for phone in request.REQUEST["to_people_phones"].split(","):
+ other_person = get_or_create_person_from_phone(phone)
+ if other_person:
+ new_card.to_people.add( get_or_create_person_from_phone(phone) )
+ if check_request_parameters(request, ("text",) ):
+ new_card.text_content = request.REQUEST["text"]
+
+
+ new_card.save()
+ json_response['success'] = True
+ json_response['card_id'] = new_card.id
+ else:
+ json_response['success'] = False
+ json_response['error'] = "missing people ids"
+
+ except Exception, e:
+ print e
+ json_response['success'] = False
+ json_response['error'] = str(e)
+ return HttpResponse(simplejson.dumps(json_response))
+
+def add_attachment(request):
+ out_fl = open('out.caf', 'w')
+ for eachfile in request.FILES:
+ out_fl.write(request.FILES[eachfile].read())
Binary file not shown.
@@ -0,0 +1,11 @@
+#!/usr/bin/env python
+from django.core.management import execute_manager
+try:
+ import settings # Assumed to be in the same directory.
+except ImportError:
+ import sys
+ sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
+ sys.exit(1)
+
+if __name__ == "__main__":
+ execute_manager(settings)
Binary file not shown.
BIN out.caf
Binary file not shown.
@@ -0,0 +1,83 @@
+# Django settings for greetingsweb project.
+
+DEBUG = True
+TEMPLATE_DEBUG = DEBUG
+
+ADMINS = (
+ # ('Your Name', 'your_email@domain.com'),
+)
+
+MANAGERS = ADMINS
+
+DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
+DATABASE_NAME = 'greetingsdb' # Or path to database file if using sqlite3.
+DATABASE_USER = '' # Not used with sqlite3.
+DATABASE_PASSWORD = '' # Not used with sqlite3.
+DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
+DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
+
+# Local time zone for this installation. Choices can be found here:
+# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
+# although not all choices may be available on all operating systems.
+# If running in a Windows environment this must be set to the same as your
+# system time zone.
+TIME_ZONE = 'America/Chicago'
+
+# Language code for this installation. All choices can be found here:
+# http://www.i18nguy.com/unicode/language-identifiers.html
+LANGUAGE_CODE = 'en-us'
+
+SITE_ID = 1
+
+# If you set this to False, Django will make some optimizations so as not
+# to load the internationalization machinery.
+USE_I18N = True
+
+# Absolute path to the directory that holds media.
+# Example: "/home/media/media.lawrence.com/"
+MEDIA_ROOT = '/Users/mkrieger/Dropbox/193p Final Project/greetingsweb/media/'
+
+# URL that handles the media served from MEDIA_ROOT. Make sure to use a
+# trailing slash if there is a path component (optional in other cases).
+# Examples: "http://media.lawrence.com", "http://example.com/media/"
+MEDIA_URL = ''
+
+# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
+# trailing slash.
+# Examples: "http://foo.com/media/", "/media/".
+ADMIN_MEDIA_PREFIX = '/media/'
+
+# Make this unique, and don't share it with anybody.
+SECRET_KEY = '_&$=c&(n5(a-y-ju3v*we-a^leb0h1uwim3e(55$0@3zhg$u65'
+
+# List of callables that know how to import templates from various sources.
+TEMPLATE_LOADERS = (
+ 'django.template.loaders.filesystem.load_template_source',
+ 'django.template.loaders.app_directories.load_template_source',
+# 'django.template.loaders.eggs.load_template_source',
+)
+
+MIDDLEWARE_CLASSES = (
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+)
+
+ROOT_URLCONF = 'greetingsweb.urls'
+
+TEMPLATE_DIRS = (
+ # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
+ # Always use forward slashes, even on Windows.
+ # Don't forget to use absolute paths, not relative paths.
+)
+
+INSTALLED_APPS = (
+ 'greetingsweb.greetings',
+ 'django.contrib.auth',
+ 'django.contrib.admin',
+ 'django.contrib.contenttypes',
+ 'django.contrib.sessions',
+ 'django.contrib.sites',
+)
+
+AUTH_PROFILE_MODULE = 'greetings.UserInfo'
19 urls.py
@@ -0,0 +1,19 @@
+from django.conf.urls.defaults import *
+
+# Uncomment the next two lines to enable the admin:
+# from django.contrib import admin
+# admin.autodiscover()
+
+from greetingsweb.admin import site
+
+urlpatterns = patterns('',
+ # Example:
+ (r'', include('greetingsweb.greetings.urls')),
+
+ # Uncomment the admin/doc line below and add 'django.contrib.admindocs'
+ # to INSTALLED_APPS to enable admin documentation:
+ # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
+
+ # Uncomment the next line to enable the admin:
+ (r'^admin/(.*)', site.root),
+)

0 comments on commit 17eaea5

Please sign in to comment.