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

« back to all changes in this revision

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

Remove some useless try-except blocks that just raise a 500.

Also get rid of some store.commit()s which are done by dispatch anyway.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# Provides an Ajax service for handling user management requests.
23
23
# This includes when a user logs in for the first time.
24
24
 
25
 
# NOTE: This app does NOT require authentication. This is because otherwise it
26
 
# would be blocked from receiving requests to activate when the user is trying
27
 
# to accept the TOS.
28
 
 
29
 
# It must do its own authentication and authorization.
30
 
 
31
25
### Actions ###
32
26
 
33
27
# The one-and-only path segment to userservice determines the action being
218
212
    """
219
213
 
220
214
    try:
221
 
        try:
222
 
            declaration = fields.getfirst('declaration')
223
 
        except AttributeError:
224
 
            declaration = None      # Will fail next test
225
 
        if declaration != USER_DECLARATION:
226
 
            raise BadRequest()
227
 
 
228
 
        # Make sure the user's status is "no_agreement", and set status to
229
 
        # pending, within the one transaction. This ensures we only do this
230
 
        # one time.
231
 
        try:
232
 
            # Check that the user's status is "no_agreement".
233
 
            # (Both to avoid redundant calls, and to stop disabled users from
234
 
            # re-enabling their accounts).
235
 
            if user.state != "no_agreement":
236
 
                raise BadRequest("You have already agreed to the terms.")
237
 
            # Write state "pending" to ensure we don't try this again
238
 
            user.state = u"pending"
239
 
        except:
240
 
            req.store.rollback()
241
 
            raise
242
 
        req.store.commit()
243
 
 
244
 
        # Get the arguments for usermgt.activate_user from the session
245
 
        # (The user must have already logged in to use this app)
246
 
        args = {
247
 
            "login": user.login,
248
 
        }
249
 
        msg = {'activate_user': args}
250
 
 
251
 
        # Release our lock on the db so usrmgt can write
252
 
        req.store.rollback()
253
 
 
254
 
        # Try and contact the usrmgt server
255
 
        try:
256
 
            response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
257
 
        except cjson.DecodeError:
258
 
            # Gave back rubbish - set the response to failure
259
 
            response = {'response': 'usrmgt-failure'}
260
 
 
261
 
        # Get the staus of the users request
262
 
        try:
263
 
            status = response['response']
264
 
        except KeyError:
265
 
            status = 'failure'
266
 
        
267
 
        if status == 'okay':
268
 
            user.state = u"enabled"
269
 
        else:
270
 
            # Reset the user back to no agreement
271
 
            user.state = u"no_agreement"
272
 
            req.store.commit()
273
 
 
274
 
        # Write the response
275
 
        req.content_type = "text/plain"
276
 
        req.write(cjson.encode(response))
 
215
        declaration = fields.getfirst('declaration')
 
216
    except AttributeError:
 
217
        declaration = None      # Will fail next test
 
218
    if declaration != USER_DECLARATION:
 
219
        raise BadRequest()
 
220
 
 
221
    # Make sure the user's status is "no_agreement", and set status to
 
222
    # pending, within the one transaction. This ensures we only do this
 
223
    # one time.
 
224
    try:
 
225
        # Check that the user's status is "no_agreement".
 
226
        # (Both to avoid redundant calls, and to stop disabled users from
 
227
        # re-enabling their accounts).
 
228
        if user.state != "no_agreement":
 
229
            raise BadRequest("You have already agreed to the terms.")
 
230
        # Write state "pending" to ensure we don't try this again
 
231
        user.state = u"pending"
277
232
    except:
278
233
        req.store.rollback()
279
234
        raise
 
235
    req.store.commit()
 
236
 
 
237
    # Get the arguments for usermgt.activate_user from the session
 
238
    # (The user must have already logged in to use this app)
 
239
    args = {
 
240
        "login": user.login,
 
241
    }
 
242
    msg = {'activate_user': args}
 
243
 
 
244
    # Release our lock on the db so usrmgt can write
 
245
    req.store.rollback()
 
246
 
 
247
    # Try and contact the usrmgt server
 
248
    try:
 
249
        response = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
 
250
    except cjson.DecodeError:
 
251
        # Gave back rubbish - set the response to failure
 
252
        response = {'response': 'usrmgt-failure'}
 
253
 
 
254
    # Get the staus of the users request
 
255
    try:
 
256
        status = response['response']
 
257
    except KeyError:
 
258
        status = 'failure'
 
259
 
 
260
    if status == 'okay':
 
261
        user.state = u"enabled"
 
262
    else:
 
263
        # Reset the user back to no agreement
 
264
        user.state = u"no_agreement"
 
265
 
 
266
    # Write the response
 
267
    req.content_type = "text/plain"
 
268
    req.write(cjson.encode(response))
280
269
 
281
270
create_user_fields_required = [
282
271
    'login', 'fullname', 'rolenm'
331
320
    user = ivle.database.User(**create)
332
321
    req.store.add(user)
333
322
    ivle.pulldown_subj.enrol_user(req.store, user)
334
 
    req.store.commit()
335
323
 
336
324
    req.content_type = "text/plain"
337
325
    req.write(str(user.unixid))
393
381
        else:
394
382
            pass
395
383
 
396
 
    req.store.commit()
397
 
 
398
384
    req.content_type = "text/plain"
399
385
    req.write('')
400
386
 
514
500
    offering = req.store.get(ivle.database.Offering, offeringid)
515
501
 
516
502
    dict_projectsets = []
517
 
    try:
518
 
        for p in offering.project_sets:
519
 
            dict_projectsets.append({
520
 
                'projectsetid': p.id,
521
 
                'max_students_per_group': p.max_students_per_group,
522
 
                'groups': [{'groupid': g.id,
523
 
                            'groupnm': g.name,
524
 
                            'nick': g.nick} for g in p.project_groups]
525
 
            })
526
 
    except Exception, e:
527
 
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
 
503
    for p in offering.project_sets:
 
504
        dict_projectsets.append({
 
505
            'projectsetid': p.id,
 
506
            'max_students_per_group': p.max_students_per_group,
 
507
            'groups': [{'groupid': g.id,
 
508
                        'groupnm': g.name,
 
509
                        'nick': g.nick} for g in p.project_groups]
 
510
        })
528
511
 
529
512
    response = cjson.encode(dict_projectsets)
530
513
    req.write(response)
557
540
    if nick is not None:
558
541
        nick = unicode(nick)
559
542
 
560
 
    # Begin transaction since things can go wrong
 
543
    group = ivle.database.ProjectGroup(name=groupnm,
 
544
                                       project_set_id=projectsetid,
 
545
                                       nick=nick,
 
546
                                       created_by=req.user,
 
547
                                       epoch=datetime.datetime.now())
 
548
    req.store.add(group)
 
549
 
 
550
    # Create the group repository
 
551
    # Yes, this is ugly, and it would be nice to just pass in the groupid,
 
552
    # but the object isn't visible to the extra transaction in
 
553
    # usrmgt-server until we commit, which we only do once the repo is
 
554
    # created.
 
555
    offering = group.project_set.offering
 
556
 
 
557
    args = {
 
558
        "subj_short_name": offering.subject.short_name,
 
559
        "year": offering.semester.year,
 
560
        "semester": offering.semester.semester,
 
561
        "groupnm": group.name,
 
562
    }
 
563
    msg = {'create_group_repository': args}
 
564
 
 
565
    # Contact the usrmgt server
561
566
    try:
562
 
        group = ivle.database.ProjectGroup(name=groupnm,
563
 
                                           project_set_id=projectsetid,
564
 
                                           nick=nick,
565
 
                                           created_by=req.user,
566
 
                                           epoch=datetime.datetime.now())
567
 
        req.store.add(group)
568
 
 
569
 
        # Create the group repository
570
 
        # Yes, this is ugly, and it would be nice to just pass in the groupid,
571
 
        # but the object isn't visible to the extra transaction in
572
 
        # usrmgt-server until we commit, which we only do once the repo is
573
 
        # created.
574
 
        offering = group.project_set.offering
575
 
 
576
 
        args = {
577
 
            "subj_short_name": offering.subject.short_name,
578
 
            "year": offering.semester.year,
579
 
            "semester": offering.semester.semester,
580
 
            "groupnm": group.name,
581
 
        }
582
 
        msg = {'create_group_repository': args}
583
 
 
584
 
        # Contact the usrmgt server
585
 
        try:
586
 
            usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
587
 
        except cjson.DecodeError, e:
588
 
            raise Exception("Could not understand usrmgt server response:" +
589
 
                            e.message)
590
 
 
591
 
        if 'response' not in usrmgt or usrmgt['response']=='failure':
592
 
            raise Exception("Failure creating repository: " + str(usrmgt))
593
 
 
594
 
        # Everything went OK. Lock it in
595
 
        req.store.commit()
596
 
 
597
 
    except Exception, e:
598
 
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
 
567
        usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
 
568
    except cjson.DecodeError, e:
 
569
        raise Exception("Could not understand usrmgt server response:" +
 
570
                        e.message)
 
571
 
 
572
    if 'response' not in usrmgt or usrmgt['response']=='failure':
 
573
        raise Exception("Failure creating repository: " + str(usrmgt))
599
574
 
600
575
    req.content_type = "text/plain"
601
576
    req.write('')
667
642
    # Add membership to database
668
643
    # We can't keep a transaction open until the end here, as usrmgt-server
669
644
    # needs to see the changes!
 
645
    group.members.add(user)
 
646
    req.store.commit()
 
647
 
 
648
    # Rebuild the svn config file
 
649
    # Contact the usrmgt server
 
650
    msg = {'rebuild_svn_group_config': {}}
670
651
    try:
671
 
        group.members.add(user)
672
 
        req.store.commit()
673
 
 
674
 
        # Rebuild the svn config file
675
 
        # Contact the usrmgt server
676
 
        msg = {'rebuild_svn_group_config': {}}
677
 
        try:
678
 
            usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
679
 
        except cjson.DecodeError, e:
680
 
            raise Exception("Could not understand usrmgt server response: %s" +
681
 
                            e.message)
682
 
 
683
 
            if 'response' not in usrmgt or usrmgt['response']=='failure':
684
 
                raise Exception("Failure creating repository: " + str(usrmgt))
685
 
    except Exception, e:
686
 
        req.throw_error(req.HTTP_INTERNAL_SERVER_ERROR, repr(e))
 
652
        usrmgt = chat.chat(usrmgt_host, usrmgt_port, msg, usrmgt_magic)
 
653
    except cjson.DecodeError, e:
 
654
        raise Exception("Could not understand usrmgt server response: %s" +
 
655
                        e.message)
 
656
 
 
657
        if 'response' not in usrmgt or usrmgt['response']=='failure':
 
658
            raise Exception("Failure creating repository: " + str(usrmgt))
687
659
 
688
660
    return(cjson.encode({'response': 'okay'}))
689
661