~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/apps/userservice/__init__.py

  • Committer: dcoles
  • Date: 2008-08-06 04:52:58 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:984
Groups: Added userservice/create_project call. You can now create a project in 
the database if you have CAP_MANAGEPROJECTS.
Also tried to standardise dates. ISO 8601 seems to be the best option (short of  
using the unix time which is pretty unreadable).
The 'deadline' parameter expects a ISO 8601 like string (at the moment 
something in the same form as what json.js does when called to parse a Date 
instance ie.  `(new Date()).toJSON()`). Really we should be far more flexible 
than that, but Python surprsingly doesn't have a good way to deal with ISO 
dates. We might have to write our own parser - see common.util.parse_iso8601() 
for an absolute minimal implementation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
89
89
 
90
90
# userservice/create_project_set
91
91
# Required cap: CAP_MANAGEPROJECTS
92
 
# Creates a project set for a offering - returns the projectsetid
 
92
# Creates a project set for a offering
93
93
# Required:
94
94
#   offeringid, max_students_per_group
 
95
# Returns:
 
96
#   projectsetid
95
97
 
96
98
# userservice/create_project
97
99
# Required cap: CAP_MANAGEPROJECTS
99
101
# Required:
100
102
#   projectsetid
101
103
# Optional:
102
 
#   synopsys, url, deadline
 
104
#   synopsis, url, deadline
 
105
# Returns:
 
106
#   projectid
103
107
 
104
108
# userservice/create_group
105
109
# Required cap: CAP_MANAGEGROUPS
445
449
    req.write(response)
446
450
 
447
451
def handle_create_project_set(req, fields):
448
 
    """Creates a project set for a offering - returns the projectsetid"""
 
452
    """Required cap: CAP_MANAGEPROJECTS
 
453
    Creates a project set for a offering - returns the projectsetid
 
454
    Required:
 
455
        offeringid, max_students_per_group
 
456
    """
449
457
    
450
458
    if req.method != "POST":
451
459
        req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
461
469
        req.throw_error(req.HTTP_BAD_REQUEST,
462
470
            "Required: offeringid, max_students_per_group")
463
471
 
464
 
    projectsetid = "Not set"
465
472
    # Talk to the DB
466
473
    db = common.db.DB()
467
474
    dbquery = db.return_insert(
480
487
    req.content_type = "text/plain"
481
488
    req.write(response)
482
489
 
483
 
# TODO: write userservice/create_project
484
 
# Required cap: CAP_MANAGEPROJECTS
485
 
# Creates a project in a specific project set
486
 
# Required:
487
 
#   projectsetid
488
 
# Optional:
489
 
#   synopsys, url, deadline
 
490
def handle_create_project(req, fields):
 
491
    """Required cap: CAP_MANAGEPROJECTS
 
492
    Creates a project in a specific project set
 
493
    Required:
 
494
        projectsetid
 
495
    Optional:
 
496
        synopsis, url, deadline
 
497
    Returns:
 
498
        projectid
 
499
    """
 
500
    
 
501
    if req.method != "POST":
 
502
        req.throw_error(req.HTTP_METHOD_NOT_ALLOWED,
 
503
            "Only POST requests are valid methods to create_user.")
 
504
    # Check if this user has CAP_MANAGEPROJECTS
 
505
    if not req.user.hasCap(caps.CAP_MANAGEPROJECTS):
 
506
        req.throw_error(req.HTTP_FORBIDDEN,
 
507
        "You do not have permission to manage projects.")
 
508
    # Get required fields
 
509
    projectsetid = fields.getfirst('projectsetid')
 
510
    if projectsetid is None:
 
511
        req.throw_error(req.HTTP_BAD_REQUEST,
 
512
            "Required: projectsetid")
 
513
    # Get optional fields
 
514
    synopsis = fields.getfirst('synopsis')
 
515
    url = fields.getfirst('url')
 
516
    deadline = fields.getfirst('deadline')
 
517
    if deadline is not None:
 
518
        try:
 
519
            deadline = util.parse_iso8601(deadline).timetuple()
 
520
        except ValueError, e:
 
521
            req.throw_error(req.HTTP_BAD_REQUEST, e.message)
 
522
 
 
523
    # Talk to the DB
 
524
    db = common.db.DB()
 
525
    dbquery = db.return_insert(
 
526
        {
 
527
            'projectsetid': projectsetid,
 
528
            'synopsis': synopsis,
 
529
            'url': url,
 
530
            'deadline': deadline,
 
531
        },
 
532
        "project", # table
 
533
        frozenset(["projectsetid", "synopsis", "url", "deadline"]), # fields
 
534
        ["projectid"], # returns
 
535
    )
 
536
    db.close()
 
537
    
 
538
    response = cjson.encode(dbquery.dictresult()[0])
 
539
 
 
540
    req.content_type = "text/plain"
 
541
    req.write(response)
 
542
 
490
543
 
491
544
# TODO: write userservice/create_group
492
545
# Required cap: CAP_MANAGEGROUPS
510
563
    "get_user": handle_get_user,
511
564
    "get_enrolments": handle_get_enrolments,
512
565
    "create_project_set": handle_create_project_set,
 
566
    "create_project": handle_create_project,
513
567
}