示例#1
0
def login(request):
    if request.method == 'POST':
        # Login User
        email = request.POST['email']
        password = request.POST['password']

        user = auth.authenticate(email=email, password=password)

        if user is not None:
            auth.login(request, user)
            messages.success(request, 'You are now logged in')
            return redirect('admin_dashboard')
        else:
            messages.error(request, 'Invalid credentials')
            return redirect('login')
    else:
        context = sidebar(request)
        return render(request, 'accounts/login.html', context)
示例#2
0
def committee_admin(request):
    context = sidebar(request)

    committee = Committee.objects.order_by('order')
    context['committee'] = committee

    page = PageContent.objects.get(name='committee')
    context['page'] = page

    if request.method == 'POST':
        position = request.POST['position']
        member_name = request.POST['position_member']

        committee_position = Committee.objects.get(position=position)

        committee_position.member = member_name

        try:
            if request.FILES['position_photo']:
                print("new photo request")
                if committee_position.photo != 'photos/users/user_placeholder.jpg':
                    if os.path.isfile(committee_position.photo.path):
                        os.remove(committee_position.photo.path)

                photo = request.FILES['position_photo']
                committee_position.photo = photo

        except:
            pass

        try:
            if request.POST['delete_image']:
                if committee_position.photo != 'photos/users/user_placeholder.jpg':
                    if os.path.isfile(committee_position.photo.path):
                        os.remove(committee_position.photo.path)

                photo = 'photos/users/user_placeholder.jpg'
                committee_position.photo = photo
        except:
            pass

        committee_position.save()

    return render(request, 'custom_admin/committee_admin.html', context)
示例#3
0
def bulk_upload_map(request):
    context = sidebar(request)

    if request.method == 'POST':

        try:
            csv_upload = request.FILES['csv_upload']
        except:
            messages.error(request, 'Please upload a CSV to continue')
            return redirect('bulk_upload_main')

        if not csv_upload.name.endswith('.csv'):
            messages.error(request, 'Uploaded file MUST be a CSV file')
            return redirect('bulk_upload_main')

        fs = FileSystemStorage()
        filename = fs.save(csv_upload.name, csv_upload)
        uploaded_file_url = fs.path(filename)

        context['uploaded_file_url'] = uploaded_file_url

        with open(uploaded_file_url) as csv_file:
            reader = csv.DictReader(csv_file)
            headers = reader.fieldnames
            print(headers)

        if request.POST['data_type'] == 'awards':
            context['form_type'] = 'awards'

        if request.POST['data_type'] == 'members':
            context['form_type'] = 'members'

        if request.POST['data_type'] == 'intro':
            context['form_type'] = 'intro'

        if request.POST['data_type'] == 'comp':
            context['form_type'] = 'comp'

        if request.POST['data_type'] == 'motd':
            context['form_type'] = 'motd'

        context['headers'] = headers

    return render(request, 'custom_admin/bulk_upload_map.html', context)
示例#4
0
def gallery_create(request):
    context = sidebar(request)

    if request.method == 'POST':
        title = request.POST['album_title']
        uploaded_images = request.FILES.getlist('images')
        position = 0

        new_album = Album(title=title, quantity=0)
        new_album.save()

        album = Album.objects.get(pk=new_album.pk)

        for uploaded_image in uploaded_images:

            try:
                is_image = PILImage.open(uploaded_image)
                is_image.verify()
                position += 1
                image_upload = Image(album=album,
                                     image=uploaded_image,
                                     position=position)
                image_upload.save()

                image = Image.objects.get(pk=image_upload.pk)

                album.quantity += 1
                album.images.add(image)

            except UnidentifiedImageError:
                continue

        album.save()
        messages.success(request, 'Your album have been uploaded!')
        return redirect('gallery-admin')

    return render(request, 'gallery/gallery-create.html', context)
示例#5
0
def store_admin(request):
    context = sidebar(request)
    return render(request, 'store/store-admin.html', context)
示例#6
0
def store_main(request):
    context = sidebar(request)

    context['items'] = Item.objects.all()

    return render(request, 'store/store-main.html', context)
