Commits

ronald martinez committed 388d8a2

a

  • Participants
  • Parent commits 6b4d5d3

Comments (0)

Files changed (10)

celerybeat-schedule

Binary file removed.

controllers/__init__.py

 from models import User
 from tornado.web import RequestHandler
 
+from libs.pagination import Paginator
+
 
 class BaseHandler(RequestHandler):
 
             .get_template(template).render(**kwargs)
 
     def render(self, template, **kwargs):
-        self.set_header('cache-control', 'no-cache')
-        kwargs['user'] = self.get_current_user()
         self.finish(self.render_string(template, **kwargs))
+
+
+class ListMixin(object):
+
+    @property
+    def current_page(self):
+        current_page = self.get_argument('page', '1')
+        return int(current_page) if current_page.isdigit() else 1
+
+    def get_pagination(self, query, count, per_page=10):
+
+        try:
+            per_page = int(per_page)
+        except ValueError:
+            per_page = 10
+
+        page = self.current_page
+        paginator = Paginator(page=self.current_page, total_items=count,
+                              per_page=per_page)
+        per_page = paginator.per_page
+
+        return {
+            'items': query[(page - 1) * per_page: page * per_page],
+            'pages': paginator.pages,
+            'total_items': count,
+            'current_page': page,
+            'total_pages': paginator.total_pages,
+        }

controllers/product.py

 from models import Product, Category, Match, Conversation, Comment, User
 from controllers import BaseHandler
 from tornado.web import authenticated
+
 from tasks import generate_thumbs
 
 
                     status_code = 3
                     logging.error('no upload image')
                 else:
+                    #if not product.send_message_to_generate_thumbs():
+                        #logging.error('no send queue to generate_thumbs')
+                        #status_code = 4
 
-                    from boto.sqs.connection import SQSConnection
-                    from boto.sqs.message import Message
-                    import settings
-
-                    conn = SQSConnection(settings.AWS_KEY, settings.AWS_SECRET)
-                    q = conn.create_queue('generate_thumbs')
-                    m = Message()
-                    m.set_body(product.id)
-
-                    
-                    try:
-                        status = q.write(m)
-                    except Exception as exc:
-                        logging.error(exc)
-                        status_code = 4
-                    else:
-                        if not status:
-                            status_code = 4
-
-                    #generate_thumbs.delay(product.id)
+                    generate_thumbs.delay(product.id)
                     self.redirect(self.reverse_url('product_edit', product.id))
                     return
 
         logging.info('small_image: %s' % product.small_url)
         logging.info('large_image: %s' % product.large_url)
         logging.info('medium_image: %s' % product.medium_url)
+        logging.info('is_upload_thumbs: %s' % product.is_upload_thumbs)
 
         kwargs['product'] = product
         kwargs['categories'] = categories
                     if not product.upload_img(_raw_image.get('body')):
                         status_code = 4
                     else:
+                        #if not product.send_message_to_generate_thumbs():
+                        #if not product.generate_thumbs():
+                            #logging.error('no generate slug')
+                            #status_code = 4
                         generate_thumbs.delay(product.id)
 
                     if status_code == 0:
-#from boto.s3.connection import S3Connection
+import sys
 import settings
 
-"""
-cn = S3Connection(
-    settings.AWS_KEY, settings.AWS_SECRET
-)
-"""
-
-#cn.delete_domain('api')
-
-
-
 from simpledb import SimpleDB
 
 sdb = SimpleDB(settings.AWS_KEY, settings.AWS_SECRET)
 
-del sdb['product']
-
+if len(sys.argv) > 1:
+    del sdb[sys.argv[1]]
 
 from StringIO import StringIO
 
+from boto.sqs.connection import SQSConnection
+from boto.sqs.message import Message
 
 AW_SDB = simpledb.SimpleDB(
     settings.AWS_KEY,
 )
 
 
