Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Application and Transactions #14

Merged
merged 17 commits into from
May 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions application/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ class Application(models.Model):
active = models.CharField(max_length=1, choices=ACTIVE_CHOICES, null=False)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.name
5 changes: 3 additions & 2 deletions application/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ def show(request, appId):
app = Application.objects.get(id=appId)
# application exists
for o in DeviceApplication.objects.filter(app=app):
devs = Device.objects.filter(id=o.dev.id)

for dev in Device.objects.filter(id=o.dev.id):
devs[dev] = dev

return render_to_response(
'application/show.html',
{
Expand Down
10 changes: 5 additions & 5 deletions config_staging.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
DATABASE_ENGINE = 'django.db.backends.sqlite3'
DATABASE_NAME = 'server.sqlite3'
DATABASE_ENGINE = 'django.db.backends.mysql'
DATABASE_NAME = 'phonelab_staging'

DATABASES = {
'default': {
'ENGINE' : DATABASE_ENGINE, # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME' : DATABASE_NAME, # Or path to database file if using sqlite3.
'USER' : '', # Not used with sqlite3.
'PASSWORD' : '', # Not used with sqlite3.
'HOST' : '', # Set to empty string for localhost. Not used with sqlite3.
'USER' : 'phonelab', # Not used with sqlite3.
'PASSWORD' : 'dbs3rv3r', # Not used with sqlite3.
'HOST' : 'phonelab-backend-db.cg2akhwze6m8.us-east-1.rds.amazonaws.com', # Set to empty string for localhost. Not used with sqlite3.
'PORT' : '', # Set to empty string for default. Not used with sqlite3.
}
}
Expand Down
4 changes: 2 additions & 2 deletions datalogger/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from device.models import Device
from lib.helper import json_response_from

import default
from default import sort_nicely
import time
import os, errno, re
# Log Dir
Expand Down Expand Up @@ -141,7 +141,7 @@ def show_tag(request, deviceId):
try:
os.chdir(path)
filelist = os.listdir(".")
default.sort_nicely(filelist)
sort_nicely(filelist)
Tagdata = ''
for file in filelist:
filename = os.path.join(RAW_LOG_ROOT, deviceId, file)
Expand Down
31 changes: 24 additions & 7 deletions device/admin.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
from django.contrib import admin
from device.models import Device, DeviceApplication
from users.models import UserProfile
from device.models import Device, DeviceApplication, DeviceProfile
from application.models import Application
from transaction.models import Transaction, TransactionDevApp

#TODO: change location and implement inline
#Inline reference: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin
"""
Admin site interface

@date 05/25/2012

@author Taeyeon
"""

class DeviceAdmin(admin.ModelAdmin):
# pass
list_display = ('id', 'email', 'reg_id')
search_fields = ['id', 'email']
admin.site.register(Device, DeviceAdmin)
#admin.site.register(Device)

class ApplicationAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'package_name')
# pass
admin.site.register(Application, ApplicationAdmin)

#Todo: add function to change object name
class DeviceApplicationAdmin(admin.ModelAdmin):
list_display = ('id', 'dev', 'app')
# pass
admin.site.register(DeviceApplication, DeviceApplicationAdmin)


class TransactionAdmin(admin.ModelAdmin):
list_display = ('id', 'user', 'total', 'progress')
# pass
admin.site.register(Transaction, TransactionAdmin)

class TransactionDevAppAdmin(admin.ModelAdmin):
list_display = ('id', 'dev', 'app', 'action', 'result')
# pass
list_display = ('id', 'tid', 'dev', 'app', 'action', 'result')
admin.site.register(TransactionDevApp, TransactionDevAppAdmin)

class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user', 'ub_id', 'activation_key', 'key_expires')
admin.site.register(UserProfile, UserProfileAdmin)

class DeviceProfileAdmin(admin.ModelAdmin):
list_display = ('dev', 'user', 'phone_no', 'working', 'plan', 'phone_purpose', 'service_type')
admin.site.register(DeviceProfile, DeviceProfileAdmin)



7 changes: 5 additions & 2 deletions device/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django import forms
from application.models import Application
from django.contrib.auth.models import User
from django.conf import settings

## other includes
Expand Down Expand Up @@ -128,8 +129,10 @@ class DeviceProfile(models.Model):
(u'4', u'4G'),
)
dev = models.ForeignKey(Device, unique=True)
phone_no = models.CharField(max_length=13)
user = models.ForeignKey(User, blank=True, null=True)
phone_no = models.CharField(max_length=13, blank=True, null=True)
working = models.CharField(max_length=1, choices=WORKING_CHOICES)
plan = models.CharField(max_length=45)
plan = models.CharField(max_length=45, blank=True, null=True)
image_version = models.CharField(max_length=45, blank=True, null=True)
phone_purpose = models.CharField(max_length=1, choices=PURPOSE_CHOICES)
service_type = models.CharField(max_length=1, choices=TYPE_CHOICES)
130 changes: 50 additions & 80 deletions device/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from device.models import Device, DeviceApplication
from application.models import Application
from transaction.models import Transaction, TransactionDevApp
import default
from default import re_sort_nicely, sort_nicely
import os, errno, re
import datetime
import string
Expand Down Expand Up @@ -122,53 +122,68 @@ def create_or_update_device(request):