示例#7
0
def store_item(request, slug):
    context = sidebar(request)
    item = get_object_or_404(Item, slug=slug)
    context['item'] = item
    return render(request, 'store/store-item.html', context)
示例#8
0
def contact_events(request, contact_type, event_id):

    contact_event = get_object_or_404(EventIntro, pk=event_id)

    if request.method == 'POST':
        if contact_type == 'intro':
            contact_event_id = contact_event
            contact_fname = request.POST['contact_fname']
            contact_lname = request.POST['contact_lname']
            contact_email = request.POST['contact_email']
            contact_phone = request.POST['contact_phone']
            contact_age = request.POST['contact_age']
            contact_dominant = request.POST['contact_dominant']
            contact_experience = request.POST['contact_experience']
            contact_reason = request.POST['contact_reason']

            contact = ContactIntro(event_id=contact_event_id,
                                   first_name=contact_fname,
                                   last_name=contact_lname,
                                   email=contact_email,
                                   phone=contact_phone,
                                   age=contact_age,
                                   left_right=contact_dominant,
                                   shot_before=contact_experience,
                                   reason=contact_reason)

            contact.save()

            contact_event.current_participants = int(
                contact_event.current_participants) + 1

            if contact_dominant == 'left':
                contact_event.current_lh = int(contact_event.current_lh) + 1

            contact_event.save()

            #### Send user confirmation email ####
            plaintext = get_template(
                settings.BASE_DIR +
                '/templates/emails/contact-intro-course.txt')
            htmly = get_template(settings.BASE_DIR +
                                 '/templates/emails/contact-intro-course.html')

            context = {'contact': contact, 'event': contact_event}
            subject = "Geelong Archers: Introduction Course Sign Up Confirmation"
            from_email = '*****@*****.**'
            to_email = contact_email
            text_content = plaintext.render(context)
            html_content = htmly.render(context)

            msg = EmailMultiAlternatives(subject, text_content, from_email,
                                         [to_email])
            msg.attach_alternative(html_content, "text/html")
            msg.send()

            ##################################################

            #### Send Come and Try Coordinator confirmation email ####
            plaintext = get_template(
                settings.BASE_DIR +
                '/templates/emails/comeandtry-contact-intro-course.txt')
            htmly = get_template(
                settings.BASE_DIR +
                '/templates/emails/comeandtry-contact-intro-course.html')

            context = {'contact': contact, 'event': contact_event}
            subject = f"New Sign up for {contact_event}"
            from_email = '*****@*****.**'
            to_email = '*****@*****.**'
            text_content = plaintext.render(context)
            html_content = htmly.render(context)

            msg = EmailMultiAlternatives(subject, text_content, from_email,
                                         [to_email])
            msg.attach_alternative(html_content, "text/html")
            msg.send()

            ##################################################

            messages.success(request,
                             'You have successfully signed up to the event!')
            return redirect('events-main')

    context = sidebar(request)
    context['contact_type'] = contact_type
    context['contact_event'] = contact_event
    return render(request, 'contact_forms/contact-events.html', context)
示例#9
0
def contact_awards(request):
    context = sidebar(request)
    return render(request, 'contact_forms/contact-awards.html', context)
示例#10
0
def contact_admin(request):
    context = sidebar(request)
    return render(request, 'contact_forms/contact-admin.html', context)
