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

« back to all changes in this revision

Viewing changes to ivle/webapp/userservice/__init__.py

Replace most userservice req.throw_error()s with new exceptions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
156
156
 
157
157
from ivle.webapp.base.views import BaseView
158
158
from ivle.webapp.base.plugins import ViewPlugin
159
 
from ivle.webapp.errors import NotFound
 
159
from ivle.webapp.errors import NotFound, BadRequest, Unauthorized
160
160
 
161
161
# The user must send this declaration message to ensure they acknowledge the
162
162
# TOS
223
223
        except AttributeError:
224
224
            declaration = None      # Will fail next test
225
225
        if declaration != USER_DECLARATION:
226
 
            req.throw_error(req.HTTP_BAD_REQUEST,
227
 
            "Please use the Terms of Service form instead of talking to "
228
 
            "this service directly.")
 
226
            raise BadRequest()
229
227
 
230
228
        # Make sure the user's status is "no_agreement", and set status to
231
229
        # pending, within the one transaction. This ensures we only do this
235
233
            # (Both to avoid redundant calls, and to stop disabled users from
236
234
            # re-enabling their accounts).
237
235
            if user.state != "no_agreement":
238
 
                req.throw_error(req.HTTP_BAD_REQUEST,
239
 
                "You have already agreed to the terms.")
 
236
                raise BadRequest("You have already agreed to the terms.")
240
237
            # Write state "pending" to ensure we don't try this again
241
238
            user.state = u"pending"
242
239
        except:
323
320
        if val is not None:
324
321
            create[f] = val
325
322
        else:
326
 
            req.throw_error(req.HTTP_BAD_REQUEST,
327
 
            "Required field %s missing." % repr(f))
 
323
            raise BadRequest("Required field %s missing." % repr(f))
328
324
    for f in create_user_fields_optional:
329
325
        val = fields.getfirst(f)
330
326
        if val is not None:
366
362
            raise AttributeError()
367
363
        if not fullpowers and login != req.user.login:
368
364
            # Not allowed to edit other users
369
 
            req.throw_error(req.HTTP_FORBIDDEN,
370
 
            "You do not have permission to update another user.")
 
365
            raise Unauthorized()
371
366
    except AttributeError:
372
367
        # If login not specified, update yourself
373
368
        login = req.user.login
384
379
        try:
385
380
            authenticate.authenticate(req.store, login, oldpassword)
386
381
        except AuthError:
 
382
            # XXX: Duplicated!
387
383
            req.headers_out['X-IVLE-Action-Error'] = \
388
384
                urllib.quote("Old password incorrect.")
389
 
            req.status = req.HTTP_BAD_REQUEST
390
 
            # Cancel all the changes made to user (including setting new pass)
391
 
            req.store.rollback()
392
 
            return
 
385
            raise BadRequest("Old password incorrect.")
393
386
 
394
387
    # Make a dict of fields to update
395
388
    for f in fieldlist:
419
412
        if login is None:
420
413
            raise AttributeError()
421
414
        if not fullpowers and login != req.user.login:
422
 
            # Not allowed to edit other users
423
 
            req.throw_error(req.HTTP_FORBIDDEN,
424
 
            "You do not have permission to see another user.")
 
415
            raise Unauthorized()
425
416
    except AttributeError:
426
417
        # If login not specified, update yourself
427
418
        login = req.user.login
456
447
        if user is None:
457
448
            raise AttributeError()
458
449
        if not fullpowers and user != req.user:
459
 
            # Not allowed to edit other users
460
 
            req.throw_error(req.HTTP_FORBIDDEN,
461
 
            "You do not have permission to see another user's subjects.")
 
450
            raise Unauthorized()
462
451
    except AttributeError:
463
452
        # If login not specified, update yourself
464
453
        user = req.user
489
478
 
490
479
    subjectid = fields.getfirst('subjectid')
491
480
    if subjectid is None:
492
 
        req.throw_error(req.HTTP_BAD_REQUEST,
493
 
            "Required: subjectid")
 
481
        raise BadRequest("Required: subjectid")
494
482
    try:
495
483
        subjectid = int(subjectid)
496
484
    except:
497
 
        req.throw_error(req.HTTP_BAD_REQUEST,
498
 
            "subjectid must be a integer")
 
485
        raise BadRequest("subjectid must be an integer")
499
486
 
