Commit 11b91150 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Make merchandise multilingual, fix showing price in frontend and add a migration script

parent 4c9f3487
from django.contrib import admin
from utils.translation import TranslatedModelAdmin
from .models import MerchandiseItem
admin.site.register(MerchandiseItem)
@admin.register(MerchandiseItem)
class MerchandiseItemAdmin(TranslatedModelAdmin):
fields = ('name', 'price',
'description', 'image',)
from django.core.files.base import ContentFile
from django.core.management.base import BaseCommand
from decimal import Decimal
from merchandise.models import MerchandiseItem
from bs4 import BeautifulSoup
import requests
import os
def filefield_from_url(filefield, url):
file = ContentFile(requests.get(url).content)
filefield.save(os.path.basename(url), file)
class Command(BaseCommand):
help = "Scrapes the merchandise from the old Thalia website"
def handle(self, *args, **options):
input_val = input(
"Do you want to delete all existing objects? (type yes or no) ")
if input_val == 'yes':
MerchandiseItem.objects.all().delete()
return
session = requests.Session()
url = "https://thalia.nu/index.php/association/merchandise"
src = session.get(url).text
soup = BeautifulSoup(src, 'lxml')
for item in soup.find_all("div", {"class": "post", "id": "item"}):
image_url = item.find("img")['src'].replace('/small', '/large')
title = item.find("h1", {"id": "merchandise-title"}).text
desc = item.find("p", {"id": "merchandise-desc"}).text
price = Decimal(item.find("div", {"id": "merchandise-price"})
.text[9:].replace(',', '.'))
merch = MerchandiseItem()
merch.name_nl = title
merch.name_en = title
merch.description_nl = desc
merch.description_en = desc
merch.price = price
filefield_from_url(merch.image, image_url)
merch.save()
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-09-16 09:50
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('merchandise', '0002_auto_20160805_1730'),
]
operations = [
migrations.RemoveField(
model_name='merchandiseitem',
name='description',
),
migrations.RemoveField(
model_name='merchandiseitem',
name='name',
),
migrations.AddField(
model_name='merchandiseitem',
name='description_en',
field=models.TextField(default='', verbose_name='description (EN)'),
preserve_default=False,
),
migrations.AddField(
model_name='merchandiseitem',
name='description_nl',
field=models.TextField(default='', verbose_name='description (NL)'),
preserve_default=False,
),
migrations.AddField(
model_name='merchandiseitem',
name='name_en',
field=models.CharField(default='', max_length=200, verbose_name='name (EN)'),
preserve_default=False,
),
migrations.AddField(
model_name='merchandiseitem',
name='name_nl',
field=models.CharField(default='', max_length=200, verbose_name='name (NL)'),
preserve_default=False,
),
]
from django.db import models
from utils.translation import MultilingualField, ModelTranslateMeta
class MerchandiseItem(models.Model):
name = models.CharField(max_length=200)
class MerchandiseItem(models.Model, metaclass=ModelTranslateMeta):
name = MultilingualField(models.CharField, max_length=200)
price = models.DecimalField(max_digits=5, decimal_places=2)
description = models.TextField()
description = MultilingualField(models.TextField)
image = models.ImageField(upload_to='public/merchandise')
def __str__(self):
......
......@@ -3,7 +3,9 @@
{% load i18n %}
{% block body %}
<h1>{% trans "Merchandise" %}</h1><p>{% trans "Below you can find all the merchandise Thalia has available. During every lunch break, the merchandise is for sale in the board room (HG00.150). Outside those hours, please make an appointment or approach the board over drinks." %}</p
<h1>{% trans "Merchandise" %}</h1>
<p class="text-center">{% trans "Below you can find all the merchandise Thalia has available. During every lunch break, the merchandise is for sale in the board room (HG00.150). Outside those hours, please make an appointment or approach the board over drinks." %}</p
{% for item in items %}
......@@ -17,7 +19,7 @@
<div class="span9" id="merchandise-text">
<h1 id="merchandise-title">{{ item.name }}</h1>
<p id="merchandise-desc">{{ item.description }}</p>
<div id="merchandise-price">{% blocktrans %}Price: € {{ item.price }}{% endblocktrans %}</div>
<div id="merchandise-price">{% blocktrans with price=item.price %}Price: € {{ price }}{% endblocktrans %}</div>
</div>
</div>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment