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

« back to all changes in this revision

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

  • Committer: William Grant
  • Date: 2009-01-20 02:36:42 UTC
  • mto: This revision was merged to the branch mainline in revision 1090.
  • Revision ID: grantw@unimelb.edu.au-20090120023642-rbgmprjihzii95zc
www/apps/userservice: Port create_group to Storm.
ivle.db.get_offering_info: Kill. Unused.

Show diffs side-by-side

added added

removed removed

Lines of Context:
139
139
 
140
140
import os
141
141
import sys
142
 
import time
 
142
import datetime
143
143
 
144
144
import cjson
145
145
import pg
652
652
        req.throw_error(req.HTTP_FORBIDDEN,
653
653
        "You do not have permission to manage groups.")
654
654
    # Get required fields
655
 
    projectsetid = fields.getfirst('projectsetid')
656
 
    groupnm = fields.getfirst('groupnm')
 
655
    projectsetid = fields.getfirst('projectsetid').value
 
656
    groupnm = fields.getfirst('groupnm').value
657
657
    if projectsetid is None or groupnm is None:
658
658
        req.throw_error(req.HTTP_BAD_REQUEST,
659
659
            "Required: projectsetid, groupnm")
 
660
    groupnm = unicode(groupnm)
660
661
    try:
661
662
        projectsetid = int(projectsetid)
662
663
    except:
663
664
        req.throw_error(req.HTTP_BAD_REQUEST,
664
665
            "projectsetid must be an int")
665
666
    # Get optional fields
666
 
    nick = fields.getfirst('nick')
667
 
 
668
 
    # Talk to the DB
669
 
    db = ivle.db.DB()
670
 
    # Other fields
671
 
    createdby = req.user.id
672
 
    epoch = time.localtime()
 
667
    nick = fields.getfirst('nick').value
 
668
    if nick is not None:
 
669
        nick = unicode(nick)
673
670
 
674
671
    # Begin transaction since things can go wrong
675
 
    db.start_transaction()
676
672
    try:
677
 
        dbquery = db.return_insert(
678
 
            {
679
 
                'groupnm': groupnm,
680
 
                'projectsetid': projectsetid,
681
 
                'nick': nick,
682
 
                'createdby': createdby,
683
 
                'epoch': epoch,
684
 
            },
685
 
            "project_group", # table
686
 
            frozenset(["groupnm", "projectsetid", "nick", "createdby",
687
 
                "epoch"]), # fields
688
 
            ["groupid"], # returns
689
 
        )
690
 
 
691
 
        singlerow = dbquery.dictresult()[0]
692
 
        groupid = singlerow['groupid']
 
673
        group = ivle.database.ProjectGroup(name=groupnm,
 
674
                                           project_set_id=projectsetid,
 
675
                                           nick=nick,
 
676
                                           created_by=req.user,
 
677
                                           epoch=datetime.datetime.now())
 
678
        req.store.add(group)
693
679
 
694
 
        # Create the groups repository
695
 
        # Get the arguments for usermgt.activate_user from the session
696
 
        # (The user must have already logged in to use this app)
697
 
    
698
 
        # Find the rest of the parameters we need
699
 
        offeringinfo = db.get_offering_info(projectsetid)
700
 
                
701
 
        subj_short_name = offeringinfo['subj_short_name']
702
 
        year = offeringinfo['year']
703
 
        semester = offeringinfo['semester']
 
680
        # Create the group repository
 
681
        # Yes, this is ugly, and it would be nice to just pass in the groupid,
 
682
        # but the object isn't visible to the extra transaction in
 
683
        # usrmgt-server until we commit, which we only do once the repo is
 
684
        # created.
 
685
        offering = group.project_set.offering
704
686
 
705
687
        args = {
706
 
            "subj_short_name": subj_short_name,
707
 
            "year": year,
708
 
            "semester": semester,
709
 
            "groupnm": groupnm,
 
688
            "subj_short_name": offering.subject.short_name,
 
689
            "year": offering.semester.year,
 
690
            "semester": offering.semester.semester,
 
691
            "groupnm": group.name,
710
692
        }
711
693
        msg = {'create_group_repository': args}
712
694
 
722
704
                "Failure creating repository: %s"%str(usrmgt))
723
705
    
724
706
        # Everything went OK. Lock it in
725
 
        db.commit()
 
707
        req.store.commit()
726
708
 
727
709
    except Exception, e:
728
 
        db.rollback()
729
710
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
730
 
    finally:
731
 
        db.close()
732
 
 
733
 
    response = cjson.encode(singlerow)
734
711
 
735
712
    req.content_type = "text/plain"
736
 
    req.write(response)
 
713
    req.write('')
737
714
 
738
715
def handle_get_group_membership(req, fields):
739
716
    """ Required cap: None