def api_send_message(request, token, user_id):
    if request.method == 'POST':
        data = json.loads(request.body)
        text = data["message"]

        sender = User.objects.get(token=token)
        receiver = User.objects.get(pk=user_id)

        if text != "":

            if sender != None and receiver != None:

                message = Message(user=sender, receiver=receiver, message=text)
                message.save()

                return response_response(
                    ResponseObject('success', 'Message Sent', 200), 200)
            else:
                return response_response(
                    ResponseObject('error', 'Unknown Users', 400), 400)
        else:
            return response_response(
                ResponseObject('error', 'Message Cannot Be Empty', 400), 400)
    else:
        return response_response(ResponseObject('error', 'Bad Request', 400),
                                 400)
def api_post_feed_back(request, token):
    if request.method == 'POST':
        user = User.objects.filter(token=token).first()
        if user != None:

            feedback = FeedBackForm(
                request.POST,
                instance=FeedBack(user=User.objects.get(token=token)))

            if feedback.is_valid():
                feedback.save()

                return response_response(
                    ResponseObject('success', 'FeedBack Sent Successfully !!!',
                                   200), 200)
            else:
                return response_response(
                    ResponseObject(
                        'error', 'Fill All Fields With Rignt Data, Please !!!',
                        400), 400)
        else:
            return response_response(
                ResponseObject('error', 'Unknown User', 400), 400)
    else:
        return response_response(ResponseObject('error', 'Bad Request', 400),
                                 400)
def check_user_token(token):
    user = User.objects.filter(token=token).first()
    if user is not None:
        request.session['student'] = user.pk
    else:
        return response_response(ResponseObject('error', 'Unknown Token', 401),
                                 401)
Exemple #4
0
def add_new_book(request):
    if request.method == 'POST':
        user = User.objects.get(pk=request.session['user'])
        book = LibraryForm(request.POST, request.FILES, instance=Library(
                user=User.objects.get(pk=user.pk)
            ))
        if book.is_valid():
            book.save()
            messages.success(request, 'E-Book Added Successfully !!!')
            return HttpJsonResponse(ResponseObject('success', 'E-Book Added Successfully !!!', 200, 
                reverse('apcon_admin_ebooks')))
        else:
            return HttpJsonResponse(
                ResponseObject('error', 'Fill All Fields With Rignt Data, Please !!!', 400, msgs=event.errors.items()))
    else:
        return HttpJsonResponse(ResponseObject('error', 'Bad Request', 400))
Exemple #5
0
def add_new_article(request):
    if request.method == 'POST':
        user = User.objects.get(pk=request.session['user'])
        article = ArticleForm(request.POST, request.FILES, instance=Article(
                user=User.objects.get(pk=user.id)
            ))
        if article.is_valid():
            article.save()
            messages.success(request, 'Article Added Successfully !!!')
            return HttpJsonResponse(ResponseObject('success', 'Article Added Successfully !!!', 200, 
                reverse('apcon_admin_articles')))
        else:
            return HttpJsonResponse(
                ResponseObject('error', 'Fill All Fields With Rignt Data, Please !!!', 400, msgs=article.errors.items()))
    else:
        return HttpJsonResponse(ResponseObject('error', 'Bad Request', 400))
def api_get_articles(request, token):
    user = User.objects.filter(token=token).first()
    if user is not None:
        articles = Article.objects.all().order_by('-created_at')
        return data_response([article.to_json() for article in articles], 200)
    else:
        return response_response(ResponseObject('error', 'Unknown Token', 401),
                                 401)
def api_get_books(request, token):
    user = User.objects.filter(token=token).first()
    if user is not None:
        books = Library.objects.all().order_by('created_at')
        return data_response([book.to_json() for book in books], 200)
    else:
        return response_response(ResponseObject('error', 'Unknown Token', 401),
                                 401)
def api_post_comment(request, token):
    if request.method == 'POST':
        data = json.loads(request.body)
        text = data["comment"]
        article = data["article"]
        user = User.objects.filter(token=token).first()
        if user != None:
            comment = Comment(user=User.objects.get(pk=user.pk),
                              comment=text,
                              article=Article.objects.get(pk=article))
            comment.save()
            return data_response(comment.to_json(), 200)
        else:
            return response_response(
                ResponseObject('error', 'Unknown User', 400), 400)
    else:
        return response_response(ResponseObject('error', 'Bad Request', 400),
                                 400)
