def import_generic(self, filename): import csv f = open(filename, 'r') r = csv.reader(f, delimiter=";") for line in r: if len(line) < 9: print('malformed:', repr(line)) continue try: u = User.objects.get(first_name=smart_unicode(line[0]), last_name=smart_unicode(line[1])) except User.DoesNotExist: print('user not found:', repr(line)) continue sum, date, method = (line[5], line[7], line[8]) try: p = Payment.objects.filter(date=date, amount=sum, user=u) if len(p) != 0: print('payment already present:', repr(line)) continue Payment.objects.create(date=date, user=u, amount=sum, method=PaymentMethod.objects.get(name=method), original_file=filename, original_line=str(line)) print('created:', repr(line)) except ValueError: print('error creating payment:', repr(line))
def getcurrentsite(http_post, path_info, query_string): """ Returns the site id and the page cache key based on the request. """ url = u'http://%s/%s' % (smart_unicode(http_post.rstrip('/')), \ smart_unicode(path_info.lstrip('/'))) pagecachekey = '%s?%s' % (smart_unicode(path_info), \ smart_unicode(query_string)) hostdict = hostcache_get() if not hostdict: hostdict = {} if url not in hostdict: default, ret = None, None for site in Site.objects.all(): if url.startswith(site.url): ret = site break if not default or site.default_site: default = site if not ret: if default: ret = default else: # Somebody is requesting something, but the user didn't create # a site yet. Creating a default one... ret = Site(name='Default Feedjack Site/Planet', \ url='www.feedjack.org', \ title='Feedjack Site Title', \ description='Feedjack Site Description. ' \ 'Please change this in the admin interface.') ret.save() hostdict[url] = ret.id hostcache_set(hostdict) return hostdict[url], pagecachekey
def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None): # required -- Boolean that specifies whether the field is required. # True by default. # widget -- A Widget class, or instance of a Widget class, that should # be used for this Field when displaying it. Each Field has a # default Widget that it'll use if you don't specify this. In # most cases, the default widget is TextInput. # label -- A verbose name for this field, for use in displaying this # field in a form. By default, Django will use a "pretty" # version of the form field name, if the Field is part of a # Form. # initial -- A value to use in this Field's initial display. This value # is *not* used as a fallback if data isn't given. # help_text -- An optional string to use as "help text" for this Field. if label is not None: label = smart_unicode(label) self.required, self.label, self.initial = required, label, initial self.help_text = smart_unicode(help_text or '') widget = widget or self.widget if isinstance(widget, type): widget = widget() # Hook into self.widget_attrs() for any Field-specific HTML attributes. extra_attrs = self.widget_attrs(widget) if extra_attrs: widget.attrs.update(extra_attrs) self.widget = widget # Increase the creation counter, and save our local copy. self.creation_counter = Field.creation_counter Field.creation_counter += 1
def __init__(self, request, title, description, url): self._title = u"%s" % smart_unicode(title) self._description = mark_safe(u"%s" % smart_unicode(description)) self._url = url if old_version: super(BaseNodeFeed, self).__init__('', request)
def end_object(self, obj): self.objects.append({ "model" : smart_unicode(obj._meta), "pk" : smart_unicode(obj._get_pk_val(), strings_only=True), "fields" : self._current }) self._current = None
def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None, error_messages=None, show_hidden_initial=False, validators=[], localize=False): # required -- Boolean that specifies whether the field is required. # True by default. # widget -- A Widget class, or instance of a Widget class, that should # be used for this Field when displaying it. Each Field has a # default Widget that it'll use if you don't specify this. In # most cases, the default widget is TextInput. # label -- A verbose name for this field, for use in displaying this # field in a form. By default, Django will use a "pretty" # version of the form field name, if the Field is part of a # Form. # initial -- A value to use in this Field's initial display. This value # is *not* used as a fallback if data isn't given. # help_text -- An optional string to use as "help text" for this Field. # error_messages -- An optional dictionary to override the default # messages that the field will raise. # show_hidden_initial -- Boolean that specifies if it is needed to render a # hidden widget with initial value after widget. # validators -- List of addtional validators to use # localize -- Boolean that specifies if the field should be localized. if label is not None: label = smart_unicode(label) self.required, self.label, self.initial = required, label, initial self.show_hidden_initial = show_hidden_initial if help_text is None: self.help_text = u'' else: self.help_text = smart_unicode(help_text) widget = widget or self.widget if isinstance(widget, type): widget = widget() # Trigger the localization machinery if needed. self.localize = localize if self.localize: widget.is_localized = True # Let the widget know whether it should display as required. widget.is_required = self.required # Hook into self.widget_attrs() for any Field-specific HTML attributes. extra_attrs = self.widget_attrs(widget) if extra_attrs: widget.attrs.update(extra_attrs) self.widget = widget # Increase the creation counter, and save our local copy. self.creation_counter = Field.creation_counter Field.creation_counter += 1 messages = {} for c in reversed(self.__class__.__mro__): messages.update(getattr(c, 'default_error_messages', {})) messages.update(error_messages or {}) self.error_messages = messages self.validators = self.default_validators + validators
def new_action(self, user, action_object, action_key, target_object=None): """ Create an action If the target_object is on the user's follow list Send the user a notification """ action_types = ActionType.objects.filter(name=action_key) if not action_types.exists(): action_types.create(name=action_key) action = self.model( user=user, action_content_type=ContentType.objects.get_for_model( action_object.__class__), action_object_id=smart_unicode(action_object.id), action_type=action_types.get()) if target_object: action.target_content_type = ContentType.objects.get_for_model( target_object.__class__) action.target_object_id = smart_unicode(target_object.id) action.save() return action
def _attachListFields(self, item): """Attaches property instances for list fields to given data entry. This is used in Admin class view methods. """ # TODO: implement this in a cleaner way item._list_properties_copy = copy.deepcopy(self._list_properties[:]) for prop in item._list_properties_copy: try: prop.value = prop.getter(item) if prop.typeName == "BlobProperty": prop.meta = utils.get_blob_properties(item, prop.name) if prop.value: prop.value = True # release the memory if prop.typeName == "ManyToManyProperty": # Show pretty list of referenced items. # Show 'None' in place of missing items new_value_list = [] for key in prop.value: new_value_list.append(smart_unicode(db.get(key))) prop.value = ", ".join(new_value_list) except datastore_errors.Error, exc: # Error is raised if referenced property is deleted # Catch the exception and set value to none logging.warning("Error catched in ModelAdmin._attachListFields: %s" % exc) prop.value = None # convert the value to unicode for displaying in list view if hasattr(prop.value, "__call__"): # support for methods prop.value = prop.value() prop.value = smart_unicode(prop.value)
def process_response(self, request, response): __traceback_hide__ = True ident = thread.get_ident() toolbar = self.__class__.debug_toolbars.get(ident) if not toolbar or request.is_ajax(): return response if isinstance(response, HttpResponseRedirect): if not toolbar.config["INTERCEPT_REDIRECTS"] or request.is_ajax(): return response redirect_to = response.get("Location", None) if redirect_to: cookies = response.cookies response = render_to_response("debug_toolbar/redirect.html", {"redirect_to": redirect_to}) response.cookies = cookies if ( "gzip" not in response.get("Content-Encoding", "") and response.get("Content-Type", "").split(";")[0] in _HTML_TYPES ): for panel in toolbar.panels: panel.process_response(request, response) response.content = replace_insensitive( smart_unicode(response.content), self.tag, smart_unicode(toolbar.render_toolbar() + self.tag) ) if response.get("Content-Length", None): response["Content-Length"] = len(response.content) del self.__class__.debug_toolbars[ident] return response
def process_response(self, request, response): __traceback_hide__ = True ident = thread.get_ident() toolbar = self.__class__.debug_toolbars.get(ident) if not toolbar: return response if isinstance(response, HttpResponseRedirect): if not toolbar.config['INTERCEPT_REDIRECTS']: return response redirect_to = response.get('Location', None) if redirect_to: cookies = response.cookies response = render_to_response( 'debug_toolbar/redirect.html', {'redirect_to': redirect_to} ) response.cookies = cookies if 'gzip' not in response.get('Content-Encoding', '') and \ response.get('Content-Type', '').split(';')[0] in _HTML_TYPES: for panel in toolbar.panels: panel.process_response(request, response) response.content = replace_insensitive( smart_unicode(response.content), self.tag, smart_unicode(toolbar.render_toolbar() + self.tag)) if response.get('Content-Length', None): response['Content-Length'] = len(response.content) del self.__class__.debug_toolbars[ident] return response
def get_traceback_html(self): "Return HTML code for traceback." if issubclass(self.exc_type, TemplateDoesNotExist): self.template_does_not_exist = True if self._template: self.get_template_exception_info() frames = self.get_traceback_frames() unicode_hint = '' if issubclass(self.exc_type, UnicodeError): start = getattr(self.exc_value, 'start', None) end = getattr(self.exc_value, 'end', None) if start is not None and end is not None: unicode_str = self.exc_value.args[1] unicode_hint = smart_unicode(unicode_str[max(start-5, 0):min(end+5, len(unicode_str))], 'ascii', errors='replace') t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 template') c = Context({ 'exception_type': self.exc_type.__name__, 'exception_value': smart_unicode(self.exc_value, errors='replace'), 'unicode_hint': unicode_hint, 'frames': frames, 'lastframe': frames[-1], 'request': self.request, 'template_info': self.template_info, 'template_does_not_exist': self.template_does_not_exist, }) return t.render(c)
def post(request) : #get current user context = checkSession(request) user = getCurrentUser(context) #get question data questionTitle = smart_unicode(request.POST["title"], encoding='utf-8', strings_only=False, errors='strict') questionDescription = smart_unicode(request.POST["description"], encoding='utf-8', strings_only=False, errors='strict') if not user: message = 'you must be logged in first!' elif questionTitle == "": message = 'a question needs words!' else: #create question q = Question(asker=user.login, title=questionTitle, description=questionDescription) if request.POST.__contains__("tags") : tags=smart_unicode(request.POST["tags"], encoding='utf-8', strings_only=False, errors='strict') topics=tags.split(',') q.topics=topics print q try : q.create() q=q.findById() user=user.findByLogin() user.followedQuestions.append(q.id); user.update() message = 'question successfully posted' except Exception as e: message = e #remove the displayed question response = HttpResponse(); response["message"] = message; response["questionId"] = q.id; return response;
def _handle_photo(flickr, photo_id, secret, license, timestamp): info = flickr.photos.getInfo(photo_id=photo_id, secret=secret)["photo"] server_id = utils.safeint(info["server"]) farm_id = utils.safeint(info["farm"]) taken_by = smart_unicode(info["owner"]["username"]) title = smart_unicode(info["title"]["_content"]) description = smart_unicode(info["description"]["_content"]) date_uploaded = datetime.datetime.fromtimestamp(utils.safeint(info["dates"]["posted"])) date_updated = datetime.datetime.fromtimestamp(utils.safeint(info["dates"]["lastupdate"])) log.debug("Handling photo: %r (taken %s)" % (title, timestamp)) try: photo = Photo.objects.get(photo_id=photo_id) except Photo.DoesNotExist: photo = Photo(photo_id=photo_id) # at this point, we have a photo photo.farm_id = farm_id photo.server_id = server_id photo.secret = secret photo.title = title photo.description = description photo.taken_by = taken_by photo.date_uploaded = date_uploaded photo.date_updated = date_updated # save once, whether creating or updating photo.save()
def _args_to_unicode(args, kwargs): key = "" if args: key += smart_unicode(args) if kwargs: key += smart_unicode(kwargs) return key
def process_response(self, request, response): if request not in self.debug_toolbars: return response if self.debug_toolbars[request].config['INTERCEPT_REDIRECTS']: if isinstance(response, HttpResponseRedirect): redirect_to = response.get('Location', None) if redirect_to: response = render_to_response( 'debug_toolbar/redirect.html', {'redirect_to': redirect_to} ) if response.status_code == 200: for panel in self.debug_toolbars[request].panels: panel.process_response(request, response) if response['Content-Type'].split(';')[0] in _HTML_TYPES: if self.tag in smart_unicode(response.content): response.content = replace_insensitive( smart_unicode(response.content), self.tag, smart_unicode(self.debug_toolbars[request].render_toolbar() + self.tag)) else: response.content = "%s%s" % (smart_unicode(response.content), smart_unicode(self.debug_toolbars[request].render_toolbar())) if response.get('Content-Length', None): response['Content-Length'] = len(response.content) del self.debug_toolbars[request] return response
def process_response(self, request, response): """ Runs during responding """ if not request.user.is_authenticated(): return response if not response.status_code == 200: return response if not response.get('content-type', '').startswith('text/html'): return response if self.test_disguise(request): # Render HTML code that helps to select disguise html = render_to_string('disguise/form.html', { 'form': DisguiseForm(), 'original_user' : getattr(request, 'original_user', None), 'disguise_user' : getattr(request, 'user'), }, RequestContext(request)) # Insert this code before </body> response.content = replace_insensitive( smart_unicode(response.content), # Subject TAGNAME, # Search smart_unicode(html + TAGNAME) # Replace ) return response
def handle_fk_field(self, obj, field): """ Called to handle a ForeignKey field. Recursively serializes relations specified in the 'relations' option. """ fname = field.name related = getattr(obj, fname) if related is not None: if fname in self.relations: # perform full serialization of FK serializer = Serializer() options = {} if isinstance(self.relations, dict): if isinstance(self.relations[fname], dict): options = self.relations[fname] self._fields[fname] = serializer.serialize([related], **options)[0] else: # emulate the original behaviour and serialize the pk value if self.use_natural_keys and hasattr(related, 'natural_key'): related = related.natural_key() else: if field.rel.field_name == related._meta.pk.name: # Related to remote object via primary key related = related._get_pk_val() else: # Related to remote object via other field related = smart_unicode(getattr(related, field.rel.field_name), strings_only=True) self._fields[fname] = related else: self._fields[fname] = smart_unicode(related, strings_only=True)
def display_for_field(value, field): from xadmin.defs import EMPTY_CHANGELIST_VALUE if field.flatchoices: if hasattr(value,'__iter__'): rets = [ dict(field.flatchoices).get(e, EMPTY_CHANGELIST_VALUE) for e in value] return ','.join(rets) else: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(tz_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field.rel, models.ManyToManyRel): return ', '.join([smart_unicode(obj) for obj in value.all()]) else: return smart_unicode(value)
def handle_fk_field(self, obj, field): """ Called to handle a ForeignKey (we need to treat them slightly differently from regular fields). """ self._start_relational_field(field) related = getattr(obj, field.name) if related is not None: if self.use_natural_keys and hasattr(related, 'natural_key'): # If related object has a natural key, use it related = related.natural_key() # Iterable natural keys are rolled out as subelements for key_value in related: self.xml.startElement("natural", {}) self.xml.characters(smart_unicode(key_value)) self.xml.endElement("natural") else: if field.rel.field_name == related._meta.pk.name: # Related to remote object via primary key related = related._get_pk_val() else: # Related to remote object via other field related = getattr(related, field.rel.field_name) self.xml.characters(smart_unicode(related)) else: self.xml.addQuickElement("None") self.xml.endElement("field")
def djmoney_contents(self): from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE field, obj, model_admin = self.field[ 'field'], self.form.instance, self.model_admin try: f, attr, value = lookup_field(field, obj, model_admin) except (AttributeError, ValueError, ObjectDoesNotExist): result_repr = EMPTY_CHANGELIST_VALUE else: if f is None: boolean = getattr(attr, "boolean", False) if boolean: result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) if getattr(attr, "allow_tags", False): result_repr = mark_safe(result_repr) else: if value is None: result_repr = EMPTY_CHANGELIST_VALUE elif isinstance(f.rel, ManyToManyRel): result_repr = ", ".join(map(str, value.all())) else: result_repr = smart_unicode(value) return conditional_escape(result_repr)
def render_edit_link(obj, db_field, popup=True, request=None): """ """ change_permission = '%s.change_%s' % ( obj._meta.app_label, obj._meta.object_name.lower()) if request and not request.user.has_perm(change_permission, obj): return u'<strong>%s</strong>' % escape(smart_unicode(obj)) try: change_url = reverse( "admin:%s_%s_change" % ( obj._meta.app_label, obj._meta.object_name.lower()), args=(obj.pk,)) except NoReverseMatch: change_url = '#error:no-change-form' input_id = 'id_%s' % db_field.name # (TODO: Use actual id prefix if custom -- pass form in via widget init) return render_to_string( _template_list(obj, '_edit_popup_link.html'), { 'change_url': change_url, 'input_id': input_id, 'object_string': escape(smart_unicode(obj)), 'obj': obj, 'popup': popup, })
def Deserializer(object_list, **options): """ Deserialize simple Python objects back into Django ORM instances. It's expected that you pass the Python objects themselves (instead of a stream or a string) to the constructor """ db = options.pop('using', DEFAULT_DB_ALIAS) models.get_apps() for d in object_list: # Look up the model and starting build a dict of data for it. Model = _get_model(d["model"]) data = {Model._meta.pk.attname : Model._meta.pk.to_python(d["pk"])} m2m_data = {} # Handle each field for (field_name, field_value) in d["fields"].iteritems(): if isinstance(field_value, str): field_value = smart_unicode(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True) field = Model._meta.get_field(field_name) # Handle M2M relations if field.rel and isinstance(field.rel, models.ManyToManyRel): if hasattr(field.rel.to._default_manager, 'get_by_natural_key'): def m2m_convert(value): if hasattr(value, '__iter__'): return field.rel.to._default_manager.db_manager(db).get_by_natural_key(*value).pk else: return smart_unicode(field.rel.to._meta.pk.to_python(value)) else: m2m_convert = lambda v: smart_unicode(field.rel.to._meta.pk.to_python(v)) m2m_data[field.name] = [m2m_convert(pk) for pk in field_value] # Handle FK fields elif field.rel and isinstance(field.rel, models.ManyToOneRel): if field_value is not None: if hasattr(field.rel.to._default_manager, 'get_by_natural_key'): if hasattr(field_value, '__iter__'): obj = field.rel.to._default_manager.db_manager(db).get_by_natural_key(*field_value) value = getattr(obj, field.rel.field_name) # If this is a natural foreign key to an object that # has a FK/O2O as the foreign key, use the FK value if field.rel.to._meta.pk.rel: value = value.pk else: value = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value) data[field.attname] = value else: data[field.attname] = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value) else: data[field.attname] = None # Handle all other fields else: data[field.name] = field.to_python(field_value) yield base.DeserializedObject(Model(**data), m2m_data)
def pygmentify_html(text, **kwargs): text = smart_unicode(text) lang = default_lang = 'text' # a tuple of known lexer names lexer_names = reduce(lambda a, b: a + b[2], LEXERS.itervalues(), ()) # custom formatter defaults = {'encoding': 'utf-8'} defaults.update(kwargs) formatter = HtmlFormatter(**defaults) subs = [] pre_re = re.compile(r'(<pre[^>]*>)(.*?)(</pre>)', re.DOTALL | re.UNICODE) br_re = re.compile(r'<br[^>]*?>', re.UNICODE) lang_re = re.compile(r'lang="(.+?)"', re.DOTALL | re.UNICODE) for pre_match in pre_re.findall(text): work_area = pre_match[1] work_area = br_re.sub('\n', work_area) match = lang_re.search(pre_match[0]) if match: lang = match.group(1).strip() if lang not in lexer_names: lang = default_lang lexer = get_lexer_by_name(lang, stripall=True) work_area = work_area.replace(u' ', u' ').replace(u'&', u'&').replace(u'<', u'<').replace(u'>', u'>').replace(u'"', u'"').replace(u''', u"'") work_area = highlight(work_area, lexer, formatter) subs.append([u''.join(pre_match), smart_unicode(work_area)]) for sub in subs: text = text.replace(sub[0], sub[1], 1) return text
def smart_unicode_cmp(a, b): """Compare two values, converting both to Unicode via smart_unicode() if either value is a string.""" if type(a) in (str, unicode) or type(b) in (str, unicode): a = smart_unicode(a) b = smart_unicode(b) return cmp(a, b)
def clean(self, values): super(ReCaptchaField, self).clean(values[1]) recaptcha_challenge_value = smart_unicode(values[0]) recaptcha_response_value = smart_unicode(values[1]) if os.environ.get('RECAPTCHA_TESTING', None) == 'True' and \ recaptcha_response_value == 'PASSED': return values[0] try: check_captcha = client.submit( recaptcha_challenge_value, recaptcha_response_value, private_key=self.private_key, remoteip=self.get_remote_ip(), use_ssl=self.use_ssl) except socket.error: # Catch timeouts, etc raise ValidationError( self.error_messages['captcha_error'] ) if not check_captcha.is_valid: raise ValidationError( self.error_messages['captcha_invalid'] ) return values[0]
def clean_cpf(self,val,row): # field isn't required in this class! if not val: return val try: val = CPF(val) except ValueError,msg: raise ValidationError,smart_unicode(msg)
def getTextualRepresentation(self, extended=False): """build a textual representation for the charge extended may take the value true, if extended infos are required""" s = u"" # start and end date if self.date_start.month == 1 and self.date_start.day == 1: s += u"dal %s " % self.date_start.year else: s += u"dal %02d/%02d/%04d " % (self.date_start.day, self.date_start.month, self.date_start.year) if self.date_end is not None: if self.date_end.month == 1 and self.date_end.day == 1: s += u"al %s " % self.date_end.year else: s += u"al %02d/%02d/%04d " % (self.date_end.day, self.date_end.month, self.date_end.year) s += u"è stato " else: s += u"è " # charge type and party if self.charge_name is not None: s += u"%s" % (smart_unicode(self.charge_name),) else: s += u"appartenente" s += u" %s (%s)" %\ (smart_unicode(self.organization.name), self.organization.url and smart_unicode(self.organization.url)) return s
def sk_forum_planning_calendar(request): from datetime import datetime, date from icalendar import Calendar, Event, UTC, LocalTimezone # timezone cal = Calendar() cal.add('prodid', '-//SK Forum Calendar//killingar.net//') cal.add('version', '2.0') import MySQLdb connection = MySQLdb.connect(user='******', passwd='2oVGx8VwuqUfY', db='forum') cursor = connection.cursor() from django.utils.encoding import smart_unicode cursor.execute(u'SELECT id FROM users where name = "%s"' % (smart_unicode(request.REQUEST['username']))) userID = cursor.fetchall()[0] cursor.execute("select id, name, time, description from events, eventdata where events.id = eventdata.event and events.visible = 1 and eventdata.user = %s and eventdata.data in (1, 0)" % userID) rows = cursor.fetchall() for row in rows: id, name, time, description = row event = Event() event.add('summary', smart_unicode(name.decode('latin-1'))) #event.add('dtstart', 'DATE:'+time.strftime("%Y%m%d")) #event.add('dtend', 'DATE:'+time.strftime("%Y%m%d")) event.add('dtstart', date(time.year, time.month, time.day)) event.add('dtend', date(time.year, time.month, time.day)) if description: event.add('description', smart_unicode(description.decode('latin-1'))) #event.add('X-FUNAMBOL-ALLDAY', '') event['uid'] = 'planning/%[email protected]' % id event.add('priority', 5) # normal priority cal.add_component(event) connection.close() return HttpResponse(cal.as_string(), content_type='text/calendar')
def gate(request) : ''' this method play the role of a security proxy, by only allowing GET method directly to couchdb, and then filtering the resulting json to remove some parameter that should remain server side ''' if request.POST : keeper(request,'Invalid Acces, use of a POST method') url= '/'+request.path.replace(KUESTIONS_API_GET_URL,couchVar.DB_NAME) print url params=None if request.GET.__contains__('key') : param= smart_unicode(request.GET['key'], encoding='utf-8', strings_only=False, errors='strict') params='?key='+quote(param.encode('UTF8')) if request.GET.__contains__('q') : param=smart_unicode(request.GET['q'], encoding='utf-8', strings_only=False, errors='strict') params='?q='+quote(param.encode('UTF8')) import urllib2 if params : url=unicode(url+params) print url f=urllib2.urlopen("http://localhost:5984"+url) data='' for line in f.readlines(): data+=line return HttpResponse(removeProtectedFields(data))
def getHosts(request): data = {} hosts = Host.objects.all() for host in hosts: data['addHost#_#' + smart_unicode(host.group.pk) + "#_#" + smart_unicode(host.pk)] = smart_unicode(host.name) + "#_#" + smart_unicode(host.ip) data_json = json.dumps(data) return HttpResponse(data_json, mimetype="application/json")
def live_paste(request): commit_kwargs = {} if request.user.is_authenticated(): commit_kwargs = { 'anonymous': request.user.preference.default_anonymous } if request.method != 'POST': return render_to_response( 'live.html', { 'forms': PasteSet(), 'set_form': SetForm(), 'commit_meta_form': CommitMetaForm(initial=commit_kwargs), 'set_meta_form': SetMetaForm(), }, RequestContext(request)) paste_forms = PasteSet(request.POST) set_form = SetForm(request.POST) commit_meta_form = CommitMetaForm(request.POST, initial=commit_kwargs) set_meta_form = SetMetaForm(request.POST) if (not paste_forms.is_valid() or not set_form.is_valid() or not commit_meta_form.is_valid() or not set_meta_form.is_valid()): return render_to_response( 'live.html', { 'forms': paste_forms, 'set_form': set_form, 'commit_meta_form': commit_meta_form, 'set_meta_form': set_meta_form, }, RequestContext(request)) # Repositories are just a random sequence of letters and digits # We store the reference repository for editing the pastes. repo_dir = os.sep.join([ settings.REPO_DIR, "".join(random.sample(string.letters + string.digits, 15)) ]) anonymous = commit_meta_form.cleaned_data.get('anonymous') os.mkdir(repo_dir) owner = None if request.user.is_authenticated() and not anonymous: owner = request.user description = set_form.cleaned_data.get('description') private = set_meta_form.cleaned_data.get('private') allow_edits = set_meta_form.cleaned_data.get('anyone_can_edit') # Calculate expiration time of set if necessary exp_option = set_meta_form.cleaned_data.get('expires') exp_map = { 'day': timedelta(days=1), 'hour': timedelta(hours=1), 'month': timedelta(365 / 12), } exp_time = datetime.utcnow( ) + exp_map[exp_option] if exp_option in exp_map else None # Generate a random hash for private access (20-30 characters from letters & numbers) private_key = ''.join( random.choice(string.ascii_letters + string.digits) for x in range(random.randrange(20, 30))) # Create a new paste set so we can reference our paste. paste_set = Set.objects.create( views=0, repo=repo_dir, owner=owner, description=description, private=private, anyone_can_edit=allow_edits, private_key=private_key, expires=exp_time, ) # Yes, this is horrible. I know. But there is a bug with Python Git. # See: https://github.com/gitpython-developers/GitPython/issues/39 os.environ['USER'] = "******" if owner: os.environ['USER'] = owner.username # Initialize a commit, git repository, and pull the current index. commit = Commit.objects.create(views=0, parent_set=paste_set, commit='', owner=owner) git_repo = git.Repo.init(repo_dir) index = git_repo.index # We enumerate over the forms so we can have a way to reference # the line numbers in a unique way relevant to the pastes. priority_filename = os.sep.join([repo_dir, 'priority.txt']) priority_file = open(priority_filename, 'w') for form_index, form in enumerate(paste_forms): data = form.cleaned_data filename = data['filename'] language_lex, language = data['language'].split(';') paste = data['paste'] # If we don't specify a filename, then obviously it is lonely if not len(filename): filename = 'paste' # Construct a more logical filename for our commit filename_base, ext = os.path.splitext(filename) filename_slugify = slugify(filename[:len(ext)]) filename_absolute = os.sep.join([repo_dir, filename]) filename_absolute += ext filename_abs_base, ext = os.path.splitext(filename_absolute) # If no extension was specified in the file, then we can append # the extension from the lexer. if not len(ext): filename_absolute += language filename += language ext = language # Gists doesn't allow for the same filename, we do. # Just append a number to the filename and call it good. i = 1 while os.path.exists(filename_absolute): filename_absolute = '%s-%d%s' % (filename_abs_base, i, ext) filename = '%s-%d%s' % (filename_base, i, ext) i += 1 cleaned = [] paste = paste.encode('UTF-8') for line in paste.split('\n'): line = line.rstrip() cleaned.append(line) paste = '\n'.join(cleaned) # Open the file, write the paste, call it good. f = open(filename_absolute, "w") f.write(paste) f.close() priority_file.write('%s: %s\n' % (filename, data['priority'])) paste = smart_unicode(paste) # This is a bit nasty and a get_by_ext something exist in pygments. # However, globals() is just much more fun. lex = globals()[language_lex] paste_formatted = highlight( paste, lex(), HtmlFormatter(style='friendly', linenos='table', lineanchors='line-%s' % form_index, anchorlinenos=True)) # Add the file to the index and create the paste index.add([filename_absolute]) p = Paste.objects.create(filename=filename, absolute_path=filename_absolute, paste=paste, priority=data['priority'], paste_formatted=paste_formatted, language=data['language'], revision=commit) # Add a priority file priority_file.close() index.add([priority_filename]) # Create the commit from the index new_commit = index.commit('Initial paste.') commit.commit = new_commit commit.save() if not paste_set.private: return redirect('paste_view', pk=paste_set.pk) else: return redirect('paste_view', pk=paste_set.pk, private_key=paste_set.private_key) return render_to_response('live.html')
def loginCheck(request): ##로그인 할때 체킹하는 부분 template_name = 'html/login.html' m_template_name = "m_skins/m_html/login.html" Mobile = request.flavour if request.method == 'POST': if request.POST.get('id', 'None') == 'admin_seal': username = request.POST['id'] password = request.POST['pw'] user = authenticate(username=username, password=password) elif 'stu_num' in request.POST: # 학번 값이 들어올 경우 해당 학번으로 로그인 제공. username = request.POST['stu_num'] user = User.objects.filter(username=username)[0] elif 'stuNum' in request.POST: # 학번 값이 들어올 경우 해당 학번으로 로그인 제공. if request.user.is_authenticated(): # 로그인 중일 때는 로그아웃 후에 재 로그인 logout(request) username = request.POST['stuNum'] if not username: LoginError(request) # igo에서 받아온 값이 비어있을 경우 로그인 에러 발생 try: user = User.objects.filter(username=username)[0] except IndexError: return HisnetCheck(request) # user = None elif request.POST.get('id', 'None'): username = request.POST['id'] # 여기서 username은 학번이 아닌 입력받은 히스넷 아이디 password = request.POST['pw'] # 크롤링 Configuration browser = mechanize.Browser() browser.set_handle_robots(False) browser.open("https://hisnet.handong.edu/login/login.php") browser.select_form(name='login') browser.form['id'] = smart_unicode(username).encode('euc-kr') browser.form['password'] = password browser.submit() browser.open( "https://hisnet.handong.edu/haksa/hakjuk/HHAK110M.php") contents = browser.response().read() soup = BeautifulSoup(contents, "html.parser") titles = soup.find_all(class_='tblcationTitlecls') # Save the information try: stu_num = titles[2].next_sibling.next_sibling.text[-8:] temp_major = titles[11].next_sibling.next_sibling.text.split( '.') first_major = temp_major[0] except IndexError as e: return LoginError(request) # 학번 혹은 전공 확인되지 않으면 로그인 에러 try: second_major = temp_major[1] except IndexError as e: second_major = None try: user = User.objects.filter(username=stu_num)[0] except IndexError: return HisnetCheck(request) # 해당 학번이 가입되지 않았을 경우 가입 ##로그인 완료시 메인페이지 view if user is not None: user.backend = 'django.contrib.auth.backends.ModelBackend' # To login without password auth_login(request, user) #메인페이지 보여줄 함수 호출 UserData = MainPageView(request.user, None, None, 2, Mobile) request.session['PageInformation'] = [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]] if request.flavour == 'full': return render(request, 'html/index.html', UserData) else: return render(request, "m_skins/m_html/index.html", UserData) else: return LoginError(request) #로그인 되지 않았을 경우 다시 로그인페이지로 elif request.user.username == "": if request.flavour == 'full': return render(request, 'html/login.html', {'user': None}) else: return render(request, 'm_skins/m_html/login.html', {'user': None}) #이미 로그인 되어있으면 else: UserData = MainPageView(request.user, None, None, 2, Mobile) request.session['PageInformation'] = [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]] if request.flavour == 'full': return render(request, 'html/index.html', UserData) else: return render(request, "m_skins/m_html/index.html", UserData)
def __unicode__(self): return smart_unicode(self.email)
def posix_shell(chan, channel, log_name=None, width=90, height=40): from shell.asgi import channel_layer stdout = list() begin_time = time.time() last_write_time = {'last_activity_time': begin_time} try: chan.settimeout(0.0) while True: try: x = u(chan.recv(1024)) if len(x) == 0: channel_layer.send( channel, { 'text': json.dumps([ 'disconnect', smart_unicode('\r\n*** EOF\r\n') ]) }) break now = time.time() delay = now - last_write_time['last_activity_time'] last_write_time['last_activity_time'] = now if x == "exit\r\n" or x == "logout\r\n" or x == 'logout': chan.close() else: if isinstance(x, unicode): stdout.append([delay, x]) else: stdout.append([ delay, codecs.getincrementaldecoder('UTF-8')( 'replace').decode(x) ]) if isinstance(x, unicode): channel_layer.send(channel, {'text': json.dumps(['stdout', x])}) else: channel_layer.send( channel, {'text': json.dumps(['stdout', smart_unicode(x)])}) #send message to monitor group if log_name: channel_layer.send_group( u'monitor-{0}'.format( log_name.rsplit('/')[1].rsplit('.json')[0]), {'text': json.dumps(['stdout', smart_unicode(x)])}) except socket.timeout: pass except Exception, e: print traceback.print_exc() channel_layer.send( channel, { 'text': json.dumps([ 'stdout', 'A bug find,You can report it to me' + smart_unicode(e) ]) }) finally: attrs = { "version": 1, "width": width, #int(subprocess.check_output(['tput', 'cols'])), "height": height, #int(subprocess.check_output(['tput', 'lines'])), "duration": round(time.time() - begin_time, 6), "command": os.environ.get('SHELL', None), 'title': None, "env": { "TERM": os.environ.get('TERM'), "SHELL": os.environ.get('SHELL', 'sh') }, 'stdout': list(map(lambda frame: [round(frame[0], 6), frame[1]], stdout)) } mkdir_p('/'.join(os.path.join(MEDIA_ROOT, log_name).rsplit('/')[0:-1])) with open(os.path.join(MEDIA_ROOT, log_name), "a") as f: f.write( json.dumps(attrs, ensure_ascii=True, cls=CustomeFloatEncoder, indent=2)) audit_log = SshLog.objects.get( channel=channel, log=log_name.rsplit('/')[-1].rsplit('.json')[0]) audit_log.is_finished = True audit_log.end_time = timezone.now() audit_log.save() #hand ssh terminal exit queue = get_redis_instance() redis_channel = queue.pubsub() queue.publish(channel, json.dumps(['close']))
def assertFileContains(self, filepath, text): self.assertIn(text, self._get_file(smart_unicode(filepath)), u"'%s' not in '%s'" % (text, filepath))
def help_text_for_field(name, model): try: help_text = model._meta.get_field_by_name(name)[0].help_text except models.FieldDoesNotExist: help_text = "" return smart_unicode(help_text)
def items_for_tree_result(cl, result, form): """ Generates the actual list of data. """ first = True pk = cl.lookup_opts.pk.attname for field_name in cl.list_display: row_class = '' try: f, attr, value = lookup_field(field_name, result, cl.model_admin) except (AttributeError, ObjectDoesNotExist): result_repr = EMPTY_CHANGELIST_VALUE else: if f is None: allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) # Strip HTML tags in the resulting text, except if the # function has an "allow_tags" attribute set to True. if not allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: if value is None: result_repr = EMPTY_CHANGELIST_VALUE if isinstance(f.rel, models.ManyToOneRel): result_repr = escape(getattr(result, f.name)) else: result_repr = display_for_field(value, f) if isinstance(f, models.DateField) or isinstance( f, models.TimeField): row_class = ' class="nowrap"' if first: try: f, attr, checkbox_value = lookup_field( 'action_checkbox', result, cl.model_admin) #result_repr = mark_safe("%s%s" % (value, result_repr)) if row_class: row_class = "%s%s" % (row_class[:-1], ' disclosure"') else: row_class = ' class="disclosure"' except (AttributeError, ObjectDoesNotExist): pass if force_unicode(result_repr) == '': result_repr = mark_safe(' ') # If list_display_links not defined, add the link tag to the first field if (first and not cl.list_display_links ) or field_name in cl.list_display_links: table_tag = 'td' #{True:'th', False:'td'}[first] url = cl.url_for_result(result) # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if cl.to_field: attr = str(cl.to_field) else: attr = pk value = result.serializable_value(attr) result_id = repr(force_unicode(value))[1:] first = False yield mark_safe(u'<%s%s>%s<a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, checkbox_value, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag)) else: # By default the fields come from ModelAdmin.list_editable, but if we pull # the fields out of the form instead of list_editable custom admins # can provide fields on a per request basis if form and field_name in form.fields: bf = form[field_name] result_repr = mark_safe( force_unicode(bf.errors) + force_unicode(bf)) else: result_repr = conditional_escape(result_repr) yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr)) if form and not form[cl.model._meta.pk.name].is_hidden: yield mark_safe(u'<td>%s</td>' % force_unicode(form[cl.model._meta.pk.name]))
def __unicode__(self): return smart_unicode(self.collection_code, u'utf-8', u'replace')
def clean_and_parse_xml(xml_string): clean_xml_str = xml_string.strip() clean_xml_str = re.sub(r">\s+<", "><", smart_unicode(clean_xml_str)) xml_obj = minidom.parseString(smart_str(clean_xml_str)) return xml_obj
def __unicode__(self): return smart_unicode(self.title_page, u'utf-8', u'replace')
def items_for_result(cl, result, form): """ Generates the actual list of data. @jjdelc: This has been shamelessly copied from original django.contrib.admin.templatetags.admin_list.items_for_result in order to alter the dispay for the first element """ first = True pk = cl.lookup_opts.pk.attname for field_name in cl.list_display: row_class = '' try: f, attr, value = lookup_field(field_name, result, cl.model_admin) except (AttributeError, ObjectDoesNotExist): result_repr = EMPTY_CHANGELIST_VALUE else: if f is None: allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) # Strip HTML tags in the resulting text, except if the # function has an "allow_tags" attribute set to True. if not allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: if value is None: result_repr = EMPTY_CHANGELIST_VALUE if isinstance(f.rel, models.ManyToOneRel): result_repr = escape(getattr(result, f.name)) else: result_repr = display_for_field(value, f) if (isinstance(f, models.DateField) or isinstance(f, models.TimeField)): row_class = ' class="nowrap"' if force_unicode(result_repr) == '': result_repr = mark_safe(' ') # If list_display_links not defined, add the link tag to the # first field if ((first and not cl.list_display_links) or field_name in cl.list_display_links): table_tag = {True: 'th', False: 'td'}[first] # This spacer indents the nodes based on their depth if first: spacer = '<span class="spacer"> </span>' * ( result.get_depth() - 1) else: spacer = '' # This shows a collapse or expand link for nodes with childs if result.get_children_count(): collapse = ('<a href="#" title="" class="collapse expanded">' '-</a>') else: collapse = '<span class="collapse"> </span>' # Add a <td/> before the first col to show the drag handler drag_handler = '' if first: drag_handler = ('<td class="drag-handler">' '<span> </span></td>') first = False url = cl.url_for_result(result) # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if cl.to_field: attr = str(cl.to_field) else: attr = pk value = result.serializable_value(attr) result_id = repr(force_unicode(value))[1:] onclickstr = ( ' onclick="opener.dismissRelatedLookupPopup(window, %s);' ' return false;"') yield mark_safe( u'%s<%s%s>%s %s <a href="%s"%s>%s</a></%s>' % (drag_handler, table_tag, row_class, spacer, collapse, url, (cl.is_popup and onclickstr % result_id or ''), conditional_escape(result_repr), table_tag)) else: # By default the fields come from ModelAdmin.list_editable, but # if we pull the fields out of the form instead of # list_editable custom admins can provide fields on a per # request basis if form and field_name in form.fields: bf = form[field_name] result_repr = mark_safe( force_unicode(bf.errors) + force_unicode(bf)) else: result_repr = conditional_escape(result_repr) yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr)) if form and not form[cl.model._meta.pk.name].is_hidden: yield mark_safe(u'<td>%s</td>' % force_unicode(form[cl.model._meta.pk.name]))
def home(request): """ Displays a list of messages to be translated """ def fix_nls(in_, out_): """Fixes submitted translations by filtering carriage returns and pairing newlines at the begging and end of the translated string with the original """ if 0 == len(in_) or 0 == len(out_): return out_ if "\r" in out_ and "\r" not in in_: out_ = out_.replace("\r", '') if "\n" == in_[0] and "\n" != out_[0]: out_ = "\n" + out_ elif "\n" != in_[0] and "\n" == out_[0]: out_ = out_.lstrip() if "\n" == in_[-1] and "\n" != out_[-1]: out_ = out_ + "\n" elif "\n" != in_[-1] and "\n" == out_[-1]: out_ = out_.rstrip() return out_ version = rosetta.get_version(True) if 'rosetta_i18n_fn' in request.session: rosetta_i18n_fn = request.session.get('rosetta_i18n_fn') rosetta_i18n_pofile = request.session.get('rosetta_i18n_pofile') rosetta_i18n_lang_code = request.session['rosetta_i18n_lang_code'] rosetta_i18n_lang_bidi = (rosetta_i18n_lang_code in settings.LANGUAGES_BIDI) rosetta_i18n_write = request.session.get('rosetta_i18n_write', True) if 'filter' in request.GET: if request.GET.get('filter') == 'untranslated' or request.GET.get( 'filter') == 'translated' or request.GET.get( 'filter') == 'both': filter_ = request.GET.get('filter') request.session['rosetta_i18n_filter'] = filter_ return HttpResponseRedirect(reverse('rosetta-home')) elif 'rosetta_i18n_filter' in request.session: rosetta_i18n_filter = request.session.get('rosetta_i18n_filter') else: rosetta_i18n_filter = 'both' if '_next' in request.POST: rx = re.compile(r'^m_([0-9]+)') rx_plural = re.compile(r'^m_([0-9]+)_([0-9]+)') file_change = False for k in request.POST.keys(): if rx_plural.match(k): id = int(rx_plural.match(k).groups()[0]) idx = int(rx_plural.match(k).groups()[1]) rosetta_i18n_pofile[id].msgstr_plural[str(idx)] = fix_nls( rosetta_i18n_pofile[id].msgid_plural[idx], request.POST.get(k)) file_change = True elif rx.match(k): id = int(rx.match(k).groups()[0]) rosetta_i18n_pofile[id].msgstr = fix_nls( rosetta_i18n_pofile[id].msgid, request.POST.get(k)) file_change = True if file_change and 'fuzzy' in rosetta_i18n_pofile[id].flags: rosetta_i18n_pofile[id].flags.remove('fuzzy') if file_change and rosetta_i18n_write: try: rosetta_i18n_pofile.metadata['Last-Translator'] = str( u"%s %s <%s>" % (request.user.first_name, request.user.last_name, request.user.email)) rosetta_i18n_pofile.metadata['X-Translated-Using'] = str( u"django-rosetta %s" % rosetta.get_version(False)) rosetta_i18n_pofile.metadata[ 'PO-Revision-Date'] = datetime.datetime.now().strftime( '%Y-%m-%d %H:%M%z') except UnicodeDecodeError: pass try: rosetta_i18n_pofile.save() rosetta_i18n_pofile.save_as_mofile( rosetta_i18n_fn.replace('.po', '.mo')) # Try auto-reloading via the WSGI daemon mode reload mechanism if rosetta_settings.WSGI_AUTO_RELOAD and \ request.environ.has_key('mod_wsgi.process_group') and \ request.environ.get('mod_wsgi.process_group',None) and \ request.environ.has_key('SCRIPT_FILENAME') and \ int(request.environ.get('mod_wsgi.script_reloading', '0')): try: os.utime(request.environ.get('SCRIPT_FILENAME'), None) except OSError: pass except: request.session['rosetta_i18n_write'] = False request.session['rosetta_i18n_pofile'] = rosetta_i18n_pofile # Retain query arguments query_arg = '' if 'query' in request.REQUEST: query_arg = '?query=%s' % request.REQUEST.get('query') if 'page' in request.GET: if query_arg: query_arg = query_arg + '&' else: query_arg = '?' query_arg = query_arg + 'page=%d' % int( request.GET.get('page')) return HttpResponseRedirect( reverse('rosetta-home') + query_arg) rosetta_i18n_lang_name = _( request.session.get('rosetta_i18n_lang_name')) rosetta_i18n_lang_code = request.session.get('rosetta_i18n_lang_code') if 'query' in request.REQUEST and request.REQUEST.get('query', '').strip(): query = request.REQUEST.get('query').strip() rx = re.compile(query, re.IGNORECASE) paginator = Paginator([ e for e in rosetta_i18n_pofile if rx.search( smart_unicode(e.msgstr) + smart_unicode(e.msgid) + u''.join([o[0] for o in e.occurrences])) ], rosetta_settings.MESSAGES_PER_PAGE) else: if rosetta_i18n_filter == 'both': paginator = Paginator(rosetta_i18n_pofile, rosetta_settings.MESSAGES_PER_PAGE) elif rosetta_i18n_filter == 'untranslated': paginator = Paginator( rosetta_i18n_pofile.untranslated_entries(), rosetta_settings.MESSAGES_PER_PAGE) elif rosetta_i18n_filter == 'translated': paginator = Paginator(rosetta_i18n_pofile.translated_entries(), rosetta_settings.MESSAGES_PER_PAGE) if 'page' in request.GET and int( request.GET.get('page')) <= paginator.num_pages and int( request.GET.get('page')) > 0: page = int(request.GET.get('page')) else: page = 1 messages = paginator.page(page).object_list needs_pagination = paginator.num_pages > 1 if needs_pagination: if paginator.num_pages >= 10: page_range = pagination_range(1, paginator.num_pages, page) else: page_range = range(1, 1 + paginator.num_pages) ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS return render_to_response('rosetta/pofile.html', locals()) else: return list_languages(request)
def Deserializer(object_list, **options): """ Deserialize simple Python objects back into Django ORM instances. It's expected that you pass the Python objects themselves (instead of a stream or a string) to the constructor """ db = options.pop('using', DEFAULT_DB_ALIAS) models.get_apps() for d in object_list: # Look up the model and starting build a dict of data for it. Model = _get_model(d["model"]) data = {Model._meta.pk.attname: Model._meta.pk.to_python(d["pk"])} m2m_data = {} # Handle each field for (field_name, field_value) in d["fields"].iteritems(): if isinstance(field_value, str): field_value = smart_unicode(field_value, options.get( "encoding", settings.DEFAULT_CHARSET), strings_only=True) field = Model._meta.get_field(field_name) # Handle M2M relations if field.rel and isinstance(field.rel, models.ManyToManyRel): if hasattr(field.rel.to._default_manager, 'get_by_natural_key'): def m2m_convert(value): if hasattr(value, '__iter__'): return field.rel.to._default_manager.db_manager( db).get_by_natural_key(*value).pk else: return smart_unicode( field.rel.to._meta.pk.to_python(value)) else: m2m_convert = lambda v: smart_unicode( field.rel.to._meta.pk.to_python(v)) m2m_data[field.name] = [m2m_convert(pk) for pk in field_value] # Handle FK fields elif field.rel and isinstance(field.rel, models.ManyToOneRel): if field_value is not None: if hasattr(field.rel.to._default_manager, 'get_by_natural_key'): if hasattr(field_value, '__iter__'): obj = field.rel.to._default_manager.db_manager( db).get_by_natural_key(*field_value) value = getattr(obj, field.rel.field_name) # If this is a natural foreign key to an object that # has a FK/O2O as the foreign key, use the FK value if field.rel.to._meta.pk.rel: value = value.pk else: value = field.rel.to._meta.get_field( field.rel.field_name).to_python(field_value) data[field.attname] = value else: data[field.attname] = field.rel.to._meta.get_field( field.rel.field_name).to_python(field_value) else: data[field.attname] = None # Handle all other fields else: data[field.name] = field.to_python(field_value) yield base.DeserializedObject(Model(**data), m2m_data)
def create_from_data(self, repository, diff_file_name, diff_file_contents, parent_diff_file_name, parent_diff_file_contents, diffset_history, basedir, request, base_commit_id=None, save=True): """Create a DiffSet from raw diff data. The diff_file_contents and parent_diff_file_contents parameters are strings with the actual diff contents. """ from reviewboard.diffviewer.diffutils import convert_to_unicode from reviewboard.diffviewer.models import FileDiff tool = repository.get_scmtool() encoding, diff_text = convert_to_unicode( diff_file_contents, repository.get_encoding_list()) parser = tool.get_parser(diff_text) files = list( self._process_files( parser, basedir, repository, base_commit_id, request, check_existence=(not parent_diff_file_contents))) # Parse the diff if len(files) == 0: raise EmptyDiffError(_("The diff file is empty")) # Sort the files so that header files come before implementation. files.sort(cmp=self._compare_files, key=lambda f: f.origFile) # Parse the parent diff parent_files = {} # This is used only for tools like Mercurial that use atomic changeset # IDs to identify all file versions but not individual file version # IDs. parent_commit_id = None if parent_diff_file_contents: diff_filenames = set([f.origFile for f in files]) parent_parser = tool.get_parser( convert_to_unicode(parent_diff_file_contents, [encoding])[1]) # If the user supplied a base diff, we need to parse it and # later apply each of the files that are in the main diff for f in self._process_files(parent_parser, basedir, repository, base_commit_id, request, check_existence=True, limit_to=diff_filenames): parent_files[f.origFile] = f # This will return a non-None value only for tools that use # commit IDs to identify file versions as opposed to file revision # IDs. parent_commit_id = parent_parser.get_orig_commit_id() diffset = super(DiffSetManager, self).create(name=diff_file_name, revision=0, basedir=basedir, history=diffset_history, repository=repository, diffcompat=DiffCompatVersion.DEFAULT, base_commit_id=base_commit_id) if save: diffset.save() for f in files: if f.origFile in parent_files: parent_file = parent_files[f.origFile] parent_content = parent_file.data.encode(encoding) source_rev = parent_file.origInfo else: parent_content = b"" if parent_commit_id and f.origInfo != PRE_CREATION: source_rev = parent_commit_id else: source_rev = f.origInfo dest_file = os.path.join(basedir, f.newFile).replace("\\", "/") if f.deleted: status = FileDiff.DELETED elif f.moved: status = FileDiff.MOVED else: status = FileDiff.MODIFIED filediff = FileDiff(diffset=diffset, source_file=f.origFile, dest_file=dest_file, source_revision=smart_unicode(source_rev), dest_detail=f.newInfo, diff=f.data.encode(encoding), parent_diff=parent_content, binary=f.binary, status=status) filediff.set_line_counts(raw_insert_count=f.insert_count, raw_delete_count=f.delete_count) if save: filediff.save() return diffset
def get_absolute_url(self): return reverse("forums-post", args=[ smart_unicode(self.thread.forum.name_slug), self.thread.id, self.id ])
def prep_for_like_query(self, x): """Prepares a value for use in a LIKE query.""" from django.utils.encoding import smart_unicode return smart_unicode(x).replace("\\", "\\\\").replace("%", "\%").replace("_", "\_")
def string_field(self, value): if value in EMPTY_VALUES: return None return smart_unicode(value)
def get_absolute_url(self): return reverse("forums-forum", args=[smart_unicode(self.name_slug)])
def __repr__(self): return smart_unicode(self.action_time)
def parse_anntaylor(self, response): self.check_shelfit_validity(response) return (False, None) hxs = HtmlXPathSelector(response) # find name of item item_name_path = hxs.select('//div[@class="hd-info"]//h1/text()') if len(item_name_path) == 0: self.invalid_links += 1 print "Invalid link: " + str(response.url) return (False, None) item_name = smart_unicode(item_name_path.extract()[0]) logging.critical("Name: " + item_name) self.count_scraped += 1 ''' PLAYING NICE: sleeping for 1min after crawling every 100 pages ''' if self.count_scraped % 100 == 0: print "Sleeping for 60 secs..." sleep(60) # sleep for 1 mins for express meta_tag_url = hxs.select('//meta[@property="og:url"]/@content') prod_url = meta_tag_url.extract()[0] logging.critical("PRODUCT URL:" + str(prod_url) + " ITEM_NAME " + smart_unicode(item_name) + " TOTAL SO FAR " + str(self.count_scraped)) # Ann Taylor is for women only gender = 'F' # find price and sale price item_id_, price_, sale_price_ = self._find_price(hxs, prod_url) if item_id_ in self.items_scraped: logging.critical("ITEM ALREADY SCRAPED " + str(item_id_)) # store the category for this itemid print "Appending categories for product " + str(item_id_) categories_path = hxs.select( '//div[@id="cat-pro-pagnation"]//a/text()').extract() num_categories = len(categories_path) categories = [] for i in range(0, num_categories): category = str(categories_path[i]).strip('\n').strip() categories.append(category) logging.critical("Categories: " + category) product = ProductModel.objects.filter(idx=item_id_).filter( insert_date=insert_date) self._create_category(product, categories) return (False, None) else: self.items_scraped.append(item_id_) logging.critical("ITEM_ID " + str(item_id_) + " PRICE " + str(price_) + " SALE PRICE " + str(sale_price_)) if price_ > sale_price_: logging.critical("SALE on ITEM_ID " + str(item_id_) + " PRICE " + str(price_) + " SALE PRICE " + str(sale_price_)) # extract image URL prod_img_path = hxs.select('//img[@id="productImage"]/@src') prod_img_url = str(prod_img_path.extract()[0]) logging.critical("Image URL: " + str(prod_img_url)) # find description and keywords: these will be useful in categorization desc = hxs.select( '//div[@class="gu gu-first description"]/p/text()').extract() prod_desc = ''.join(desc) logging.critical("Description: " + prod_desc) # promo text # DIDN'T FIND ANY #promo_path = hxs.select('//span[@class="cat-pro-promo-text"]//font/text()').extract() #promo_str = str(promo_path) #logging.critical("Promotion: ") #logging.critical(promo_str) promo_str = "" product, created_new = self._create_product_item(item_name, int(item_id_), str(prod_url), price_, \ sale_price_, gender, str(prod_img_url), promo_str, prod_desc) product = None #self._store_in_file(response, item_id_) #raise CloseSpider('Blah') logging.critical("Total unique items: " + str(len(self.all_items_scraped)) + " we have scraped so far: " +\ str(self.count_scraped) + " Unique URLs scraped: " + str(len(self.urls_scraped))) #raise SystemExit return (True, product)
def adjust_typo(texte, html=True): texte = smart_unicode(texte).strip() if not texte or (html and re.match(r'(\s*<(/?[^>]*[^>/]|br /)>\s*)+$', texte, re.UNICODE | re.IGNORECASE)): return u'' # TODO: add unit tests # TODO: in regex add code to ignore tags replacement if html: # remove HTML tags before processing text tokens = re.findall(u'<[^>]+>', texte) for idx, value in enumerate(tokens): texte = texte.replace(value, ']TAG%s[' % idx, 1) # replace OE and AE by their correct ligature, Œ and Æ. for old, new in ligatures: texte = texte.replace(old, new) # TODO: verify if these cases are cover # s/—/—/g; # s/ - / — /g; # s/--/—/g; # s/—/—/g; # s/ — / — /g; # s/—/—/g; # do some typographic adjustments (mostly putting non-breaking space where needed) regexs = [ (u' +', u' '), # remove more then one normal space (u' +', u' '), # remove more then one special space (u'«(\s| )+', u'« '), # make space non-breaking after « (u'(\s| )+»', u' »'), # make space non-breaking before » (u'«([^&])', u'« \g<1>'), # add non-breaking space after « (u'([^;])»', u'\g<1> »'), # add non-breaking space before » (u'(\s| )+(:|;|\?|!|$|%)', u' \g<2>'), # make space non-breaking before :, ?, !, $, % ( u'(\d)(\s| )+(cm)', u'\g<1> \g<3>' ), # put non-breaking space between groups in long numbers (ex.: 23 000) ( u'(\d)(\s| )+(\d{3})', u'\g<1> \g<3>' ), # put non-breaking space between groups in long numbers (ex.: 23 000) (u'(\s| )P\.(\s| )', u'\g<1>P. '), # put non-breaking space after Page abbreviation (u'(\s| )p\.', u' p.'), # put non-breaking space before page abbreviation (u' -- ', u' — '), # changed 2 hyphen in a EM dash (u'&(l|g)t;', u'&\g<1>t;' ), # to keep < and > as entities when doing unescape_entities ] if html: regexs.extend([ (u'(\d)(ème|e|es)(\s| |-)', u'\g<1><sup>\g<2></sup>\g<3>' ), # put number extension in exposant (ex. 2e) (u'([IVX])e(\s| )', u'\g<1><sup>e</sup>\g<2>' ), # put roman number extension in exposant (ex. Xe) (u'1er(\s| |-)', u'1<sup>er</sup>\g<1>'), # put 1 extension in exposant (ex. 1er) ]) for old, new in regexs: texte = re.sub(old, new, texte) # replace html tags at their good location if html: for idx, value in enumerate(tokens): texte = texte.replace(']TAG%s[' % idx, value, 1) # do more typographic adjustments with smartypants texte = typogrify.smartypants(texte) return unescape_entities(texte).strip()
def log_ex_with_message(message, inner_ex): return "{0}{sep}Inner Exception: {1}{sep}\t{2}".format( message, type(inner_ex), encoding.smart_unicode(inner_ex, errors='ignore'), sep=os.linesep )
def get_string_value(self, obj, field): """ Convert a field's value to a string. """ return smart_unicode(field.value_to_string(obj))
def log_action(self, user_id, content_type_id, object_id, object_repr, action_flag, change_message=''): e = self.model(None, None, user_id, content_type_id, smart_unicode(object_id), object_repr[:200], action_flag, change_message) e.save()
try: fb = FormatsBackend(resource, language) template = fb.compile_translation(**kwargs) except Exception, e: messages.error(request, "Error compiling translation file.") logger.error("Error compiling '%s' file for '%s': %s" % (language, resource, str(e))) return HttpResponseRedirect( reverse('resource_detail', args=[resource.project.slug, resource.slug]), ) response = HttpResponse(template, mimetype=registry.mimetypes_for( resource.i18n_method)[0]) _filename = "%(proj)s_%(res)s_%(lang)s%(type)s" % { 'proj': smart_unicode(resource.project.slug), 'res': smart_unicode(resource.slug), 'lang': language.code, 'type': registry.file_extension_for(resource, language) } # Prefix filename with mode, case it exists if kwargs.has_key('mode'): _filename = "%s_" % kwargs.get('mode').label + _filename response['Content-Disposition'] = ('attachment; filename=%s' % _filename) return response # Restrict access only for private projects # DONT allow anonymous access
def smart_path(string): """Returns a string you can pass to path.path safely.""" if os.path.supports_unicode_filenames: return smart_unicode(string) return smart_str(string)
def __unicode__(self): return smart_unicode('%s' % (self.name))
def home(request): """ Displays a list of messages to be translated """ def fix_nls(in_,out_): """Fixes submitted translations by filtering carriage returns and pairing newlines at the begging and end of the translated string with the original """ if 0 == len(in_) or 0 == len(out_): return out_ if "\r" in out_ and "\r" not in in_: out_=out_.replace("\r",'') if "\n" == in_[0] and "\n" != out_[0]: out_ = "\n" + out_ elif "\n" != in_[0] and "\n" == out_[0]: out_ = out_.lstrip() if "\n" == in_[-1] and "\n" != out_[-1]: out_ = out_ + "\n" elif "\n" != in_[-1] and "\n" == out_[-1]: out_ = out_.rstrip() return out_ version = rosetta.get_version(True) if 'rosetta_i18n_fn' in request.session: rosetta_i18n_fn=request.session.get('rosetta_i18n_fn') rosetta_i18n_app = get_app_name(rosetta_i18n_fn) rosetta_i18n_lang_code = request.session['rosetta_i18n_lang_code'] rosetta_i18n_lang_bidi = rosetta_i18n_lang_code.split('-')[0] in settings.LANGUAGES_BIDI rosetta_i18n_write = request.session.get('rosetta_i18n_write', True) if rosetta_i18n_write: rosetta_i18n_pofile = pofile(rosetta_i18n_fn) for entry in rosetta_i18n_pofile: entry.md5hash = hashlib.md5(entry.msgid.encode("utf8")+entry.msgstr.encode("utf8")).hexdigest() else: rosetta_i18n_pofile = request.session.get('rosetta_i18n_pofile') if 'filter' in request.GET: if request.GET.get('filter') in ('untranslated', 'translated', 'fuzzy', 'all'): filter_ = request.GET.get('filter') request.session['rosetta_i18n_filter'] = filter_ return HttpResponseRedirect(reverse('rosetta-home')) rosetta_i18n_filter = request.session.get('rosetta_i18n_filter', 'all') if '_next' in request.POST: rx = re.compile(r'^m_([0-9a-f]+)') rx_plural = re.compile(r'^m_([0-9a-f]+)_([0-9]+)') file_change = False for key, value in request.POST.items(): md5hash = None plural_id = None if rx_plural.match(key): md5hash = str(rx_plural.match(key).groups()[0]) # polib parses .po files into unicode strings, but # doesn't bother to convert plural indexes to int, # so we need unicode here. plural_id = unicode(rx_plural.match(key).groups()[1]) elif rx.match(key): md5hash = str(rx.match(key).groups()[0]) if md5hash is not None: entry = rosetta_i18n_pofile.find(md5hash, 'md5hash') # If someone did a makemessage, some entries might # have been removed, so we need to check. if entry: if plural_id is not None: plural_string = fix_nls(entry.msgstr_plural[plural_id], value) entry.msgstr_plural[plural_id] = plural_string else: entry.msgstr = fix_nls(entry.msgid, value) is_fuzzy = bool(request.POST.get('f_%s' % md5hash, False)) if 'fuzzy' in entry.flags and not is_fuzzy: entry.flags.remove('fuzzy') elif 'fuzzy' not in entry.flags and is_fuzzy: entry.flags.append('fuzzy') file_change = True else: request.session['rosetta_last_save_error'] = True if file_change and rosetta_i18n_write: try: rosetta_i18n_pofile.metadata['Last-Translator'] = unicodedata.normalize('NFKD', u"%s %s <%s>" %(request.user.first_name,request.user.last_name,request.user.email)).encode('ascii', 'ignore') rosetta_i18n_pofile.metadata['X-Translated-Using'] = u"django-rosetta %s" % rosetta.get_version(False) rosetta_i18n_pofile.metadata['PO-Revision-Date'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M%z') except UnicodeDecodeError: pass try: rosetta_i18n_pofile.save() rosetta_i18n_pofile.save_as_mofile(rosetta_i18n_fn.replace('.po','.mo')) # Try auto-reloading via the WSGI daemon mode reload mechanism if rosetta_settings.WSGI_AUTO_RELOAD and \ request.environ.has_key('mod_wsgi.process_group') and \ request.environ.get('mod_wsgi.process_group',None) and \ request.environ.has_key('SCRIPT_FILENAME') and \ int(request.environ.get('mod_wsgi.script_reloading', '0')): try: os.utime(request.environ.get('SCRIPT_FILENAME'), None) except OSError: pass except: request.session['rosetta_i18n_write'] = False request.session['rosetta_i18n_pofile']=rosetta_i18n_pofile # Retain query arguments query_arg = '' if 'query' in request.REQUEST: query_arg = '?query=%s' %request.REQUEST.get('query') if 'page' in request.GET: if query_arg: query_arg = query_arg + '&' else: query_arg = '?' query_arg = query_arg + 'page=%d' % int(request.GET.get('page')) return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg)) rosetta_i18n_lang_name = _(request.session.get('rosetta_i18n_lang_name')) rosetta_i18n_lang_code = request.session.get('rosetta_i18n_lang_code') if 'query' in request.REQUEST and request.REQUEST.get('query','').strip(): query=request.REQUEST.get('query').strip() rx=re.compile(re.escape(query), re.IGNORECASE) paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete and rx.search(smart_unicode(e.msgstr)+smart_unicode(e.msgid)+u''.join([o[0] for o in e.occurrences]))], rosetta_settings.MESSAGES_PER_PAGE) else: if rosetta_i18n_filter == 'untranslated': paginator = Paginator(rosetta_i18n_pofile.untranslated_entries(), rosetta_settings.MESSAGES_PER_PAGE) elif rosetta_i18n_filter == 'translated': paginator = Paginator(rosetta_i18n_pofile.translated_entries(), rosetta_settings.MESSAGES_PER_PAGE) elif rosetta_i18n_filter == 'fuzzy': paginator = Paginator([e for e in rosetta_i18n_pofile.fuzzy_entries() if not e.obsolete], rosetta_settings.MESSAGES_PER_PAGE) else: paginator = Paginator([e for e in rosetta_i18n_pofile if not e.obsolete], rosetta_settings.MESSAGES_PER_PAGE) if 'page' in request.GET and int(request.GET.get('page')) <= paginator.num_pages and int(request.GET.get('page')) > 0: page = int(request.GET.get('page')) else: page = 1 messages = paginator.page(page).object_list if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code: main_language = None for language in settings.LANGUAGES: if language[0] == rosetta_settings.MAIN_LANGUAGE: main_language = _(language[1]) break fl = ("/%s/" % rosetta_settings.MAIN_LANGUAGE).join(rosetta_i18n_fn.split("/%s/" % rosetta_i18n_lang_code)) po = pofile(fl) main_messages = [] for message in messages: message.main_lang = po.find(message.msgid).msgstr needs_pagination = paginator.num_pages > 1 if needs_pagination: if paginator.num_pages >= 10: page_range = pagination_range(1, paginator.num_pages, page) else: page_range = range(1,1+paginator.num_pages) ADMIN_MEDIA_PREFIX = settings.ADMIN_MEDIA_PREFIX ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS MESSAGES_SOURCE_LANGUAGE_NAME = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_NAME MESSAGES_SOURCE_LANGUAGE_CODE = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE if 'rosetta_last_save_error' in request.session: del(request.session['rosetta_last_save_error']) rosetta_last_save_error = True return render_to_response('rosetta/pofile.html', locals(), context_instance=RequestContext(request)) else: return list_languages(request)
#-*- coding: utf-8 -*- from django.apps import AppConfig from django.utils.encoding import smart_unicode class YourAppConfig(AppConfig): name = 'frisbee' verbose_name = smart_unicode('Frisbee')
def __unicode__(self): try: return smart_unicode('%s profile' % (self.registration_profile)) except: return smart_unicode('%d NO-Profile' % (self.pk))