示例#11
0
def events_edit(request, event_type, event_id):
    context = sidebar(request)

    if event_type == 'motd':
        event = get_object_or_404(EventMotD, pk=event_id)
        context['event'] = event
    if event_type == 'comp':
        event = get_object_or_404(EventComp, pk=event_id)
        context['event'] = event
    if event_type == 'intro':
        event = get_object_or_404(EventIntro, pk=event_id)
        context['event'] = event

    if request.method == 'POST':

        if event_type == 'motd':

            event.short_title = request.POST['event_short_title']
            event.short_description = request.POST['event_short_description']
            event.date_start = request.POST['event_date_start']
            event.date_end = request.POST['event_date_end']

            event.save()

            messages.success(request, 'Your event has been saved!')
            return redirect('events-admin')

        elif event_type == 'comp':

            event.title = request.POST['event_title']
            event.short_title = request.POST['event_short_title']
            event.description = request.POST['event_description']
            event.short_description = request.POST['event_short_description']
            event.max_participants = request.POST['event_max_participants']
            event.date_start = request.POST['event_date_start']
            event.date_end = request.POST['event_date_end']
            event.event_url = request.POST['event_url']

            event.save()

            messages.success(request, 'Your event has been saved!')
            return redirect('events-admin')

        elif event_type == 'intro':

            event.title = request.POST['event_title']
            event.short_title = request.POST['event_short_title']
            event.description = request.POST['event_description']
            event.short_description = request.POST['event_short_description']
            event.max_participants = request.POST['event_max_participants']
            event.max_lh = request.POST['event_max_lh']
            event.min_age = request.POST['event_min_age']
            event.date_start = request.POST['event_date_start']
            event.date_end = request.POST['event_date_end']

            event.save()

            messages.success(request, 'Your event has been saved!')
            return redirect('events-admin')

    context['event_type'] = event_type
    return render(request, 'events/events-edit.html', context)
示例#12
0
def bulk_upload_main(request):
    context = sidebar(request)
    return render(request, 'custom_admin/bulk_upload_main.html', context)
示例#13
0
def gallery_edit(request, album_id):
    context = sidebar(request)
    album = Album.objects.get(id=album_id)

    if request.method == 'POST':
        if request.POST['action_type'] == 'feature':

            images = request.POST.getlist('images')

            if len(images) > 1:
                messages.error(request,
                               'Only one image can be the feature image')
                return redirect('gallery-edit', album_id)
            else:
                album.feature_image = Image.objects.get(pk=images[0])
                album.save()
                messages.success(request, 'Featured Image has been updated')
                return redirect('gallery-edit', album_id)

        elif request.POST['action_type'] == 'delete':

            images = request.POST.getlist('images')
            count = 0

            for image in images:
                # image = Image.objects.get(pk=images[images.index(image)])
                image = Image.objects.get(pk=image)
                if album.feature_image == image:
                    album.feature_image = None

                # if album.feature_image == image:
                #     album.feature_image = None
                count += 1

                image.delete()
                album.quantity -= 1

            album.save()
            messages.success(request, f'{count} images deleted from album')
            return redirect('gallery-edit', album_id)

        elif request.FILES.getlist('upload_images'):

            uploaded_images = request.FILES.getlist('upload_images')
            exists = 0
            count = 0
            for uploaded_image in uploaded_images:
                try:
                    test = Image.objects.get(
                        image=f'photos/albums/{uploaded_image}')
                    print(test)
                    exists += 1
                    continue
                except:
                    image_upload = Image(album=album,
                                         image=uploaded_image,
                                         position=0)
                    image_upload.save()

                    image = Image.objects.get(pk=image_upload.pk)

                    count += 1
                    album.quantity += 1
                    album.images.add(image)

            album.save()
            if count > 0:
                messages.success(request, f'{count} images uploaded!')
            if exists > 0:
                messages.info(
                    request,
                    f'{exists} images already existed and were not uploaded')
            return redirect('gallery-edit', album_id)

        else:
            album.title = request.POST['album_title']

            if request.POST.get('album_hidden'):
                album.hidden = True
            else:
                album.hidden = False

            album.save()
            return redirect('gallery-admin')

    context['album'] = album
    return render(request, 'gallery/gallery-edit.html', context)
示例#14
0
def gallery_album(request, album_id):
    context = sidebar(request)
    album = Album.objects.get(id=album_id)

    context['album'] = album
    return render(request, 'gallery/gallery-album.html', context)
示例#15
0
def store_create(request):
    context = sidebar(request)
    return render(request, 'store/store-create.html', context)