+class SystemUser(models.Model):
+
+    __id = models.ItemName()
+    username = models.Field(required=True)
+    password = models.Field(required=True)
+    email = models.Field(required=True)
+    status = models.Field(default='enabled')
+    created_at = models.DateTimeField(default=datetime.now())
+    modified_at = models.DateTimeField()
+    last_login_at = models.DateTimeField()
+
+    class Meta:
+        connection = AW_SDB
+        domain = 'system_user'
+
+    @classmethod
+    def email_exists(self, email):
+        rs = self.objects.filter(
+            simpledb.item_name(eq=email)
+        )
+        return True if len(rs) > 0 else False
+
+    def set_password(self, value):
+        if isinstance(value, unicode):
+            value = value.encode('utf8')
+        self.password = hashlib.sha1(value).hexdigest()
+
+    @classmethod
+    def auth(self, email, password):
+        password = str(hashlib.sha224(password).hexdigest())
+        tot = len(self.objects.filter(
+            simpledb.where(email=email) & \
+            simpledb.where(password=password) & \
+            simpledb.where(status='active')
+        ))
+        return True if tot > 0 else False
+
+    def save(self, **kwargs):
+        if self.password:
+            self.password = hashlib.sha224(self.password).hexdigest()
+        super(User, self).save(**kwargs)
+
+
 class User(models.Model):
     email = models.ItemName()
     name = models.Field(required=True)
     #image = models.Field()
     location = models.Field()
     audience = models.Field()
+    is_upload_thumbs = models.BooleanField(default=False)
 
     class Meta:
         connection = AW_SDB
         else:
             return url
 
+    def send_message_to_generate_thumbs(self):
+        conn = SQSConnection(settings.AWS_KEY, settings.AWS_SECRET)
+        q = conn.create_queue('generate_thumbs')
+        m = Message()
+        m.set_body(self.id)
+
+        try:
+            status = q.write(m)
+        except Exception as exc:
+            logging.error(exc)
+            return None
+        else:
+            if not status:
+                return None
+            else:
+                return True
+
 
 class Conversation(models.Model):
     __id = models.ItemName()
         try:
             user = self.objects.filter(token=token)[0]
         except Exception as exc:
+            logging.error(exc)
             user = None
         return user
 
         send_email.retry(exc=exc)
 
 
-"""
 @task(name='generate_thumbs', max_retries=0, ignore_result=True)
 def generate_thumbs(product_id):
 
 
             try:
                 for width, height, suffix in  Product.THUMB_SIZES_IMAGES:
-                    print width, height
                     output_image = StringIO()
                     thumb = thumbnail(image, width, height, force=True,
                             crop=False, adjust_to_width=True)
             except Exception as exc:
                 logging.error(exc)
                 generate_thumbs.retry(exc=exc)
-"""
 
 
-@task(name='generate_thumbs3', max_retries=0)
-def generate_thumbs():
-
-    logger = generate_thumbs.logger()
-    logger.info('OK')
-
-
-    #log = generate_thumbs.logger()
-    #print log.info('ok')
-    print "x"
+@task(name='generate_thumbs_queue', max_retries=0)
+def generate_thumbs_queue():
     from boto.sqs.connection import SQSConnection
-
     conn = SQSConnection(settings.AWS_KEY, settings.AWS_SECRET)
     q = conn.create_queue('generate_thumbs')
     messages = q.get_messages()
     #visibility_timeout=60
 
-    print len(messages)
-
     if len(messages) > 0:
-
         for m in messages:
-            print m
-            
-            print m.get_body()
 
             try:
                 product = Product.objects.get(m.get_body())
 
                 bucket = cn.get_bucket(settings.S3_BUCKET_NAME)
                 key = Key(bucket)
-                key.key = '%s/%s' % (settings.S3_UPLOAD_PATH_PRODUCT, product.id)
+                key.key = '%s/%s' % (
+                    settings.S3_UPLOAD_PATH_PRODUCT, product.id)
 
                 try:
                     image = Image_.open(StringIO(key.get_contents_as_string()))
                 else:
 
                     try:
