diff --git a/article/management/commands/articles_from_blog.py b/article/management/commands/articles_from_blog.py new file mode 100644 index 00000000..7380727d --- /dev/null +++ b/article/management/commands/articles_from_blog.py @@ -0,0 +1,77 @@ +# -*- coding: utf-8 -*- +import MySQLdb +from MySQLdb.cursors import DictCursor +from django.core.management.base import BaseCommand, CommandError +from functions.translate import fill_with_signal +from article.models import Article +from accounts.models import User +from theme.models import Theme,Tag + + +class Command(BaseCommand): + def handle(self, *args, **options): + db = MySQLdb.connect(host="localhost", + user="kotzilla", + passwd="qazedc", + db="expomap_old_blog", + charset='utf8', + cursorclass=DictCursor) + + cursor = db.cursor() + + sql = """SELECT post.post_date as publish_date, + post.post_content as description, + post.post_modified as modified, + post.post_title as main_title, + post.post_name as slug, + tag.name as tag_name + FROM wp_posts post + INNER JOIN wp_term_relationships rel ON rel.object_id = post.ID + INNER JOIN wp_terms tag ON tag.term_id=rel.term_taxonomy_id + WHERE post_status = "publish";""" + + cursor.execute(sql) + result = cursor.fetchall() + user = User.objects.get(id=1) + + tags = {x.name: x.id for x in Tag.objects.language('ru')} + + clear_result = {} + + errors = [] + for a in result: + if a.get('tag_name') in tags.keys(): + tag_id = tags[a['tag_name']] + a['tag_id'] = [tag_id] + print a + else: + tag_id = None + + if not clear_result.get(a['main_title']): + clear_result[a['main_title']] = a + else: + if clear_result[a['main_title']].get('paid_id') and tag_id: + clear_result[a['main_title']]['tag_id'].append(tag_id) + + + for a in clear_result.values(): + article = Article(type=1, + created = a['publish_date'], + publish_date= a['publish_date'], + modified = a['modified'], + ) + article.author = user + article.translate('ru') + article.main_title = a['main_title'] + article.description = a['description'] + article.slug = a['slug'] + if a.get('tag_id'): + article.tag = [Tag.objects.language('ru').get(id=id) for id in a['tag_id']] + try: + article.save() + except Exception as e: + errors.append(e) + + print errors + + diff --git a/article/models.py b/article/models.py index 5a0da916..aa252baf 100644 --- a/article/models.py +++ b/article/models.py @@ -123,7 +123,7 @@ class Article(TranslatableModel): files = generic.GenericRelation('file.FileModel', content_type_field='content_type', object_id_field='object_id') class Meta: - ordering = ['-created'] + ordering = ['-publish_date'] def __unicode__(self): return self.lazy_translation_getter('main_title', self.pk) diff --git a/city/management/commands/update_hotels_currency.py b/city/management/commands/update_hotels_currency.py new file mode 100644 index 00000000..f28365d4 --- /dev/null +++ b/city/management/commands/update_hotels_currency.py @@ -0,0 +1,51 @@ +import urllib2, base64 +import json +from django.core.management.base import BaseCommand, CommandError +from city.models import Hotel + +HOTEL_URL = 'https://distribution-xml.booking.com/json/bookings.getHotels?' +username = 'expomap' +password = '33xp00m33p' + + +def handle_hotels(hotel_ids): + good_hotels = [] + hotel_ids_str = ','.join(hotel_ids) + new_url = HOTEL_URL + 'hotel_ids=%s'%hotel_ids_str + request = urllib2.Request(new_url) + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + try: + response = urllib2.urlopen(request) + code = response.getcode() + except urllib2.HTTPError, e: + code = e.code + except urllib2.URLError, e: + code = e.code + + json_hotels = response.read() + hotels = json.loads(json_hotels) + for item in hotels: + id = item['hotel_id'] + currency = item['currencycode'] + Hotel.objects.filter(id=id).update(currency=currency) + good_hotels.append(id) + bad_hotels = list(set(hotel_ids) - set(good_hotels)) + + Hotel.objects.filter(id__in=bad_hotels).delete() + print('success update currency in %d hotels'%len(good_hotels)) + + +def main(): + hotels_todo = Hotel.objects.filter(currency__isnull=True)[:100].count() + while hotels_todo > 0: + hotel_ids = [str(item.id) for item in list(Hotel.objects.filter(currency__isnull=True)[:100])] + handle_hotels(hotel_ids) + hotels_todo =Hotel.objects.filter(currency__isnull=True)[:100].count() + + + + +class Command(BaseCommand): + def handle(self, *args, **options): + main() diff --git a/city/management/commands/update_hotels_price.py b/city/management/commands/update_hotels_price.py new file mode 100644 index 00000000..38243658 --- /dev/null +++ b/city/management/commands/update_hotels_price.py @@ -0,0 +1,68 @@ +import urllib2, base64 +import json +from django.core.management.base import BaseCommand, CommandError +from city.models import Hotel + +URL = 'https://distribution-xml.booking.com/json/bookings.getRooms?' +username = 'expomap' +password = '33xp00m33p' +test_id = '298239' + +def get_prices(rooms): + min_price = None + max_price = None + for room in rooms: + try: + cur_min = float(room.get('min_price')) + except TypeError: + cur_min = None + try: + cur_max = float(room.get('max_price')) + except TypeError: + cur_max = None + + if cur_min > min_price: + min_price = cur_min + if cur_max > max_price: + max_price = cur_max + + return min_price, max_price + +def handle_hotels(hotel_id): + hotel_ids_str = hotel_id + new_url = URL + 'hotel_ids=%s'%hotel_ids_str + request = urllib2.Request(new_url) + base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '') + request.add_header("Authorization", "Basic %s" % base64string) + try: + response = urllib2.urlopen(request) + code = response.getcode() + except urllib2.HTTPError, e: + code = e.code + except urllib2.URLError, e: + code = e.code + + json_rooms = response.read() + rooms = json.loads(json_rooms) + min_price, max_price = get_prices(rooms) + Hotel.objects.filter(id=hotel_id).update(min_price=min_price, max_price=max_price) + print('success') + +def main(): + hotel_id = test_id + for hotel in Hotel.objects.all(): + try: + handle_hotels(str(hotel.id)) + except: + continue + + #hotels_todo = Hotel.objects.filter(currency__isnull=True)[:100].count() + #while hotels_todo > 0: + #hotel_ids = [str(item.id) for item in list(Hotel.objects.filter(currency__isnull=True)[:100])] + #handle_hotels(hotel_ids) + #hotels_todo = Hotel.objects.filter(currency__isnull=True)[:100].count() + + +class Command(BaseCommand): + def handle(self, *args, **options): + main() \ No newline at end of file diff --git a/city/models.py b/city/models.py index aa4f4ead..8ec29ea9 100644 --- a/city/models.py +++ b/city/models.py @@ -136,6 +136,10 @@ class Hotel(TranslatableModel): latitude = models.FloatField(blank=True, null=True) longitude = models.FloatField(blank=True, null=True) photo = ImageField(upload_to='hotels') + currency = models.CharField(max_length=10) + min_price = models.FloatField(blank=True, null=True) + max_price = models.FloatField(blank=True, null=True) + checked = models.NullBooleanField(blank=True, null=True) translations = TranslatedFields( name = models.CharField(max_length=255, blank=True), diff --git a/core/views.py b/core/views.py index 8da905d5..d762dc01 100644 --- a/core/views.py +++ b/core/views.py @@ -260,8 +260,7 @@ from django.utils.translation import get_language from .utils import queryset_to_workbook from exposition.models import Exposition from conference.models import Conference -from django.core.urlresolvers import reverse - +from django.conf import settings def download_workbook(request): lang = get_language() @@ -269,9 +268,9 @@ def download_workbook(request): if data: qs = [] for i,obj in enumerate(data): - if data.get('data[%i][name]'%i) == 'expo': + if data.get('data[%i][name]' % i) == 'expo': qs.append(Exposition.objects.language(lang).get(id=data['data[%i][value]'%i])) - elif data.get('data[%i][name]'%i) == 'conf': + elif data.get('data[%i][name]' % i) == 'conf': qs.append(Conference.objects.language(lang).get(id=data['data[%i][value]'%i])) earliest_event = qs[0].data_begin @@ -279,7 +278,12 @@ def download_workbook(request): if obj.data_begin < earliest_event: earliest_event = obj.data_begin setattr(obj, 'number', i) - setattr(obj, 'dates', u'%s - %s'%(obj.data_begin.strftime('%d %B %Y'),obj.data_end.strftime('%d %B %Y'))) + setattr(obj, 'dates', u'%s %s %s - %s %s %s'%(obj.data_begin.strftime('%d'), + settings.MONTHES[obj.data_begin.strftime("%b").lower()]['name'], + obj.data_begin.strftime('%Y'), obj.data_end.strftime('%d'), + settings.MONTHES[obj.data_end.strftime("%b").lower()]['name'], + obj.data_end.strftime('%Y')) + ) setattr(obj, 'full_place', u'%s, %s, %s' % (obj.country, obj.city, getattr(obj.place, 'name', ''))) try: setattr(obj, 'link', u'http://www.expomap.ru%s'%obj.get_absolute_url()) diff --git a/templates/client/article/article.html b/templates/client/article/article.html index 71f5d860..e36596ee 100644 --- a/templates/client/article/article.html +++ b/templates/client/article/article.html @@ -19,7 +19,7 @@ {% include 'client/includes/article/article_logo.html' with obj=object %}