"""
Show Device Details [GET]
Show Device Details and Application monitor [GET]

@date 01/29/2012
@date 05/17/2012
@param String deviceId

@author Micheal
@author TKI, Micheal
"""
@login_required
def show(request, deviceId):
def show(request, deviceId):
# define default response
response = { "err": "", "data": "" }
# get device
device = Device.objects.filter(id=deviceId)
# device exists
if device.count() == 1:
try:
dev = Device.objects.get(id=deviceId)
# device exists
app_list = {}
apps = {}
unapps = {}
# get apps of particular device
for o in DeviceApplication.objects.filter(dev=dev.id).values('app', 'dev'):
app_list[o['app']] = o['dev']
# get list of apps to download
for app in Application.objects.all():
if app_list.has_key(app.id):
apps[app.id] = {"app_object": app,}
else:
unapps[app.id] = {"app_object": app,}

# get log data list from deviceId directory
path = os.path.join(RAW_LOG_ROOT, device[0].id)
path = os.path.join(RAW_LOG_ROOT, dev.id)
# empty
filelist = {}
try:
os.chdir(path)
filelist = os.listdir(".")
default.re_sort_nicely(filelist)
re_sort_nicely(filelist)

except OSError, e:
if e.errno != errno.EEXIST:
response['err'] = {
'no' : 'err1',
'msg': 'cannot change dir, failed upload'
}

return render_to_response(
'device/show.html',
{
'device': device[0],
'filelist': filelist
},
'device/show.html',
{
'device' : dev,
'apps' : apps,
'unapps' : unapps,
'filelist' : filelist
},
context_instance=RequestContext(request)
)
)

# device does not exist
else:
except Device.DoesNotExist:
response['err'] = {
'no' : 'err1',
'msg': 'invalid device'
}
return json_response_from(response)


"""
Edit Device Form [GET]

Expand Down Expand Up @@ -265,15 +280,17 @@ def c2dm(request, deviceId):
else:
msg = request.POST['c2dm_msg']
# get device
device = Device.objects.filter(id=deviceId)
# if device exists, update
# msg = "new_manifest"
if device.count() == 1:
response = device[0].send_message(payload=json({"message": msg}))
else:
return HttpResponseRedirect('/error/')
return json_response_from(response)

try:
dev = Device.objects.get(id=deviceId)
# msg = "new_manifest"
#TODO: Implement other messages.
response = dev.send_message(payload=json({"message": msg}))
except Device.DoesNotExist:
response['err'] = {
'no' : 'err1',
'msg': 'invalid device'
}
return json_response_from(response)

"""
Status monitor [GET]
Expand Down Expand Up @@ -310,7 +327,7 @@ def status(request, deviceId, statusType):
try:
os.chdir(path)
filelist = os.listdir(".")
default.sort_nicely(filelist)
sort_nicely(filelist)
Tagdata = ''
for file in filelist:
filename = os.path.join(RAW_LOG_ROOT, deviceId, file)
Expand All @@ -321,11 +338,12 @@ def status(request, deviceId, statusType):
temp = line.split()
Tagdata += ' [ ' + temp[0] + ' ' + temp[1] + ' ] '
if statusType == '1':
Tagdata += 'Battery Level: ' + temp[7]
Tagdata += 'Battery Level: ' + temp[7] + '\n'
elif statusType == '2':
Tagdata += 'GPS Latitude: ' + temp[7] + ', Longitude: ' + temp[9] + 'Accuracy: ' + temp[11]
Tagdata += 'GPS Latitude: ' + temp[7] + ', Longitude: ' + temp[9] + ', Accuracy: ' + temp[11] + '\n'
else:
Tagdata += 'Signal Strengh: ' + temp[7] + ', asu: ' + temp[9]
Tagdata += 'Signal Strengh: ' + temp[7] + ', asu: ' + temp[9] + '\n'

# render respone
return render_to_response(
'device/status.html',
Expand Down Expand Up @@ -354,54 +372,6 @@ def status(request, deviceId, statusType):
return json_response_from(response)


"""
Application monitor [GET]

@date 03/25/2012
@param String deviceId

@author TKI
"""
@login_required
def list_app(request, deviceId):
# define default response
response = { "err": "", "data": "" }
# get device
try:
dev = Device.objects.get(id=deviceId)
# device exists
app_list = {}
apps = {}
unapps = {}
# get apps of particular device
for o in DeviceApplication.objects.filter(dev=dev.id).values('app', 'dev'):
app_list[o['app']] = o['dev']
# get list of apps to download
for app in Application.objects.all():
if app_list.has_key(app.id):
apps[app.id] = {"app_object": app,}
else:
unapps[app.id] = {"app_object": app,}

return render_to_response(
'device/list.html',
{
'deviceId': deviceId,
'apps' : apps,
'unapps' : unapps
},
context_instance=RequestContext(request)
)

# device does not exist
except Device.DoesNotExist:
response['err'] = {
'no' : 'err1',
'msg': 'invalid device'
}
return json_response_from(response)


"""
Insert DeviceApplication DB [POST]
Update DeviceApplication DB [POST]
Expand Down
9 changes: 5 additions & 4 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'

EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_HOST_USER = 'phonelab.activation'
EMAIL_HOST_PASSWORD = 'phonelab2012'

EMAIL_PORT = 587
FROM_EMAIL = EMAIL_HOST_USER

ADMINS = (
# ('Micheal Benedict', 'micheala@buffalo.edu'),
('Micheal Benedict', 'micheala@buffalo.edu'),
('Taeyeon Ki', 'tki@buffalo.edu'),
)

Expand Down Expand Up @@ -80,7 +80,8 @@
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
#Deprecated in Django 1.4: This setting has been obsoleted by the django.contrib.staticfiles app integration.
#ADMIN_MEDIA_PREFIX = '/static/admin/'

# Additional locations of static files
STATICFILES_DIRS = (
Expand Down
Loading