~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-10 03:32:42 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1006
Groups: Use db transactions for risky usermanagement operations. (Such as when 
we have to create a svn repo) to try and maintain some database consistency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
603
603
 
604
604
    # Talk to the DB
605
605
    db = common.db.DB()
606
 
    dbquery = db.return_insert(
607
 
        {
608
 
            'projectsetid': projectsetid,
609
 
            'synopsis': synopsis,
610
 
            'url': url,
611
 
            'deadline': deadline,
612
 
        },
613
 
        "project", # table
614
 
        frozenset(["projectsetid", "synopsis", "url", "deadline"]), # fields
615
 
        ["projectid"], # returns
616
 
    )
617
 
    db.close()
 
606
    try:
 
607
        dbquery = db.return_insert(
 
608
            {
 
609
                'projectsetid': projectsetid,
 
610
                'synopsis': synopsis,
 
611
                'url': url,
 
612
                'deadline': deadline,
 
613
            },
 
614
            "project", # table
 
615
            frozenset(["projectsetid", "synopsis", "url", "deadline"]),
 
616
            ["projectid"], # returns
 
617
        )
 
618
    except Exception, e:
 
619
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
 
620
    finally:
 
621
        db.close()
618
622
    
619
623
    response = cjson.encode(dbquery.dictresult()[0])
620
624
 
657
661
    # Other fields
658
662
    createdby = db.get_user_loginid(req.user.login)
659
663
    epoch = time.localtime()
 
664
 
 
665
    # Begin transaction since things can go wrong
 
666
    db.start_transaction()
660
667
    try:
661
668
        dbquery = db.return_insert(
662
669
            {
671
678
                "epoch"]), # fields
672
679
            ["groupid"], # returns
673
680
        )
674
 
    except pg.ProgrammingError, e:
675
 
        req.throw_error(req.HTTP_FORBIDDEN, repr(e))
676
681
 
677
 
    singlerow = dbquery.dictresult()[0]
678
 
    groupid = singlerow['groupid']
 
682
        singlerow = dbquery.dictresult()[0]
 
683
        groupid = singlerow['groupid']
679
684
 
680
 
    # Create the groups repository
681
 
    # Get the arguments for usermgt.activate_user from the session
682
 
    # (The user must have already logged in to use this app)
 
685
        # Create the groups repository
 
686
        # Get the arguments for usermgt.activate_user from the session
 
687
        # (The user must have already logged in to use this app)
683
688
    
684
 
    # Find the rest of the parameters we need
685
 
    try:
 
689
        # Find the rest of the parameters we need
686
690
        offeringinfo = db.get_offering_info(projectsetid)
687
 
    except pg.ProgrammingError, e:
 
691
                
 
692
        subj_short_name = offeringinfo['subj_short_name']
 
693
        year = offeringinfo['year']
 
694
        semester = offeringinfo['semester']
 
695
 
 
696
        args = {
 
697
            "subj_short_name": subj_short_name,
 
698
            "year": year,
 
699
            "semester": semester,
 
700
            "groupnm": groupnm,
 
701
        }
 
702
        msg = {'create_group_repository': args}
 
703
 
 
704
        # Contact the usrmgt server
 
705
        try:
 
706
            usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
 
707
        except cjson.DecodeError, e:
 
708
            req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
 
709
                "Could not understand usrmgt server response: %s"%e.message)
 
710
 
 
711
        if 'response' not in usrmgt or usrmgt['response']=='failure':
 
712
            req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
 
713
                "Failure creating repository: %s"%str(usrmgt))
 
714
    
 
715
        # Everything went OK. Lock it in
 
716
        db.commit()
 
717
 
 
718
    except Exception, e:
 
719
        db.rollback()
688
720
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
689
 
    db.close()
690
 
 
691
 
    subj_short_name = offeringinfo['subj_short_name']
692
 
    year = offeringinfo['year']
693
 
    semester = offeringinfo['semester']
694
 
 
695
 
    args = {
696
 
        "subj_short_name": subj_short_name,
697
 
        "year": year,
698
 
        "semester": semester,
699
 
        "groupnm": groupnm,
700
 
    }
701
 
    msg = {'create_group_repository': args}
702
 
 
703
 
    # Contact the usrmgt server
704
 
    try:
705
 
        usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
706
 
    except cjson.DecodeError, e:
707
 
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
708
 
            "Could not understand usrmgt server response: %s"%e.message)
709
 
 
710
 
    if 'response' not in usrmgt or usrmgt['response']=='failure':
711
 
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
712
 
            "Failure creating repository: %s"%str(usrmgt))
 
721
    finally:
 
722
        db.close()
713
723
 
714
724
    response = cjson.encode(singlerow)
715
725
 
787
797
        req.throw_error(req.HTTP_BAD_REQUEST, repr(e))
788
798
 
789
799
    # Add assignment to database
 
800
    # Start transaction since things can go wrong
 
801
    db.start_transaction()
790
802
    try:
791
803
        dbquery = db.insert(
792
804
            {
796
808
            "group_member", # table
797
809
            frozenset(["loginid", "groupid"]), # fields
798
810
        )
799
 
    except pg.ProgrammingError, e:
800
 
        req.throw_error(req.HTTP_FORBIDDEN, repr(e))
801
 
 
802
 
    # Rebuild the svn config file
803
 
    # Contact the usrmgt server
804
 
    msg = {'rebuild_svn_group_config': {}}
805
 
    try:
806
 
        usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
807
 
    except cjson.DecodeError, e:
808
 
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
809
 
            "Could not understand usrmgt server response: %s"%e.message)
810
 
 
811
 
    if 'response' not in usrmgt or usrmgt['response']=='failure':
812
 
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
813
 
            "Failure creating repository: %s"%str(usrmgt))
 
811
 
 
812
        # Rebuild the svn config file
 
813
        # Contact the usrmgt server
 
814
        msg = {'rebuild_svn_group_config': {}}
 
815
        try:
 
816
            usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
 
817
        except cjson.DecodeError, e:
 
818
            req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
 
819
                "Could not understand usrmgt server response: %s"%e.message)
 
820
 
 
821
            if 'response' not in usrmgt or usrmgt['response']=='failure':
 
822
                req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
 
823
                    "Failure creating repository: %s"%str(usrmgt))
 
824
 
 
825
        # Everything went OK. Lock it into the database
 
826
        db.commit()
 
827
 
 
828
    except Exception, e:
 
829
        db.rollback()
 
830
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
 
831
    finally:
 
832
        db.close()
814
833
 
815
834
    return(cjson.encode({'response': 'okay'}))
816
835