示例#16
0
def events_create(request, event_type):

    if request.method == 'POST':
        if event_type == 'motd':
            event_short_title = request.POST['event_short_title']
            event_short_description = request.POST['event_short_description']
            event_date_start = request.POST['event_date_start']
            event_date_end = request.POST['event_date_end']

            event = EventMotD(short_title=event_short_title,
                              short_description=event_short_description,
                              date_start=event_date_start,
                              date_end=event_date_end)

            event.save()
            messages.success(request, 'Your event has been published!')
            return redirect('events-admin')

        elif event_type == 'comp':
            event_title = request.POST['event_title']
            event_short_title = request.POST['event_short_title']
            event_description = request.POST['event_description']
            event_short_description = request.POST['event_short_description']
            event_max_participants = request.POST['event_max_participants']
            event_date_start = request.POST['event_date_start']
            event_date_end = request.POST['event_date_end']
            event_url = request.POST['event_url']

            event = EventComp(title=event_title,
                              short_title=event_short_title,
                              description=event_description,
                              short_description=event_short_description,
                              max_participants=event_max_participants,
                              date_start=event_date_start,
                              date_end=event_date_end,
                              event_url=event_url)

            event.save()
            messages.success(request, 'Your event has been published!')
            return redirect('events-admin')

        elif event_type == 'intro':
            event_title = request.POST['event_title']
            event_short_title = request.POST['event_short_title']
            event_description = request.POST['event_description']
            event_short_description = request.POST['event_short_description']
            event_max_participants = request.POST['event_max_participants']
            event_min_age = request.POST['event_min_age']
            event_date_start = request.POST['event_date_start']
            event_date_end = request.POST['event_date_end']

            event = EventIntro(title=event_title,
                               short_title=event_short_title,
                               description=event_description,
                               short_description=event_short_description,
                               max_participants=event_max_participants,
                               min_age=event_min_age,
                               date_start=event_date_start,
                               date_end=event_date_end)

            event.save()

            messages.success(request, 'Your event has been published!')
            return redirect('events-admin')

    context = sidebar(request)
    context['event_type'] = event_type
    return render(request, 'events/events-create.html', context)
示例#17
0
def store_checkout(request):
    context = sidebar(request)

    if request.method == 'POST':
        order = Order.objects.get(user=request.user, ordered=False)
        # `source` is obtained with Stripe.js; see https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token
        form = PaymentForm
        token = request.POST.get('stripeToken')
        amount = int(order.get_total() * 100)  # amount is in cents

        try:
            charge = stripe.Charge.create(
                amount=amount,
                currency="aud",
                source=token,
            )

            # create the payment
            payment = Payment()
            payment.stripe_charge_id = charge.id
            payment.user = request.user
            payment.amount = order.get_total()
            payment.save()

            # assign the payment to the order
            order.ordered = True
            order.payment = payment
            order.save()

            messages.success(request, "Your order was successful")
            return redirect("/")

        except stripe.error.CardError as e:
            # Since it's a decline, stripe.error.CardError will be caught
            body = e.json_body
            err = body.get('error', {})
            messages.error(request, f"{err.get('message')}")
            return redirect("/")

        except stripe.error.RateLimitError as e:
            # Too many requests made to the API too quickly
            messages.error(request, "Rate limit error")
            return redirect("/")

        except stripe.error.InvalidRequestError as e:
            # Invalid parameters were supplied to Stripe's API
            messages.error(request, "Invalid parameters")
            return redirect("/")

        except stripe.error.AuthenticationError as e:
            # Authentication with Stripe's API failed
            # (maybe you changed API keys recently)
            messages.error(request, "Not authenticated")
            return redirect("/")

        except stripe.error.APIConnectionError as e:
            # Network communication with Stripe failed
            messages.error(request, "Network Error")
            return redirect("/")

        except stripe.error.StripeError as e:
            # Display a very generic error to the user, and maybe send
            # yourself an email
            messages.error(
                request, "Something went wrong. You were not charged. Please try again")
            return redirect("/")

        except Exception as e:
            # Send email to webmaster as error is server side
            messages.error(
                request, "A serious error occurred. We have been notified")
            return redirect("/")

    return render(request, 'store/store-checkout.html', context)
