Exemplo n.º 1
0
def group_details(request, id, template='group/details.html'):
    d = {}
    item = get_object_or_404(dGroup, pk=id)
    #print(dGroup.objects.filter(id=1).all())
    d['members'] = item.members.all()

    d['form'] = GroupForm(instance=item)
    if request.method == 'POST':
        form = GroupForm(request.POST, instance=item)
        if form.is_valid():
            item = form.save()
            return JsonResponse(
                data={
                    'form': GroupForm(instance=item).as_p(),
                    'token': get_token(request)
                })
        else:
            d['form'] = form
            return JsonResponse(data={
                'form': d['form'].as_p(),
                'token': get_token(request)
            },
                                success=False)
    d['group'] = dGroup.objects.get(pk=id)
    return render(request, template, d)
Exemplo n.º 2
0
def import_dgroups(request):
    client = Utils.getDiscourseClient()
    groupsDict = client.groups()
    for groupDict in groupsDict:
        # gruppe in da erzeugen falls noch nicht vorhanden
        groupObj, created = dGroup.objects.get_or_create(
            discourse_group_id=groupDict['id'])
        if groupDict['automatic'] & (groupDict['name'] != "vertrauensstufe_0"):
            groupObj.delete()
        else:
            print("import: " + str(groupDict['id']) + " : " +
                  groupDict['name'])
            if created:
                print("created")
                for key in groupDict:
                    if key != "id":
                        setattr(groupObj, key, groupDict[key])
                groupObj.create_date = datetime.datetime.now()
            else:
                print("already exists")  #TODO: Discourse group aktualisieren ?
                #groupObj.update_date = datetime.datetime.now()
            groupObj.discourse_group_id = groupDict['id']
            groupObj.save()
            Utils.import_dgroup_members(groupDict['name'], groupObj.id)

    return JsonResponse()
Exemplo n.º 3
0
def set_basic_group(request):
    basicgroup = Utils.get_or_create_basic_group()
    print(basicgroup)
    users = User.objects.all().exclude(groups__id=1)
    print(users)
    for user in users:
        basicgroup.user_set.add(user)
    return JsonResponse()
Exemplo n.º 4
0
def testpd(request):
    client = Utils.getDiscourseClient()
    #client.groups()
    #result = client.watch_topic(57, username="******", notification_level=3)
    result = client.watch_category(5, "heiner")
    print(result)
    print(request.method)
    print(request)
    return JsonResponse()
Exemplo n.º 5
0
def user_edit(request, id, template='user/edit.html'):
    #TODO: wieder einbauen ?
    return JsonResponse()
    d = {}
    item = get_object_or_404(User, pk=id)
    print(serializers.serialize('json', [ item, ]))
    #pdb.set_trace()
    d['form'] = UserForm(instance=item)
    if request.method == 'POST':
        form = UserForm(request.POST, instance=item)
        if form.is_valid():
            item = form.save()
            return JsonResponse(data={'form': UserForm(instance=item).as_p(), 'token': get_token(request)})
        else:
            d['form'] = form
            return JsonResponse(data={'form': d['form'].as_p(), 'token': get_token(request)}, success=False)
    d['user'] = User.objects.get(pk=id)
    return render(request, template, d)
Exemplo n.º 6
0
def user_details(request, id, template='user/details.html'):
    d = {}
    user = item = get_object_or_404(User, pk=id)
    d['user_groups'] = item.dgroup_set.all()
    d['admin_groups'] = dGroup.objects.all().filter(user_groups__rights=1, user_groups__user_id=request.user.id).exclude(id__in=d['user_groups'])

    if request.user.is_superuser : 
        d['all_groups'] = dGroup.objects.all()
    else:
        d['all_groups'] = False
        
    d['form'] = HasDiscoGroups()
    
    if request.method == 'POST':
        print(request.POST)
        form = HasDiscoGroups(request.POST, instance=item)
        print(serializers.serialize('json', [ item, ]))
        if form.is_valid():
            print(form.cleaned_data)
            item = form.save()
            #form.save_m2m()
            return JsonResponse(data={'form': UserForm(instance=item).as_p(), 'token': get_token(request)})
        else:
            d['form'] = form
            return JsonResponse(data={'form': d['form'].as_p(), 'token': get_token(request)}, success=False)
    d['other_user'] = User.objects.get(pk=id)
    
    # check connection to discourse
    client = Utils.getDiscourseClient()
    try: 
        p = user.participant
        dUserDetails = client.user_all(user_id=p.discourse_user)
    except: 
        d['dUser_exists'] = False
    else:
        d['dUser_exists'] = True
        
    return render(request, template, d)
