Exemplo n.º 1
0
 def create(self, request):
     # Création ou mise à jour d'un utilisateur
     serializer = core_serializers.UserRightSerializer(request.data)
     user = serializer.data
     new_user, created = core_models.UserRight.objects.update_or_create(
         login=user['login'],
         defaults={
             'right': user['right'],
             'name': user['name']
         })
     user = core_serializers.UserRightSerializer(new_user).data
     user = get_ginger_info(user['login'], user['right'], user['name'])
     return JsonResponse({'user': user})
Exemplo n.º 2
0
def _set_session_information(request, username, sessionid, connexion="full"):
    """Set username, sessionid, rights and user information into the session"""
    request.session['login'] = username
    request.session['payutc_session'] = sessionid
    request.session['connexion'] = connexion
    # Check for user rights into the database
    try:
        user_right_queryset = core_models.UserRight.objects.get(login = username)
        user_right = core_serializers.UserRightSerializer(user_right_queryset)
    except core_models.UserRight.DoesNotExist:
        user_right = None
        request.session['right'] = None

    if user_right is not None and user_right.data and user_right.data['right'] != 'N':
        request.session['right'] = user_right.data['right']
        try:
            member_queryset = core_models.Member.objects.filter(userright_id= user_right.data['id'], semestre__id=get_current_semester()).get()
            member = core_serializers.MemberSerializer(member_queryset)
        except core_models.Member.DoesNotExist:
            member = None
            request.session['member_id'] = None

        if member is not None and member.data and 'id' in member.data:
            request.session['member_id'] = member.data['id']

    # Get detailed user information with Ginger
    ginger = GingerClient()
    ginger_response = ginger.get_user_info(username)

    request.session['user'] = ginger_response['data']
    # Ajout 2h de session
    request.session.set_expiry(2*3600)
    return request
Exemplo n.º 3
0
def _is_user_member(login):
    """Check if the login has right"""
    try:
        user_queryset = core_models.UserRight.objects.filter(login= login).get()
        user = core_serializers.UserRightSerializer(user_queryset)
    except core_models.UserRight.DoesNotExist:
        return False
    if user.data["right"] not in ["A", "M"]:
        return False
    return True
Exemplo n.º 4
0
 def list(self, request):
     # Récupération utilisateurs avec droit Admin et Membre
     queryset = core_models.UserRight.objects.filter(
         Q(right='A') | Q(right='M'))
     serializer = core_serializers.UserRightSerializer(queryset, many=True)
     # Récupération des informations des utilisateurs via Ginger
     # Ajout des droits correspondants
     ginger = g.GingerClient()
     users = []
     for i, user in enumerate(serializer.data):
         users.append(
             get_ginger_info(user['login'], user['right'], user['name'],
                             ginger))
     return JsonResponse({'users': users})
Exemplo n.º 5
0
    def create(self, request):
        serializer = core_serializers.UserRightSerializer(request.data)
        user = serializer.data

        new_user, created = core_models.UserRight.objects.update_or_create(
            login=user['login'], defaults={'right': user['right']})
        user = core_serializers.UserRightSerializer(new_user).data
        status = 200
        if created:
            status = 201
        ginger = g.GingerClient()
        ginger_response = ginger.get_user_info(user['login'])
        if ginger_response['status'] != 200:
            user['user'] = None
        else:
            user['user'] = ginger_response['data']
        if user['right'] == 'A':
            user['right_detail'] = 'Admin'
        elif user['right'] == 'M':
            user['right_detail'] = 'Membre'
        elif user['right'] == 'N':
            user['right_detail'] = 'Aucun droit'

        return JsonResponse({'user': user})
Exemplo n.º 6
0
    def create(self, request):
        """Méthode pour générer 20 vainqueurs des goodies parmis les non-membres du Pic"""

        # Récupération des ventes entre une date de début et une date de fin
        # START doit être au format "AAAA-MM-JJ"
        # END doit être au format "AAAA-MM-JJ"
        # ROW_COUNT représente le nombre maximum de ventes récupérées
        sessionid = request.session['payutc_session']
        p = PayutcClient()
        p.login_admin()
        start = request.data['start_date'] + "T00:00:01.000Z"
        end = request.data['end_date'] + "T23:00:00.000Z"
        ROW_COUNT = 20000
        sales = p.get_sales(start=start, end=end,
                            row_count=ROW_COUNT)['transactions']

        # Recherche des membres dans le Pic actuel
        pic_members = []
        queryset = core_models.UserRight.objects.filter(
            Q(right='A') | Q(right='M'))
        serializer = core_serializers.UserRightSerializer(queryset, many=True)
        # Récupération des logins des membres du Pic pour les supprimer par la suite de la génération
        for i, user in enumerate(serializer.data):
            login = serializer.data[i]['login']
            pic_members.append(login)

        # Récupération aléatoire d'utilisateurs
        goodies_winners = []
        nb_sales = len(sales)
        while (len(goodies_winners) < 20):
            # Tant qqu'on a pas 20 vainqueurs on itère
            random_value = random.randint(1, nb_sales)
            user = sales[int(random_value) -
                         1]['rows'][0]['payments'][0]['buyer']
            user_description = user["first_name"] + " " + user["last_name"]
            # Si l'utilisateur n'est pas un membre du Pic ou un login déjà dans la liste des vainqueurs, ajout
            if (user["username"] not in pic_members
                    and user_description not in goodies_winners):
                goodies_winners.append(user_description)
                payutc_models.GoodiesWinner.objects.create(
                    winner=user_description, picked_up=False)

        # Renvoi des données
        queryset = payutc_models.GoodiesWinner.objects.all()
        serializer = payutc_serializers.GoodiesWinnerSerializer(queryset,
                                                                many=True)
        return JsonResponse({'winners': serializer.data})
Exemplo n.º 7
0
def _set_session_information(request, username, sessionid, connexion="full"):
    """Set username, sessionid, rights and user information into the session"""
    request.session['login'] = username
    request.session['payutc_session'] = sessionid
    request.session['connexion'] = connexion

    # Check for user rights into the database
    user_right_queryset = core_models.UserRight.objects.get(login=username)
    user_right = core_serializers.UserRightSerializer(user_right_queryset)
    if user_right is not None and user_right.data and user_right.data[
            'right'] != 'N':
        request.session['right'] = user_right.data['right']

    # Get detailed user information with Ginger
    ginger = GingerClient()
    ginger_response = ginger.get_user_info(username)
    request.session['user'] = ginger_response['data']
    # 2h de session
    request.session.set_expiry(2 * 3600)
    return request
Exemplo n.º 8
0
    def list(self, request):
        queryset = core_models.UserRight.objects.filter(
            Q(right='A') | Q(right='M'))
        serializer = core_serializers.UserRightSerializer(queryset, many=True)
        # Retrieve user from Ginger and add right details
        ginger = g.GingerClient()
        for i, user in enumerate(serializer.data):
            login = serializer.data[i]['login']
            ginger_response = ginger.get_user_info(login)

            if ginger_response['status'] != 200:
                serializer.data[i]['user'] = None
            else:
                serializer.data[i]['user'] = ginger_response['data']
            right = serializer.data[i]['right']
            if right == 'A':
                serializer.data[i]['right_detail'] = 'Admin'
            elif right == 'M':
                serializer.data[i]['right_detail'] = 'Membre'
        return JsonResponse({'users': serializer.data})