示例#18
0
def dashboard(request):
    context = sidebar(request)
    # Scrape Archers Diary
    if request.method == 'POST':
        requested_member = request.user
        scrape(request, requested_member)
        return redirect('dashboard')

    # Your Rating
    disciplines = ['Outdoor', 'Field', 'Indoor', 'Clout']
    division = request.user.division
    discipline = 'Outdoor'

    try:
        if request.GET['request_type'] == 'rating':
            discipline = request.GET['discipline']
    except:
        pass

    past_year = date.today() - timedelta(days=365)

    event_ratings = MemberEvents.objects.order_by('date').filter(
        member=request.user,
        date__gt=past_year,
        discipline=discipline,
        division=division).values_list('rating', flat=True)

    if len(event_ratings) < 10:
        event_ratings_reverse = MemberEvents.objects.order_by('-date').filter(
            member=request.user, discipline=discipline,
            division=division)[:10].values_list('rating', flat=True)

        event_ratings = event_ratings_reverse[::-1]

    if len(event_ratings) < 3:
        rating = 'N/A'
    else:
        rating = (event_ratings[0] + event_ratings[1] + event_ratings[2]) / 3
        for event_rating in event_ratings[2:]:
            if event_rating > rating:
                rating = rating + (event_rating - rating) / 2
                rating = math.floor(rating)

    # Your Classifications
    disciplines = ['Outdoor', 'Field', 'Indoor', 'Clout']
    division = request.user.division
    archer_class = request.user.archer_class

    try:
        if request.GET['request_type'] == 'classification':
            division = request.GET['division']
            archer_class = request.GET['archer_class']
    except:
        pass

    for discipline in disciplines:
        score_count = 0
        score_count_var = f'{discipline}_score'

        query = MemberClassificationAnnual.objects.order_by('-rank').filter(
            member=request.user,
            discipline=discipline,
            division=division,
            archer_class=archer_class,
            score_count__gte=3)[:1]

        if query:
            rank = (query[0].rank) + 1

            query2 = MemberClassificationAnnual.objects.filter(
                member=request.user,
                discipline=discipline,
                division=division,
                archer_class=archer_class,
                rank=rank)

            if query2:
                score_count = query2[0].score_count
                score_count = 3 - score_count
            else:
                score_count = 3

        else:
            query = MemberClassification.objects.order_by('-rank').filter(
                member=request.user,
                discipline=discipline,
                division=division,
                archer_class=archer_class,
                score_count__gte=3)[:1]

            if query:
                # If they are gold check in the annual table
                if query[0].rank == 4:
                    rank = (query[0].rank) + 1

                    query2 = MemberClassificationAnnual.objects.filter(
                        member=request.user,
                        discipline=discipline,
                        division=division,
                        archer_class=archer_class,
                        rank=rank)
                else:
                    rank = (query[0].rank) + 1

                    query2 = MemberClassification.objects.filter(
                        member=request.user,
                        discipline=discipline,
                        division=division,
                        archer_class=archer_class,
                        rank=rank)

                if query2:
                    score_count = query2[0].score_count
                    score_count = 3 - score_count
                else:
                    score_count = 3

        context[discipline] = query
        context[score_count_var] = score_count

    #  Your Events
    event_history = MemberEvents.objects.order_by('-date').filter(
        member=request.user)

    # Your Awards
    member_awards = MemberAwards.objects.order_by('award').filter(
        member=request.user)

    # Your Unclaimed Awards
    unclaimed_awards = MemberAvailableAwards.objects.order_by('award').filter(
        member=request.user)

    divisions = [
        'Recurve', 'Compound', 'Longbow', 'Barebow Recurve',
        'Barebow Compound', 'Crossbow'
    ]
    archer_classes = [
        'Cub', 'Intermediate', 'Cadet', '20 & Under', 'Open', 'Master',
        'Veteran', 'Veteran+', 'Para Archer', 'Vision Impaired'
    ]

    context['event_history'] = event_history
    context['member_awards'] = member_awards
    context['unclaimed_awards'] = unclaimed_awards

    context['values'] = request.GET
    context['divisions'] = divisions
    context['disciplines'] = disciplines
    context['archer_classes'] = archer_classes
    context['rating'] = rating

    return render(request, 'accounts/dashboard.html', context)