500
487
    subject = req.store.get(ivle.database.Subject, subjectid)
501
488
 
518
505
 
519
506
    offeringid = fields.getfirst('offeringid')
520
507
    if offeringid is None:
521
 
        req.throw_error(req.HTTP_BAD_REQUEST,
522
 
            "Required: offeringid")
 
508
        raise BadRequest("Required: offeringid")
523
509
    try:
524
510
        offeringid = int(offeringid)
525
511
    except:
526
 
        req.throw_error(req.HTTP_BAD_REQUEST,
527
 
            "offeringid must be a integer")
 
512
        raise BadRequest("offeringid must be an integer")
528
513
 
529
514
    offering = req.store.get(ivle.database.Offering, offeringid)
530
515
 
560
545
    projectsetid = fields.getfirst('projectsetid').value
561
546
    groupnm = fields.getfirst('groupnm').value
562
547
    if projectsetid is None or groupnm is None:
563
 
        req.throw_error(req.HTTP_BAD_REQUEST,
564
 
            "Required: projectsetid, groupnm")
 
548
        raise BadRequest("Required: projectsetid, groupnm")
565
549
    groupnm = unicode(groupnm)
566
550
    try:
567
551
        projectsetid = int(projectsetid)
568
552
    except:
569
 
        req.throw_error(req.HTTP_BAD_REQUEST,
570
 
            "projectsetid must be an int")
 
553
        raise BadRequest("projectsetid must be an integer")
 
554
 
571
555
    # Get optional fields
572
556
    nick = fields.getfirst('nick').value
573
557
    if nick is not None:
601
585
        try:
602
586
            usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
603
587
        except cjson.DecodeError, e:
604
 
            req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
605
 
                "Could not understand usrmgt server response: %s"%e.message)
 
588
            raise Exception("Could not understand usrmgt server response:" +
 
589
                            e.message)
606
590
 
607
591
        if 'response' not in usrmgt or usrmgt['response']=='failure':
608
 
            req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
609
 
                "Failure creating repository: %s"%str(usrmgt))
610
 
    
 
592
            raise Exception("Failure creating repository: " + str(usrmgt))
 
593
 
611
594
        # Everything went OK. Lock it in
612
595
        req.store.commit()
613
596
 
628
611
    groupid = fields.getfirst('groupid')
629
612
    offeringid = fields.getfirst('offeringid')
630
613
    if groupid is None or offeringid is None:
631
 
        req.throw_error(req.HTTP_BAD_REQUEST,
632
 
            "Required: groupid, offeringid")
 
614
        raise BadRequest("Required: groupid, offeringid")
633
615
    try:
634
616
        groupid = int(groupid)
635
617
    except:
636
 
        req.throw_error(req.HTTP_BAD_REQUEST,
637
 
            "groupid must be an int")
 
618
        raise BadRequest("groupid must be an integer")
638
619
    group = req.store.get(ivle.database.ProjectGroup, groupid)
639
620
 
640
621
    try:
641
622
        offeringid = int(offeringid)
642
623
    except:
643
 
        req.throw_error(req.HTTP_BAD_REQUEST,
644
 
            "offeringid must be an int")
 
624
        raise BadRequest("offeringid must be an integer")
645
625
    offering = req.store.get(ivle.database.Offering, offeringid)
646
626
 
647
627
 
679
659
    login = fields.getfirst('login')
680
660
    groupid = fields.getfirst('groupid')
681
661
    if login is None or groupid is None:
682
 
        req.throw_error(req.HTTP_BAD_REQUEST,
683
 
            "Required: login, groupid")
 
662
        raise BadRequest("Required: login, groupid")
684
663
 
685
664
    group = req.store.get(ivle.database.ProjectGroup, int(groupid))
686
665
    user = ivle.database.User.get_by_login(req.store, login)
698
677
        try:
699
678
            usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
700
679
        except cjson.DecodeError, e:
701
 
            req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
702
 
                "Could not understand usrmgt server response: %s"%e.message)
 
680
            raise Exception("Could not understand usrmgt server response: %s" +
 
681
                            e.message)
703
682
 
704
683
            if 'response' not in usrmgt or usrmgt['response']=='failure':
705
 
                req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR,
706
 
                    "Failure creating repository: %s"%str(usrmgt))
 
684
                raise Exception("Failure creating repository: " + str(usrmgt))
707
685
    except Exception, e:
708
686
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
709
687