def api_get_users(request, token):
    user = User.objects.filter(token=token).first()
    if user is not None:
        users = User.objects.filter(
            user_type=user.user_type).order_by('-created_at')
        return data_response([u.to_json() for u in users], 200)
    else:
        return response_response(ResponseObject('error', 'Unknown Token', 401),
                                 401)
def api_get_events(request, token):
    user = User.objects.filter(token=token).first()
    if user is not None:
        events = Event.objects.filter(
            user__branch__pk=user.branch.pk).order_by('-created_at')
        return data_response([event.to_json() for event in events], 200)
    else:
        return response_response(ResponseObject('error', 'Unknown Token', 401),
                                 401)
def api_get_messages(request, token, user_id):
    user = User.objects.filter(token=token).first()
    if user is not None:
        messages = Message.objects.filter(
            Q(user__id=user.pk, receiver__id=user_id)
            | Q(user__id=user_id, receiver__id=user.pk)).order_by('created_at')
        return data_response([mess.to_json() for mess in messages], 200)
    else:
        return response_response(ResponseObject('error', 'Unknown Token', 401),
                                 401)
Exemple #12
0
def add_new_student(request):
    if request.method == 'POST':
        session = User.objects.get(pk=request.session['user'])
        user = UserForm(request.POST, instance=User(
                name='%s %s' % (request.POST.get('first_name'), request.POST.get('last_name')),
                date_of_birth=datetime.datetime.strptime('{0}'.format(request.POST.get('date_of_birth')), '%Y-%m-%d'),
                branch=Branch.objects.get(pk=session.branch.id),
                course=Course.objects.get(pk=request.POST.get('course')),
                user_type='student'
            ))
        if user.is_valid():
            user.save()
            messages.success(request, 'Student Added Successfully !!!')
            return HttpJsonResponse(ResponseObject('success', 'Student Added Successfully !!!', 200, 
                reverse('apcon_admin_students')))
        else:
            return HttpJsonResponse(
                ResponseObject('error', 'Fill All Fields With Rignt Data, Please !!!', 400, msgs=user.errors.items()))
    else:
        return HttpJsonResponse(ResponseObject('error', 'Bad Request', 400))
def api_login(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        roll_no = data["roll_no"].upper()
        password = data["password"]

        user = User.objects.filter(roll_no=roll_no).first()
        if user != None:
            if check_password(
                    password,
                    user.password) == True and user.user_type == 'student':
                return data_response(user.to_json(), 200)
            else:
                return response_response(
                    ResponseObject('error',
                                   'Incorrect Roll Number or Password', 400),
                    400)
        else:
            return response_response(
                ResponseObject('error', 'Incorrect Roll Number or Password',
                               400), 400)
    else:
        return response_response(ResponseObject('error', 'Bad Request', 400),
                                 400)
def api_update_user_image(request, token):
    if request.method == 'POST':
        user = User.objects.filter(token=token).first()
        if user != None:

            image_form = ProfileImageForm(request.POST, request.FILES)

            if image_form.is_valid():
                sessuser = User.objects.get(pk=user.pk)
                sessuser.image = image_form.cleaned_data['image']
                sessuser.save()

                return data_response(sessuser.to_json(), 200)
            else:
                return response_response(
                    ResponseObject(
                        'error', 'Fill All Fields With Rignt Data, Please !!!',
                        400), 400)
        else:
            return response_response(
                ResponseObject('error', 'Unknown User', 400), 400)
    else:
        return response_response(ResponseObject('error', 'Bad Request', 400),
                                 400)
def api_post_article(request, token):
    if request.method == 'POST':
        user = User.objects.filter(token=token).first()
        if user != None:
            article = ArticleForm(
                request.POST,
                request.FILES,
                instance=Article(user=User.objects.get(pk=user.id)))
            if article.is_valid():
                article.save()
                return response_response(
                    ResponseObject('success', 'Article Added Successfully !!!',
                                   200), 200)
            else:
                return response_response(
                    ResponseObject(
                        'error', 'Fill All Fields With Rignt Data, Please !!!',
                        400), 400)
        else:
            return response_response(
                ResponseObject('error', 'Unknown User', 400), 400)
    else:
        return response_response(ResponseObject('error', 'Bad Request', 400),
                                 400)