~robbyoconnor/sahana-eden/surveytool

« back to all changes in this revision

Viewing changes to models/00_utils.py

  • Committer: Robert O'Connor
  • Date: 2010-09-15 14:29:32 UTC
  • mfrom: (840.1.415 eden)
  • Revision ID: robby.oconnor@gmail.com-20100915142932-7uw2dm1yqdq09thb
merge with trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
    output = "S3 Debug: " + str(message)
69
69
    if value:
70
70
        output += ": " + str(value)
71
 
    
 
71
 
72
72
    print >> sys.stderr, output
73
73
 
74
74
# -----------------------------------------------------------------------------
75
75
# User Time Zone Operations:
 
76
#
 
77
def s3_get_utc_offset():
 
78
 
 
79
    """ Get the current UTC offset for the client """
 
80
 
 
81
    if auth.is_logged_in():
 
82
        # 1st choice is the personal preference (useful for GETs if user wishes to see times in their local timezone)
 
83
        offset = session.auth.user.utc_offset
 
84
        if offset:
 
85
            offset = offset.strip()
 
86
 
 
87
    if not offset:
 
88
        # 2nd choice is what the client provides in the hidden field (for form POSTs)
 
89
        offset = request.post_vars.get("_utc_offset", None)
 
90
        if offset:
 
91
            offset = int(offset)
 
92
            utcstr = offset < 0 and "UTC +" or "UTC -"
 
93
            hours = abs(int(offset/60))
 
94
            minutes = abs(int(offset % 60))
 
95
            offset = "%s%02d%02d" % (utcstr, hours, minutes)
 
96
 
 
97
    if not offset:
 
98
        # 3rd choice is the server default (what most clients should see the timezone as)
 
99
        offset = deployment_settings.L10n.utc_offset
 
100
 
 
101
    return offset
 
102
 
 
103
# Store last value in session
 
104
session.s3.utc_offset = s3_get_utc_offset()
 
105
 
 
106
 
76
107
def shn_user_utc_offset():
77
 
    """
78
 
        returns the UTC offset of the current user or None, if not logged in
79
 
    """
80
108
 
81
 
    if auth.is_logged_in():
82
 
        return db(db.auth_user.id == session.auth.user.id).select(db.auth_user.utc_offset, limitby=(0, 1)).first().utc_offset
83
 
    else:
84
 
        try:
85
 
            offset = db().select(db.s3_setting.utc_offset, limitby=(0, 1)).first().utc_offset
86
 
        except:
87
 
            offset = None
88
 
        return offset
 
109
    """ for backward compatibility """
 
110
    return session.s3.utc_offset
89
111
 
90
112
 
91
113
def shn_as_local_time(value):
98
120
 
99
121
    format="%Y-%m-%d %H:%M:%S"
100
122
 
101
 
    offset = IS_UTC_OFFSET.get_offset_value(shn_user_utc_offset())
 
123
    offset = IS_UTC_OFFSET.get_offset_value(session.s3.utc_offset)
102
124
 
103
125
    if offset:
104
126
        dt = value + datetime.timedelta(seconds=offset)
374
396
 
375
397
 
376
398
# -----------------------------------------------------------------------------
377
 
def shn_reference_field():
378
 
 
379
 
    return
380
 
 
 
399
def shn_table_links(reference):
 
400
    """
 
401
        Return a dict of tables & their fields which have references to the specified table
 
402
        - to be replaced by db[tablename]._referenced_by
 
403
    """
 
404
    tables = {}
 
405
    for table in db.tables:
 
406
        count = 0
 
407
        for field in db[table].fields:
 
408
            if db[table][field].type == "reference %s" % reference:
 
409
                if count == 0:
 
410
                    tables[table] = {}
 
411
                tables[table][count] = field
 
412
                count += 1
 
413
 
 
414
    return tables
381
415
 
382
416
# -----------------------------------------------------------------------------
383
417
def shn_insert_subheadings(form, tablename, subheadings):