def get_or_create_user( username, first_name=None, last_name=None, email=None, phone=None, show_email=None, show_profile=None, ): from baljan.ldapbackend import valid_username kwargs = { 'username': username, } u, created = User.objects.get_or_create(**kwargs) if first_name is not None: u.first_name = first_name if last_name is not None: u.last_name = last_name u.set_unusable_password() u.save() if valid_username(username): liu_email = u"%s@%s" % (username, settings.USER_EMAIL_DOMAIN) liu_eaddr, liu_eaddr_created = EmailAddress.objects.get_or_create( user=u, email=liu_email, verified=True, ) liu_eaddr.set_as_primary() if email: eaddr, eaddr_created = EmailAddress.objects.get_or_create( user=u, email=email, verified=True, ) eaddr.set_as_primary() p = u.get_profile() if show_email is not None: p.show_email = show_email if show_profile is not None: p.show_profile = show_profile if phone is not None: p.mobile_phone = phone p.save() return u, created
def job_opening(request, semester_name): opening_log = get_logger('baljan.jobopening') tpl = {} tpl['semester'] = sem = baljan.models.Semester.objects.get(name__exact=semester_name) user = request.user found_user = None if request.method == 'POST': if request.is_ajax(): # find user searched_for = request.POST['liu_id'] valid_search = valid_username(searched_for) if valid_search: results = baljan.search.for_person(searched_for, use_cache=False) if len(results) == 1: found_user = results[0] if valid_search and found_user is None: # FIXME: User should not be created immediately. First we # should tell whether or not he exists, then the operator # may choose to import the user. if exists_in_ldap(searched_for): opening_log.info('%s found in LDAP' % searched_for) found_user = fetch_user(searched_for) else: opening_log.info('%s not found in LDAP' % searched_for) info = {} info['user'] = None info['msg'] = _('enter liu id') info['msg_class'] = 'pending' info['all_ok'] = False if found_user: info['user'] = { 'username': found_user.username, 'text': "%s (%s)" % ( found_user.get_full_name(), found_user.username ), 'phone': found_user.get_profile().mobile_phone, 'url': found_user.get_absolute_url(), } info['msg'] = _('OK') info['msg_class'] = 'saved' info['all_ok'] = True else: if valid_search: info['msg'] = _('liu id unfound') info['msg_class'] = 'invalid' return HttpResponse(simplejson.dumps(info)) else: # the user hit save, assign users to shifts shift_ids = [int(x) for x in request.POST['shift-ids'].split('|')] usernames = request.POST['user-ids'].split('|') phones = request.POST['phones'].split('|') # Update phone numbers. for uname, phone in zip(usernames, phones): try: to_update = baljan.models.Profile.objects.get( user__username__exact=uname ) to_update.mobile_phone = phone to_update.save() except: opening_log.error('invalid phone for %s: %r' % (uname, phone)) # Assign to shifts. shifts_to_save = baljan.models.Shift.objects.filter(pk__in=shift_ids) users_to_save = User.objects.filter(username__in=usernames) for shift_to_save in shifts_to_save: for user_to_save in users_to_save: signup, created = baljan.models.ShiftSignup.objects.get_or_create( user=user_to_save, shift=shift_to_save ) if created: opening_log.info('%r created' % signup) else: opening_log.info('%r already existed' % signup) sched = workdist.Scheduler(sem) pairs = sched.pairs_from_db() slots = _pair_matrix(pairs) pair_javascript = {} for pair in pairs: pair_javascript[pair.label] = { 'shifts': [unicode(sh.name()) for sh in pair.shifts], 'ids': [sh.pk for sh in pair.shifts], } tpl['slots'] = slots tpl['pair_javascript'] = simplejson.dumps(pair_javascript) tpl['pairs_free'] = pairs_free = len([p for p in pairs if p.is_free()]) tpl['pairs_taken'] = pairs_taken = len([p for p in pairs if p.is_taken()]) tpl['pairs_total'] = pairs_total = pairs_free + pairs_taken tpl['pairs_taken_percent'] = int(round(pairs_taken * 100.0 / pairs_total)) return render_to_response('baljan/job_opening.html', tpl, context_instance=RequestContext(request))