-                        for width, height, suffix in  Product.THUMB_SIZES_IMAGES:
-                            print width, height
+                        for width, height, suffix in \
+                            Product.THUMB_SIZES_IMAGES:
                             output_image = StringIO()
                             thumb = thumbnail(image, width, height, force=True,
                                     crop=False, adjust_to_width=True)
                             thumb.save(output_image, 'jpeg')
 
                             key.key = '%s/%s_%s' % (
-                                settings.S3_UPLOAD_PATH_PRODUCT, product.id, suffix)
+                                settings.S3_UPLOAD_PATH_PRODUCT,
+                                product.id, suffix)
 
                             key.set_contents_from_string(
                                 output_image.getvalue(),
 
 if __name__ == '__main__':
     #generate_thumbs('7d20cf73-1e96-429e-bbc7-5820af5d4ede')
-
-    generate_thumbs()
+    #generate_thumbs()
+    logging.info('main')

templates/product.html

 {% extends "layout.html" %}
 {% set product = product|default %}
 {% set action = action|default %}
-{% set tot=0 %}
 
 {% block content %}
 
 <form method="post" enctype="multipart/form-data">
     <h2> {% if action == 'edit' %} Edit Product {% else %} Add Product {% endif %}  </h2>
 
-    <table width="80%">
+    <table width="50%">
 
         {% if action == 'edit' %}
         <tr>
             <td>Categories</td>
             <td>
                 {% for x in categories %}
-                {% set tot = tot + 1 %}
-
-                <input type="checkbox" name="categories" value="{{ x.slug }}" {% if x.checked == True %}checked{% endif %} ><font size="1">{{ x.title }}</font>
-
-                {% if tot == 7 %}<br />{% endif%}
-                {% if tot == 14 %}<br />{% endif%}
-
+                <input type="checkbox" name="categories" value="{{ x.slug }}" {% if x.checked == True %}checked{% endif %} >{{ x.title }}<br />
                 {% endfor %}
             <td/>
         </tr>
         <tr>
-            <td><font size="2">Title</font></td>
-            <td><input size="40" type='text' name='title' value='{{ product.title|default('') }}' /><td/>
+            <td>Title</td>
+            <td><input size="47" type='text' name='title' value='{{ product.title|default('') }}' /><td/>
         </tr>
         <tr>
-            <td><font size="2">Description</font></td>
+            <td>Description</td>
             <td><textarea cols="35" rows="5" name='description'>{{ product.description }}</textarea><td/>
         </tr>
         <tr>
-            <td><font size="2">How much?</font></td>
+            <td>How much?</td>
             <td><input size="4" type='text' name='cost' value='{{ product.cost|default(0) }}' /><td/>
         </tr>
         <tr>
-            <td><font size="2">How far travel?</font></td>
+            <td>How far will you travel?</td>
             <td><input size="4" type='text' name='will_travel' value='{{ product.will_travel|default(1000) }}' /><td/>
         </tr>
         <tr>
-            <td><font size="2">Allow retailers?</font></td>
-            <td><input type="checkbox" value="1" name="allow_retailers" {% if product.allow_retailers == True %} checked {% else %} {% endif %} /></td>
+            <td>Allow retailers to see this post?</td>
+            <td><input type="checkbox" value="1" name="allow_retailers" {% if product.allow_retailers == '1' %} checked {% else %} {% endif %} /></td>
         </tr>
         <tr>
-            <td><font size="2">Allow charities?</font></td>
-            <td><input type="checkbox" value="1" name="allow_charities" {% if product.allow_charities == True %} checked {% else %} {% endif %} /></td>
+            <td>Allow charities to see this post?</td>
+            <td><input type="checkbox" value="1" name="allow_charities" {% if product.allow_charities == '1' %} checked {% else %} {% endif %} /></td>
         </tr>
         <tr>
-            <td><font size="2">Donate now?</font></td>
-            <td><input type="checkbox" value="1" name="donate_now" {% if product.donate_now == True %} checked {% else %} {% endif %} /><td/>
+            <td>Donate this item now?</td>
+            <td><input type="checkbox" value="1" name="donate_now" {% if product.donate_now == '1' %} checked {% else %} {% endif %} /><td/>
         </tr>
         <tr>
-            <td><font size="2">Published date</font></td>
-            <td><input type='text' size="10" name='published_at' value='{{ product.published_at|default('') }}'/><td/>
+            <td>Published date</td>
+            <td><input type='text' size="8" name='published_at' value='{{ product.published_at|default('2012-05-28') }}'/><td/>
         </tr>
         <tr>
-            <td><font size="2">Expires date</font></td>
-            <td><input type='text' size="10" name='expires_at' value='{{ product.expires_at|default('') }}'/><td/>
+            <td>Expires date</td>
+            <td><input type='text' size="8" name='expires_at' value='{{ product.expires_at|default('2012-05-28') }}'/><td/>
         </tr>
         <tr>
-            <td><font size="2">Location</font></td>
+            <td>Location</td>
             <td><input size="47" type='text' name='location' value='{{ product.location|default('') }}' /><td/>
         </tr>
         <tr>
-            <td><font size="2">Audience</font></td>
+            <td>Audience</td>
             <td>
                 <select name="audience">
                 {% for x in audiences %}
             </td>
         </tr>
         <tr>
-            <td><font size="2">Type</td>
+            <td>Type</td>
             <td>
                 <input type="radio" name="type" value="have" checked>have
                 <input type="radio" name="type" value="want">want
         {% endif %}
 
         <tr>
-            <td><font size="2">Image</td>
+            <td>Image</td>
             <td><input type='file' name='filename'/><td/>
         </tr>
         <tr style="height:15px;">
         </tr>
         <tr>
             <td></td>
-            <td><input type='submit' value="save product"><td/>
+            <td><input type='submit' value="save"><td/>
         </tr>
 
 

templates/site/product.htm5

-<!DOCTYPE html><!--[if lt IE 7]>
-<html lang="en" class="no-js lt-ie9 lt-ie8 lt-ie7"></html><![endif]--><!--[if IE 7]>
-<html lang="en" class="no-js lt-ie9 lt-ie8"></html><![endif]--><!--[if IE 8]>
-<html lang="en" class="no-js lt-ie9"></html><![endif]-->
-<!--[if gt IE 8]><!-->
-<html lang="en" class="no-js">
-  <!--<![endif]-->
-  <head>
-    <title>VUULO</title>
-    <meta charset="utf-8">
-    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
-    <meta name="description" content="">
-    <meta name="viewport" content="width=device-width">
-    <link type="text/css" rel="stylesheet" href="{{ handler.static_url('css/site.css') }}">
-  </head>
-  <body> 
-    <div id="wrap">
-      <div id="header">
-        <div id="logo"><img src="{{ handler.static_url('images/site/vuulo-logo.png') }}" class="logo-vuulo"></div>
-        <div id="user_tools"><a id="btn_iphoneapp" href="" class="tool">Get the IPhone app!</a>{% if user %}<a id="btn_logout" href="{{ handler.reverse_url('user_logout') }}" class="tool"></a>{% else %}<a id="btn_login" href="{{ handler.reverse_url('user_login') }}" class="tool"></a>{% endif %}</div>
-        <div id="search">
-          <input id="search_input" type="text" value="" placeholder="items, categories, groups">
-          <button id="search_submit" type="submit">Search</button>
-        </div>{% if user %}<a id="tutorial" href="">Check out our video tutorial</a><a id="tutorial_points" href="">you have 124 points</a>{% endif %}
-      </div>
-      <div id="content-product">
-        <div id="main-product">
-          <div id="user-thumb">
-            <div class="user-photo"><img src="../static/images/site/product-user.png" class="img-profile"></div>
-            <div class="user-name">{{user.name}}</div>
-            <div class="clear"></div>
-          </div>
-          <div id="product">
-            <div class="tit-product"> <span class="span">I {% if product.type == 'want' %}WANT</span>{% else %}HAVE{% endif %}</span> {{ product.title }}</div><img src="{{ product.image }}" width="4000" id="img-product">
-            <div id="buttons-social"><a href="" class="btn-ihave">I HAVE this!</a>
-              <div class="button"><a name="fb_share"></a>
-                <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
-              </div>
-              <div class="button"><a href="http://pinterest.com/USERNAME/" target="_blank"><img src="http://passets-ec.pinterest.com/images/about/buttons/pinterest-button.png" width="65" height="20" alt="Follow Me on Pinterest"></a></div>
-              <div class="button"><a href="https://twitter.com/share" data-count="none" class="twitter-share-button">Share</a></div>
-              <div class="button">
-                <g:plusone size="medium"> </g:plusone>
-              </div>
-              <div class="clear"></div>
-            </div>
-            <div class="clear"></div>
-    
-            {% if user %}
-                {% if is_owner %}
-                    {% if have_comments %}
-                        <div style="display: block" class="single">
-                            <div class="user-conversacion">
-                                {% for item in comments %}
-                                <div class="box-user">
-                                    <div class="box-user-data">
-                                        <img src="../static/images/site/product-user.png" class="box-user-img">
-                                        <div class="box-user-name">{{item.user}} Said:</div>
-                                    </div>
-                                    <div class="box-txt">{{ item.text }}</div>
-                                    <div class="clear"> </div>
-                                </div>
-                                {% endfor %}
-
-                            <form action="" method="post" class="box-comment">{{ xsrf }}
-                                <input type="hidden" name="product_id" value="{{ product.id }}">
-                                <input type="hidden" name="to_user" value="{{ comments[0].user.email }}">
-                                <textarea placeholder="Send a message to this user." name="message" class="textarea"></textarea>
-                                <input type="submit" value="SEND" class="submit">
-                            </form>
-                            </div>
-                        </div>
-                    {% else %}
-                        <div style="display: block" class="">
-                            <font color="red">You have {{ tot_unread_comments }} new messages</font>
-                                <table border="1" style="width:180px">
-                                {% for item in conversations %}
-                                <tr><td>&nbsp;<a href="{{ handler.reverse_url("product", product.slug) }}/{{ item.id }}/comments">{{item.user}}
-                                <font color="red">{{item.unread_comments}}</font></a></td>
-                                </tr>
-                                {% endfor %}
-                                </table>
-                            </div>
-                            <!--
-                            <div style="display:none" class="multiple">
-                            {% for item in [1,2,3] %}
-                                <div class="conversation">
-                                    <div class="user"><img src="../static/images/site/product-user.png" class="box-user-img">
-                                        <div class="box-user-name">{{user.name}}</div>
-                                        <div class="counter">4</div>
-                                    </div>
-                                </div>
-                            {% endfor %}
-                            </div>
-                            -->
-                    {% endif %}
-                {% else %}
-                    <div style="display: block" class="single">
-                        <div class="user-conversacion">
-                            {% for item in comments %}
-                            <div class="box-user">
-                                <div class="box-user-data">
-                                    <img src="../static/images/site/product-user.png" class="box-user-img">
-                                    <div class="box-user-name">{{item.user}} Said:</div>
-                                </div>
-                                <div class="box-txt">{{ item.text }}</div>
-                                <div class="clear"> </div>
-                            </div>
-                            {% endfor %}
-
-                        <form action="" method="post" class="box-comment">{{ xsrf }}
-                            <input type="hidden" name="product_id" value="{{ product.id }}">
-                            <textarea placeholder="Send a message to this user." name="message" class="textarea"></textarea>
-                            <input type="submit" value="SEND" class="submit">
-                        </form>
-                        </div>
-                    </div>
-                {% endif %}
-            {% endif %}
-            <div class="clear"></div>
-          </div>
-        </div>
-
-        <div id="sidebar-product">
-          <div class="tit-product">Product Details</div>
-          <div class="side-txt">{{ product.description }}</div>
-          <div class="side-txt"><strong>Categories:</strong></div>
-          <div class="side-categories">        
-            <ul>
-               
-              {% for c in product.data_categories %}
-              <li><a href="" class="category-link">{{ c.title}}</a></li>{% else %}
-              <li>Category no found</li>{% endfor %}
-            </ul>
-            <div class="clear"> </div>
-          </div>
-          <div class="side-txt"><strong>Price: </strong>{{product.cost}}</div>
-          <div class="side-txt">                                                                                                                                        <strong>Location: </strong>{{product.location}}</div>
-          <div class="side-txt"><strong>Audience: </strong>{{product.audience}} </div>
-          <div class="side-txt"><strong>Status: </strong>expires in {{ product.expire_days }} days
-            <div class="clear"></div>
-          </div>
-          <div id="user-want">{% if product.matches_want %}
-            <div class="users-want-tit">Users WANT the same</div>
-            <div class="users-want-thumbs">{% for item in product.matches_want %}
-              <div class="user-thumb"><a href="{{ handler.reverse_url("product", item.slug)  }}"><img src="{{ item.image }}" width="60" height="60"></a></div>{% endfor %}
-              <div class="clear"></div>
-            </div>{% endif %}
-            {% if product.matches_have %}
-            <div class="users-want-tit">Users HAVE the same</div>
-            <div class="users-want-thumbs">{% for item in product.matches_have %}
-              <div class="user-thumb"><a href="{{ handler.reverse_url("product", item.slug)  }}"><img src="{{ item.image }}" width="60" height="60"></a></div>{% endfor %}
-              <div class="clear"></div>
-            </div>{% endif %}
-          </div>
-        </div>
-        <div class="clear">         </div>
-      </div>
-    </div>
-    <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
-    <script>
-      (function() {
-        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js';      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
-      })();  
-      
-    </script>
-  </body>
-</html>

templates/site/product.html

           <div class="side-txt"><strong>Price: </strong>{{product.cost}}</div>
           <div class="side-txt">                                                                                                                                        <strong>Location: </strong>{{product.location}}</div>
           <div class="side-txt"><strong>Audience: </strong>{{product.audience}} </div>
-          <div class="side-txt"><strong>Status: </strong>Expires in {{product.expire_days}} days.
+          <div class="side-txt"><strong>Status: </strong>{{product.expires}}
             <div class="clear"></div>
           </div>
           <div id="user-want">{% if product.matches_want %}
 from tornado.web import url
 from controllers import user, home, product, api
 
+import controllers.admin.product
+import controllers.admin.auth
+
 
 handlers = (
+    url(
+        r'/admin/?',
+        controllers.admin.auth.Login,
+        name="admin_"
+    ),
+    url(
+        r'/admin/login',
+        controllers.admin.auth.Login,
+        name="admin_login"
+    ),
+    url(
+        r'/admin/logout',
+        controllers.admin.auth.Logout,
+        name="admin_logout"
+    ),
+    url(
+        r'/admin/product',
+        controllers.admin.product.List,
+        name='admin_product'
+    ),
+    url(
+        r'/admin/product/delete',
+        controllers.admin.product.Delete,
+        name='admin_product_delete'
+    ),
+    url(
+        r'/admin/product/edit/(.*)',
+        controllers.admin.product.Edit,
+        name='admin_product_edit'
+    ),
+    url(
+        r'/admin/product/delete-image',
+        controllers.admin.product.DeleteImage,
+        name='admin_product_delete_image'
+    ),
+
     url('/api/login', api.Login),
     url('/api/add_product', api.AddProduct),
     url('/api/edit_product/(.*)', api.EditProduct),