Exemplo n.º 7
0
def import_users(request):
    client = Utils.getDiscourseClient()
    usersDict = client.users()
    for userDict in usersDict:
        userObj, created = User.objects.get_or_create(
            username=userDict['username'])

        print(userDict)
        #print(created)

        if 'suspended_at' in userDict:
            userObj.is_active = False
            userObj.save()

        if created:
            # wenn der benutzer per login erzeugt wurde muss er hier aktualisiert werden
            userDetails = client.user_all(user_id=userDict['id'])
            ssoDetails = userDetails['single_sign_on_record']
            if ssoDetails != None:
                print(ssoDetails['external_email'])
                userObj.email = ssoDetails['external_email']

            for key in userDict:
                if key != "id":
                    setattr(userObj, key, userDict[key])

            if not userDetails['active']:
                userObj.is_active = False

            userObj.save()

        else:
            None
        #TODO overwrite Userdata in Discourse
        #print("TODO")

        try:
            p = userObj.participant
        except:
            p = Participant(user=userObj)
        p.discourse_user = discourse_user = userDict['id']
        p.save()

        print(userDict)
        print("-")
        print(userObj.__dict__)
        print("-")
    return JsonResponse()
Exemplo n.º 8
0
def deactivate_inactives(request):
    if (hasattr(settings, 'DEACTIVATE_INACTIVE_AFTER_DAYS')):
        logger.info("check deactivate_inactive")
        heute = datetime.datetime.now()
        tag_x = heute - datetime.timedelta(
            days=settings.DEACTIVATE_INACTIVE_AFTER_DAYS)
        users = User.objects.all().filter(date_joined__lte=tag_x,
                                          is_active=True).filter(
                                              Q(last_login__lte=tag_x)
                                              | Q(last_login=None))
        for user in users:
            #hier wird deaktiviert.
            di_logger.info("deaktiviere " + user.username +
                           " wegen inaktivität")
            deactivate_user(
                request, user.id, "Automatisch gesperrt wegen Inaktivität (" +
                str(settings.DEACTIVATE_INACTIVE_AFTER_DAYS) + " Tage)")
    return JsonResponse()
Exemplo n.º 9
0
def fix_users_email(request):

    users = User.objects.all().filter(email="")
    for user in users:
        print(user.username)
        client = Utils.getDiscourseClient()
        try:
            dUser = client.user(username=user.username)
        except:
            print("Der Benutzer " + user.username +
                  " scheint nicht sinvoll mit discourse verknüpft zu sein")
        else:
            emails = client.user_emails(user.username)
            user.email = emails['email']
            print(emails['email'])
            user.save()

    return JsonResponse()
Exemplo n.º 10
0
def init_set_departments(request):

    users = User.objects.all()
    for user in users:
        try:
            if user.participant.department is None:
                groups = user.dgroup_set.all()
                for group in groups:
                    if group.is_department:
                        user.participant.department = group
                        print(user)
                        print(user.participant.department)
                        user.participant.save()
            else:
                None
        except:
            None

        #dgroup = dGroup.objects.get(id=group_id)
        #otheruser = User.objects.get(id=user_id)
        #otheruser.participant.department = dgroup

    return JsonResponse()
Exemplo n.º 11
0
def user_groups_delete(request, id):
    item = User_Groups.objects.get(pk=id)
    item.delete()
    return JsonResponse()
Exemplo n.º 12
0
def user_delete(request, id):
    #TODO: wieder einbauen ?
    return JsonResponse()
    item = User.objects.get(pk=id)
    item.delete()
Exemplo n.º 13
0
def import_dgroup(request, groupname, da_group_id):
    limit = request.GET.get("limit") if request.GET.get("limit") else 1000
    offset = request.GET.get("offset") if request.GET.get("offset") else 0
    client = Utils.getDiscourseClient()
    Utils.import_dgroup_members(groupname, da_group_id, limit, offset)
    return JsonResponse()