text: from django.shortcuts import render, redirect
  from django.contrib.auth.decorators import login_required
  from django.http import HttpResponse, Http404, HttpResponseRedirect
  from django.http import JsonResponse
  from .models import Article, MoringaMerch
  from .serializer import MerchSerializer
  from .forms import NewsLetterForm, NewArticleForm
  from .email import send_welcome_email
  import datetime as dt
  from rest_framework.response import Response
  from rest_framework.views import APIView
  
  # Create your views here.
  def news_today(request):
      date = dt.date.today()
      news = Article.todays_news()
      form = NewsLetterForm()
      return render(request, 'all-news/today-news.html', {"date": date,"news":news, "letterForm": form})
  
  def past_days_news(request, past_date):
      try:
          # Converts data from the string Url
          date = dt.datetime.strptime(past_date, '%Y-%m-%d').date()
      except ValueError:
          # Raise 404 error when ValueError is thrown
          raise Http404()
          assert False
  
      if date == dt.date.today():
          return redirect(news_today)
  
      news = Article.days_news(date)
      return render(request, 'all-news/past-news.html',{"date": date,"news":news})
      
  @login_required(login_url='/accounts/login/')
  def new_article(request):
      current_user = request.user
      if request.method == 'POST':
          form = NewArticleForm(request.POST, request.FILES)
          if form.is_valid():
              article = form.save(commit=False)
              article.editor = current_user
              article.save()
          return redirect('NewsToday')
  
      else:
          form = NewArticleForm()
      return render(request, 'new_article.html', {"form": form})
  
  def search_results(request):
      if 'article' in request.GET and request.GET["article"]:
          search_term = request.GET.get("article")
          searched_articles = Article.search_by_title(search_term)
          message = f"{search_term}"
  
          return render(request, 'all-news/search.html',{"message":message,"articles": searched_articles})
      else:
          message = "You haven't searched for any term"
          return render(request, 'all-news/search.html',{"message":message})
  
  def newsletter(request):
      name = request.POST.get('your_name')
      email = request.POST.get('email')
  
      recipient = NewsLetterRecipients(name=name, email=email)
      recipient.save()
      send_welcome_email(name, email)
      data = {'success': 'You have been successfully added to mailing list'}
      return JsonResponse(data)
  
  # API View to handle requests
  class MerchList(APIView):
      def get(self, request, format=None):
          all_merch = MoringaMerch.objects.all()
          serializers = MerchSerializer(all_merch, many=True)
          return Response(serializers.data)